Esempio n. 1
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();
        }
Esempio n. 2
0
        public async Task Handle_ParseUrls()
        {
            Random random         = new Random();
            Guid   clientSystemId = random.NextGuid();

            BeerClient savedClient = GetSaveBeerClient(random, clientSystemId);

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

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

            repoMock.Setup(x => x.CheckIfClientExists(clientSystemId)).ReturnsAsync(true).Verifiable();
            repoMock.Setup(x => x.GetClientById(clientSystemId)).ReturnsAsync(savedClient).Verifiable();

            repoMock.Setup(x => x.UpdateClient(It.Is <BeerClient>(y =>
                                                                  y.DisplayName == request.DisplayName
                                                                  ))).ReturnsAsync(true).Verifiable();

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

            Boolean result = await handler.Handle(new UpdateClientCommand(request), CancellationToken.None);

            Assert.True(result);

            Assert.Equal(new[] { "https://myapp.com" }, savedClient.AllowedCorsOrigins);
            Assert.Equal(request.AllowedScopes, savedClient.AllowedScopes);
            Assert.Equal(request.DisplayName, savedClient.DisplayName);
            Assert.Equal("http://mylogout.com/logout", savedClient.FrontChannelLogoutUri);
            Assert.Equal(new[] { "https://myapp2.com/login-callback" }, savedClient.RedirectUris);
            Assert.Equal(new[] { "https://myapp2.com/logout-callback" }, savedClient.PostLogoutRedirectUris);

            repoMock.Verify();
        }
Esempio n. 3
0
        public async Task GetClientById_ClientId()
        {
            Random random = new();

            String existingId = random.GetAlphanumericString();

            var beerClien = new BeerClient
            {
                Id                     = random.NextGuid(),
                ClientId               = existingId,
                AllowedCorsOrigins     = new[] { "https://myapp.com/" },
                AllowedScopes          = new[] { "test", "testus" },
                DisplayName            = random.GetAlphanumericString(),
                HashedPassword         = "******",
                FrontChannelLogoutUri  = "http://mylogout.com/logout/",
                PostLogoutRedirectUris = new[] { "https://myapp2.com/logout-callback/" },
                RedirectUris           = new[] { "https://myapp2.com/login-callback/" },
            };

            await ExecuteDatabaseAwareTest(async (store, repo) =>
            {
                using (var session = store.LightweightSession())
                {
                    session.Store(beerClien);

                    await session.SaveChangesAsync();
                }

                var result = await repo.GetClientById(existingId);
                Assert.NotNull(result);

                Assert.Equal(result.Id, result.Id);
                Assert.Equal(result.DisplayName, result.DisplayName);
                Assert.Equal(result.ClientId, result.ClientId);
                Assert.Equal(result.HashedPassword, result.HashedPassword);

                Assert.Equal(result.AllowedCorsOrigins, result.AllowedCorsOrigins);
                Assert.Equal(result.AllowedScopes, result.AllowedScopes);
                Assert.Equal(result.FrontChannelLogoutUri, result.FrontChannelLogoutUri);
                Assert.Equal(result.PostLogoutRedirectUris, result.PostLogoutRedirectUris);
                Assert.Equal(result.RedirectUris, result.RedirectUris);
            });
        }
Esempio n. 4
0
        public async Task <Boolean> Handle(UpdateClientCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug("Handle started");

            var content = request.Request;

            if (await _clientRepo.CheckIfClientExists(content.SystemId) == false)
            {
                return(false);
            }

            BeerClient client = await _clientRepo.GetClientById(content.SystemId);

            if (String.IsNullOrEmpty(content.Password) == false)
            {
                client.HashedPassword = content.Password.Sha256();
            }

            if (client.ClientId != content.ClientId)
            {
                if (await _clientRepo.CheckIfClientIdExists(content.ClientId, content.SystemId) == true)
                {
                    return(false);
                }
            }

            client.DisplayName            = content.DisplayName;
            client.ClientId               = content.ClientId;
            client.RedirectUris           = content.RedirectUris;
            client.AllowedCorsOrigins     = content.AllowedCorsOrigins ?? Array.Empty <String>();
            client.FrontChannelLogoutUri  = content.FrontChannelLogoutUri ?? String.Empty;
            client.PostLogoutRedirectUris = content.PostLogoutRedirectUris;
            client.AllowedScopes          = content.AllowedScopes;
            client.RequirePkce            = content.RequirePkce;

            client.ParseUrls();

            var result = await _clientRepo.UpdateClient(client);

            return(result);
        }
