示例#1
0
        public void Does_process_messages_in_BasicAppHost()
        {
            using (var appHost = new BasicAppHost(typeof(HelloService).Assembly)
            {
                ConfigureAppHost = host =>
                {
                    host.Container.Register(c => CreateMqServer());

                    var mqServer = host.Container.Resolve <IMessageService>();

                    mqServer.RegisterHandler <HelloIntro>(host.ExecuteMessage);
                    mqServer.Start();
                }
            }.Init())
            {
                using (var mqClient = appHost.Resolve <IMessageService>().CreateMessageQueueClient())
                {
                    mqClient.Publish(new HelloIntro {
                        Name = "World"
                    });

                    IMessage <HelloIntroResponse> responseMsg = mqClient.Get <HelloIntroResponse>(QueueNames <HelloIntroResponse> .In);
                    mqClient.Ack(responseMsg);
                    Assert.That(responseMsg.GetBody().Result, Is.EqualTo("Hello, World!"));
                }
                appHost.Resolve <IMessageService>().Dispose();
            }
        }
        [Ignore] // todo: Think about how to test nuget server
        public void Deploy_XCopy_Package()
        {
            // arrange
            const string Version           = "1.3.3.7";
            const string Package           = "PowerDeploy.Sample.XCopy";
            const string TargetEnvironment = "unittest";

            _appHost.Container.Resolve <IDocumentStore>().CreateSzenario()
            .PublishPackage(Package, Version)
            .Play();

            System.Environment.CurrentDirectory = "samples".MapVcsRoot();

            var target = _appHost.Resolve <DeployService>();

            // make sure nuget server has no package with this version
            File.Delete(Path.Combine(NugetServerPackagesPath, "{0}.{1}.nupkg".Fmt(Package, Version)));

            MsBuild(@"{0}\{0}.csproj /t:clean,build /p:OctoPackPackageVersion={1} /p:OctoPackPublishPackageToFileShare={2} /p:Configuration=Release /p:RunOctoPack=true /v:m".Fmt(Package, Version, NugetServerPackagesPath));

            // act
            target.Post(new TriggerDeployment()
            {
                Environment = TargetEnvironment,
                PackageId   = Package,
                Version     = Version,
            });

            // assert
            var environment = new XmlEnvironmentSerializer().Deserialize(@".powerdeploy\{0}.xml".Fmt(TargetEnvironment));
            var targetPath  = environment["SampleAppConsole_Destination"];

            Assert.IsTrue(File.Exists(Path.Combine(targetPath.Value, "PowerDeploy.SampleApp.ConsoleXCopy.exe")));
        }
示例#3
0
        public void Does_dispose_request_scope_dependency_in_PostMessageHandler()
        {
            var disposeCount = 0;

            using (var appHost = new BasicAppHost(typeof(HelloWithDepService).Assembly)
            {
                ConfigureAppHost = host =>
                {
#if !NETCORE_SUPPORT
                    RequestContext.UseThreadStatic = true;
#endif
                    host.Container.Register <IDisposableDependency>(c => new DisposableDependency(() =>
                    {
                        Interlocked.Increment(ref disposeCount);
                    }))
                    .ReusedWithin(ReuseScope.Request);
                    host.Container.Register(c => CreateMqServer(host));

                    var mqServer = host.Container.Resolve <IMessageService>();

                    mqServer.RegisterHandler <HelloIntroWithDep>(host.ExecuteMessage);
                    mqServer.Start();
                }
            }.Init())
            {
                using (var mqClient = appHost.Resolve <IMessageService>().CreateMessageQueueClient())
                {
                    mqClient.Publish(new HelloIntroWithDep {
                        Name = "World"
                    });

                    IMessage <HelloIntroResponse> responseMsg = mqClient.Get <HelloIntroResponse>(QueueNames <HelloIntroResponse> .In);
                    mqClient.Ack(responseMsg);

                    Assert.That(disposeCount, Is.EqualTo(1));
                }
                appHost.Resolve <IMessageService>().Dispose();
            }
        }
        public void GetUsers_Test()
        {
            using (var appHost = new BasicAppHost(typeof(GetUsersService).Assembly).Init())
            {
                var request = new GetUsers
                {
                    UserIds   = new ArrayOfLong(1, 2),
                    UserNames = new ArrayOfString("User3", "User4")
                };

                var factory = new OrmLiteConnectionFactory(
                    InMemoryDb, SqliteDialect.Provider);

                using (var db = factory.Open())
                {
                    db.DropAndCreateTable <User>();
                    db.Insert(new User {
                        Id = 1, UserName = "******"
                    });
                    db.Insert(new User {
                        Id = 2, UserName = "******"
                    });
                    db.Insert(new User {
                        Id = 3, UserName = "******"
                    });
                    db.Insert(new User {
                        Id = 4, UserName = "******"
                    });

                    var handler = appHost.Resolve <GetUsersService>();

                    var response = handler.Any(request);

                    Assert.That(response.Users.Count, Is.EqualTo(4));
                }
            }
        }
