Пример #1
0
        public async Task ShouldCreate_A_Client()
        {
            string name     = "Vader";
            Guid   clientId = Guid.NewGuid();

            var request = new CreateClientCommand {
                Name = name
            };
            var expected = new CreateClientResponse {
                Name = name, Id = clientId
            };

            var mapper         = PetShopMappingConfiguration.GetPetShopMappings();
            var mockRepository = new Mock <IClientRepository>();

            mockRepository.Setup(p => p.Add(It.Is <Client>(c => c.Name == name)))
            .Returns((Client client) => Task.Run(() =>
            {
                client.Id = clientId;
            }));

            var handler = new CreateClientCommandHandler(mapper, mockRepository.Object);

            var result = await handler.Handle(request, CancellationToken.None);

            result.Data.Should().BeEquivalentTo(expected);
            result.Message.Should().BeEquivalentTo("Client Created");
            mockRepository.Verify(m => m.Add(It.IsAny <Client>()), Times.Once());
        }
 public void SetUp()
 {
     _mockLogger                 = new Mock <ILogger <CreateClientCommandHandler> >();
     _mockClientService          = new Mock <IClientService>();
     _createClientCommandHandler = new CreateClientCommandHandler(_mockClientService.Object, _mockLogger.Object);
     _fixture = new Fixture();
 }
Пример #3
0
        public async Task Handle(Boolean repoResult)
        {
            Random random         = new Random();
            Guid   clientSystemId = random.NextGuid();

            var request = new CreateClientRequest
            {
                ClientId               = random.GetAlphanumericString(),
                AllowedCorsOrigins     = new[] { "https://myapp.com" },
                AllowedScopes          = new[] { "test", "testus" },
                DisplayName            = random.GetAlphanumericString(),
                FrontChannelLogoutUri  = "http://mylogout.com/logout",
                Password               = "******",
                PostLogoutRedirectUris = new[] { "https://myapp2.com/logout-callback" },
                RedirectUris           = new[] { "https://myapp2.com/login-callback" },
                RequirePkce            = true,
            };

            BeerClient clientCreated = null;

            var repoMock = new Mock <IClientRepository>(MockBehavior.Strict);

            repoMock.Setup(x => x.CheckIfClientIdExists(request.ClientId)).ReturnsAsync(false).Verifiable();
            repoMock.Setup(x => x.AddClient(It.Is <BeerClient>(y =>
                                                               y.DisplayName == request.DisplayName
                                                               ))).ReturnsAsync(repoResult).Callback <BeerClient>(x => { clientCreated = x; x.Id = clientSystemId; }).Verifiable();

            var handler = new CreateClientCommandHandler(repoMock.Object,
                                                         Mock.Of <ILogger <CreateClientCommandHandler> >());

            Guid?result = await handler.Handle(new CreateClientCommand(request), CancellationToken.None);

            if (repoResult == true)
            {
                Assert.True(result.HasValue);
                Assert.Equal(clientSystemId, result.Value);
            }
            else
            {
                Assert.False(result.HasValue);
            }

            Assert.Equal(request.ClientId, clientCreated.ClientId);
            Assert.Equal(request.AllowedCorsOrigins, clientCreated.AllowedCorsOrigins);
            Assert.Equal(request.AllowedScopes, clientCreated.AllowedScopes);
            Assert.Equal(request.DisplayName, clientCreated.DisplayName);
            Assert.Equal(request.FrontChannelLogoutUri, clientCreated.FrontChannelLogoutUri);
            Assert.Equal("mypassword".Sha256(), clientCreated.HashedPassword);
            Assert.Equal(request.PostLogoutRedirectUris, clientCreated.PostLogoutRedirectUris);
            Assert.Equal(request.RedirectUris, clientCreated.RedirectUris);
            Assert.True(clientCreated.RequirePkce);

            repoMock.Verify();
        }
Пример #4
0
        public async Task Handle_WithEmptyClientName_ThrowsException()
        {
            using (var db = new InMemoryDataContextBuilder()
                            .BuildScoped())
            {
                var handler = new CreateClientCommandHandler(db.Context);

                await Assert.ThrowsAsync <ValidationException>(() => handler.Handle(new CreateClientCommand
                {
                    Name = string.Empty,
                    AccountManagerEmail = "*****@*****.**",
                    AccountManagerName  = "Manager One"
                }, CancellationToken.None));
            }
        }
Пример #5
0
        public async Task Handle_WithValidClientDetails_CreateTheRecord()
        {
            using (var db = new InMemoryDataContextBuilder().BuildScoped())
            {
                var handler = new CreateClientCommandHandler(db.Context);
                await handler.Handle(new CreateClientCommand
                {
                    Name = "Fabrikam",
                    AccountManagerEmail = "*****@*****.**",
                    AccountManagerName  = "Manager One"
                }, CancellationToken.None);

                using (var varifyContext = new InMemoryDataContextBuilder().WithDbName(db.DatabaseName).Build())
                {
                    varifyContext.Clients
                    .FirstOrDefaultAsync(c => c.Name == "Fabrikam")
                    .Should()
                    .NotBeNull();
                }
            }
        }
Пример #6
0
        public async Task Handle_Failed_ClientIdExsits()
        {
            Random random         = new Random();
            Guid   clientSystemId = random.NextGuid();

            var request = new CreateClientRequest
            {
                ClientId = random.GetAlphanumericString(),
            };

            var repoMock = new Mock <IClientRepository>(MockBehavior.Strict);

            repoMock.Setup(x => x.CheckIfClientIdExists(request.ClientId)).ReturnsAsync(true).Verifiable();

            var handler = new CreateClientCommandHandler(repoMock.Object,
                                                         Mock.Of <ILogger <CreateClientCommandHandler> >());

            Guid?result = await handler.Handle(new CreateClientCommand(request), CancellationToken.None);

            Assert.False(result.HasValue);

            repoMock.Verify();
        }
 public CreateClientCommandHandlerTests()
 {
     mediator = new Mock <IMediator>();
     sut      = new CreateClientCommandHandler(context, mediator.Object);
 }
Пример #8
0
        public void CreateClient(CreateClientCommand command)
        {
            var handler = new CreateClientCommandHandler(new ClientRepository());

            handler.Handle(command);
        }