Esempio n. 5
0
        public async Task UpdateClient()
        {
            Random random = new();

            Guid existingId = random.NextGuid();

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

            await ExecuteDatabaseAwareTest(async (store, repo) =>
            {
                using (var session = store.LightweightSession())
                {
                    session.Store(exisitngBeerClient);

                    await session.SaveChangesAsync();
                }

                var clientToUpdate = new BeerClient
                {
                    Id                     = existingId,
                    ClientId               = random.GetAlphanumericString(),
                    AllowedCorsOrigins     = new[] { "https://myapp.com/" },
                    AllowedScopes          = new[] { "test", "testus" },
                    DisplayName            = random.GetAlphanumericString(),
                    HashedPassword         = exisitngBeerClient.HashedPassword,
                    FrontChannelLogoutUri  = "http://mylogout.com/logout/",
                    PostLogoutRedirectUris = new[] { "https://myapp2.com/logout-callback/" },
                    RedirectUris           = new[] { "https://myapp2.com/login-callback/" },
                };

                var result = await repo.UpdateClient(clientToUpdate);
                Assert.True(result);

                using (var querySessionn = store.QuerySession())
                {
                    var storedResult = await querySessionn.Query <BeerClient>().FirstOrDefaultAsync(x => x.Id == existingId);

                    Assert.Equal(clientToUpdate.Id, storedResult.Id);
                    Assert.Equal(clientToUpdate.DisplayName, storedResult.DisplayName);
                    Assert.Equal(clientToUpdate.ClientId, storedResult.ClientId);
                    Assert.Equal(clientToUpdate.HashedPassword, storedResult.HashedPassword);

                    Assert.Equal(clientToUpdate.AllowedCorsOrigins, storedResult.AllowedCorsOrigins);
                    Assert.Equal(clientToUpdate.AllowedScopes, storedResult.AllowedScopes);
                    Assert.Equal(clientToUpdate.FrontChannelLogoutUri, storedResult.FrontChannelLogoutUri);
                    Assert.Equal(clientToUpdate.PostLogoutRedirectUris, storedResult.PostLogoutRedirectUris);
                    Assert.Equal(clientToUpdate.RedirectUris, storedResult.RedirectUris);

                    Assert.False(storedResult.RequirePkce);
                }
            });
        }