示例#5
0
        public void CheckRoleAssignmentInUserAuthTable()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureContainer = container =>
                {
                    container.Register <IDbConnectionFactory>(DbConnFactory);
                    container.Register <IAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>()));
                }
            }.Init())
            {
                // Arrange
                UserAuth userAuth;
                var      newRegistration = CreateNewUserRegistration();
                var      request         = new BasicRequest(newRegistration);
                var      response        = (RegisterResponse)appHost.ExecuteService(newRegistration, request);
                var      authRepo        = appHost.Resolve <IAuthRepository>();

                // Test #1: Check role and permission assignment
                // ---------------------------------------------
                // Act
                using (var db = DbConnFactory.Open())
                {
                    // Hydrate userAuth
                    userAuth = db.SingleById <UserAuth>(response.UserId);
                }

                var assignRoleRequest =
                    new AssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.AssignRoles(userAuth, assignRoleRequest.Roles, assignRoleRequest.Permissions);

                // Assert:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.EqualTo(TestRoleName));
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.EqualTo(TestPermissionName));

                // Test #2: Check role and permission un-assignment
                // ------------------------------------------------
                // Act
                using (var db = DbConnFactory.Open())
                {
                    // Hydrate userAuth
                    userAuth = db.SingleById <UserAuth>(response.UserId);
                }

                var unassignRolesRequest =
                    new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.UnAssignRoles(userAuth, unassignRolesRequest.Roles, unassignRolesRequest.Permissions);

                // Assert:
                // Check UserAuth not to contain roles and permissions above
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.Null);
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.Null);
            }
        }
示例#6
0
        public void CheckRoleAssignmentInUserRoleTableUsingIAuthRepository()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureContainer = container =>
                {
                    container.Register <IUnitOfWork>(c => UnitOfWork);
                    container.Register <IAuthRepository>(c =>
                                                         new LightSpeedUserAuthRepository(c.Resolve <IUnitOfWork>())
                    {
                        UseDistinctRoleTables = true
                    });
                }
            }.Init())
            {
                // Arrange
                LightSpeed.UserAuth userAuth;
                AssignRolesResponse assignRolesResponse;
                var newRegistration = CreateNewUserRegistration();
                var request         = new BasicRequest(newRegistration);
                var response        = (RegisterResponse)appHost.ExecuteService(newRegistration, request);
                var authRepo        = appHost.Resolve <IAuthRepository>();

                // Test #1: Check role and permission assignment
                // ---------------------------------------------
                // Act
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId); // Hydrate userAuth

                var assignRoleRequest =
                    new AssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (LightSpeed.UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.AssignRoles(userAuth, assignRoleRequest.Roles, assignRoleRequest.Permissions);

                // Assert #1.1:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.EqualTo(TestRoleName));
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.EqualTo(TestPermissionName));

                // Assert #1.2:
                // Check that roles and permissions are not persisted to UserAuth table
                Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));

                // Assert #1.3:
                // Check UserRoles table to contain roles and permissions
                var manageRoles = (IManageRoles)appHost.Container.Resolve <IAuthRepository>();
                Assert.That(manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));

                // Test #2: Check role and permission un-assignment
                // ------------------------------------------------
                // Act
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId); // Hydrate userAuth

                var unassignRolesRequest =
                    new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (LightSpeed.UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.UnAssignRoles(userAuth, unassignRolesRequest.Roles, unassignRolesRequest.Permissions);

                // Assert #2.1:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.Null);
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.Null);

                // Assert #2.2:
                // Check UserRole table not to contain roles and permissions above
                Assert.That(!manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(!manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));
            }
        }