Esempio n. 6
0
        public async Task GetAllClientsSortedByName()
        {
            Random random = new();

            Guid firstId  = random.NextGuid();
            Guid secondId = random.NextGuid();

            var firstBeerClient = new BeerClient
            {
                Id          = firstId,
                DisplayName = "my first client",

                ClientId               = random.GetAlphanumericString(),
                AllowedCorsOrigins     = new[] { $"https://{random.GetAlphanumericString()}.com/" },
                AllowedScopes          = new[] { $"test-{random.GetAlphanumericString()}", "testus" },
                HashedPassword         = "******",
                FrontChannelLogoutUri  = $"http://{random.GetAlphanumericString()}.com/logout/",
                PostLogoutRedirectUris = new[] { $"https://{random.GetAlphanumericString()}.com/logout-callback/" },
                RedirectUris           = new[] { $"https://{random.GetAlphanumericString()}.com/login-callback/" },
            };

            var secondBeerClient = new BeerClient
            {
                Id          = secondId,
                DisplayName = "a client",

                ClientId               = random.GetAlphanumericString(),
                AllowedCorsOrigins     = new[] { $"https://{random.GetAlphanumericString()}.com/" },
                AllowedScopes          = new[] { $"test-{random.GetAlphanumericString()}", "testus" },
                HashedPassword         = "******",
                FrontChannelLogoutUri  = $"http://{random.GetAlphanumericString()}.com/logout/",
                PostLogoutRedirectUris = new[] { $"https://{random.GetAlphanumericString()}.com/logout-callback/" },
                RedirectUris           = new[] { $"https://{random.GetAlphanumericString()}.com/login-callback/" },
            };

            await ExecuteDatabaseAwareTest(async (store, repo) =>
            {
                using (var session = store.LightweightSession())
                {
                    session.Store(firstBeerClient);
                    session.Store(secondBeerClient);

                    await session.SaveChangesAsync();
                }

                var existingResult = await repo.GetAllClientsSortedByName();
                Assert.NotNull(existingResult);
                Assert.NotEmpty(existingResult);
                Assert.Equal(2, existingResult.Count());

                {
                    var firstItem = existingResult.ElementAt(0);

                    Assert.Equal(secondBeerClient.Id, firstItem.SystemId);
                    Assert.Equal(secondBeerClient.DisplayName, firstItem.DisplayName);
                    Assert.Equal(secondBeerClient.ClientId, firstItem.ClientId);
                    Assert.Equal(secondBeerClient.AllowedCorsOrigins, firstItem.AllowedCorsOrigins);
                    Assert.Equal(secondBeerClient.AllowedScopes, firstItem.AllowedScopes);
                    Assert.Equal(secondBeerClient.FrontChannelLogoutUri, firstItem.FrontChannelLogoutUri);
                    Assert.Equal(secondBeerClient.PostLogoutRedirectUris, firstItem.PostLogoutRedirectUris);
                    Assert.Equal(secondBeerClient.RedirectUris, firstItem.RedirectUris);
                }

                {
                    var secomdItem = existingResult.ElementAt(1);

                    Assert.Equal(firstBeerClient.Id, secomdItem.SystemId);
                    Assert.Equal(firstBeerClient.DisplayName, secomdItem.DisplayName);
                    Assert.Equal(firstBeerClient.ClientId, secomdItem.ClientId);
                    Assert.Equal(firstBeerClient.AllowedCorsOrigins, secomdItem.AllowedCorsOrigins);
                    Assert.Equal(firstBeerClient.AllowedScopes, secomdItem.AllowedScopes);
                    Assert.Equal(firstBeerClient.FrontChannelLogoutUri, secomdItem.FrontChannelLogoutUri);
                    Assert.Equal(firstBeerClient.PostLogoutRedirectUris, secomdItem.PostLogoutRedirectUris);
                    Assert.Equal(firstBeerClient.RedirectUris, secomdItem.RedirectUris);
                }
            });
        }
Esempio n. 7
0
        private static async Task SendRequest(bool repoResult, bool setPassword, bool changeClientId)
        {
            Random random         = new Random();
            Guid   clientSystemId = random.NextGuid();

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

            if (setPassword == true)
            {
                request.Password = "******";
            }

            BeerClient savedClient = GetSaveBeerClient(random, clientSystemId);


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

            repoMock.Setup(x => x.CheckIfClientExists(clientSystemId)).ReturnsAsync(true).Verifiable();
            repoMock.Setup(x => x.GetClientById(clientSystemId)).ReturnsAsync(savedClient).Verifiable();

            repoMock.Setup(x => x.UpdateClient(It.Is <BeerClient>(y =>
                                                                  y.DisplayName == request.DisplayName
                                                                  ))).ReturnsAsync(repoResult).Verifiable();

            if (changeClientId == false)
            {
                request.ClientId = savedClient.ClientId;
            }
            else
            {
                repoMock.Setup(x => x.CheckIfClientIdExists(request.ClientId, request.SystemId)).ReturnsAsync(false).Verifiable();
            }

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

            Boolean actual = await handler.Handle(new UpdateClientCommand(request), CancellationToken.None);

            Assert.Equal(repoResult, actual);

            Assert.Equal(request.ClientId, savedClient.ClientId);
            Assert.Equal(request.AllowedCorsOrigins, savedClient.AllowedCorsOrigins);
            Assert.Equal(request.AllowedScopes, savedClient.AllowedScopes);
            Assert.Equal(request.DisplayName, savedClient.DisplayName);
            Assert.False(savedClient.RequirePkce);
            Assert.Equal(request.FrontChannelLogoutUri, savedClient.FrontChannelLogoutUri);
            if (setPassword == false)
            {
                Assert.Equal("previousPassword".Sha256(), savedClient.HashedPassword);
            }
            else
            {
                Assert.Equal("mynewchangedpassword".Sha256(), savedClient.HashedPassword);
            }

            Assert.Equal(request.PostLogoutRedirectUris, savedClient.PostLogoutRedirectUris);
            Assert.Equal(request.RedirectUris, savedClient.RedirectUris);

            repoMock.Verify();
        }