示例#1
0
        public async Task AddClientAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Generate random new client
                var client = ClientDtoMock.GenerateRandomClient(0);

                await clientService.AddClientAsync(client);

                //Get new client
                var clientEntity =
                    await context.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                var clientDto = await clientService.GetClientAsync(clientEntity.Id);

                //Assert new client
                client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));
            }
        }
示例#2
0
        public async Task RemoveClientAsync()
        {
            IClientRepository clientRepository = new ClientDapperRepository(_configuration);

            var localizerMock = new Mock <IClientServiceResources>();
            var localizer     = localizerMock.Object;

            IClientService clientService = new ClientService(clientRepository, localizer);

            //Generate random new client without id
            var client = ClientDtoMock.GenerateRandomClient(0);

            //Add new client
            var clientId = await clientService.AddClientAsync(client);

            //Get new client
            var clientEntity = await clientRepository.GetClientAsync(clientId);

            var clientDto = await clientService.GetClientAsync(clientEntity.Id);

            //Assert new client
            client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

            //Remove client
            await clientService.RemoveClientAsync(clientDto);

            //Try Get Removed client
            var removeClientEntity = await clientRepository.GetClientAsync(clientEntity.Id);

            //Assert removed client - it might be null
            removeClientEntity.Should().BeNull();
        }
示例#3
0
        public async Task CloneClientAsync()
        {
            int clonedClientId;

            //Generate random new client
            var clientDto = ClientDtoMock.GenerateRandomClient(0);

            IClientRepository clientRepository = new ClientDapperRepository(_configuration);

            var localizerMock = new Mock <IClientServiceResources>();
            var localizer     = localizerMock.Object;

            IClientService clientService = new ClientService(clientRepository, localizer);

            //Add new client
            var clientId = await clientService.AddClientAsync(clientDto);

            var clientDtoToClone = await clientService.GetClientAsync(clientId);

            var clientCloneDto = ClientDtoMock.GenerateClientCloneDto(clientDtoToClone);

            //Try clone it
            clonedClientId = await clientService.CloneClientAsync(clientCloneDto);

            var cloneClientEntity = await clientRepository.GetClientAsync(clonedClientId);

            //Assert cloned client
            cloneClientEntity.ShouldBeEquivalentTo(clientDtoToClone,
                                                   options => options.Excluding(o => o.Id)
                                                   .Excluding(o => o.ClientSecrets)
                                                   .Excluding(o => o.ClientId)
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedGrantTypes\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedGrantTypes\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedGrantTypes\\[.+\\].GrantType"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedGrantTypes\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "RedirectUris\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "RedirectUris\\[.+\\].RedirectUri"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "RedirectUris\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "PostLogoutRedirectUris\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "PostLogoutRedirectUris\\[.+\\].PostLogoutRedirectUri"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "PostLogoutRedirectUris\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedScopes\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedScopes\\[.+\\].Scope"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedScopes\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "IdentityProviderRestrictions\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "IdentityProviderRestrictions\\[.+\\].Provider"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "IdentityProviderRestrictions\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "ClientSecrets\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "Claims\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "IdentityProviderRestrictions\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedCorsOrigins\\[.+\\].Id"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedCorsOrigins\\[.+\\].Origin"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "AllowedCorsOrigins\\[.+\\].Client"))
                                                   .Excluding(x => Regex.IsMatch(x.SelectedMemberPath, "Properties\\[.+\\].Id")));
        }
示例#4
0
        public async Task DeleteClientSecretAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Generate random new client
                var client = ClientDtoMock.GenerateRandomClient(0);

                await clientService.AddClientAsync(client);

                //Get new client
                var clientEntity =
                    await context.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                var clientDto = await clientService.GetClientAsync(clientEntity.Id);

                //Assert new client
                client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

                //Generate random new Client secret
                var clientSecret = ClientDtoMock.GenerateRandomClientSecret(0, clientEntity.Id);

                //Add new client secret
                await clientService.AddClientSecretAsync(clientSecret);

                //Get inserted client secret
                var secret = await context.ClientSecrets.Where(x => x.Value == clientSecret.Value && x.Client.Id == clientEntity.Id)
                             .SingleOrDefaultAsync();

                //Map entity to model
                var secretsDto = secret.ToModel();

                //Get new client secret
                var clientSecretsDto = await clientService.GetClientSecretAsync(secret.Id);

                //Assert
                clientSecretsDto.ShouldBeEquivalentTo(secretsDto, options => options.Excluding(o => o.ClientSecretId));

                //Delete client secret
                await clientService.DeleteClientSecretAsync(clientSecretsDto);

                //Get removed client secret
                var deleteClientSecret = await context.ClientSecrets.Where(x => x.Id == secret.Id).SingleOrDefaultAsync();

                //Assert after delete it
                deleteClientSecret.Should().BeNull();
            }
        }
示例#5
0
        public async Task DeleteClientSecretAsync()
        {
            IClientRepository clientRepository = new ClientDapperRepository(_configuration);

            var localizerMock = new Mock <IClientServiceResources>();
            var localizer     = localizerMock.Object;

            IClientService clientService = new ClientService(clientRepository, localizer);

            //Generate random new client
            var client = ClientDtoMock.GenerateRandomClient(0);

            var clientId = await clientService.AddClientAsync(client);

            //Get new client
            var clientEntity = await clientRepository.GetClientAsync(clientId);

            var clientDto = await clientService.GetClientAsync(clientEntity.Id);

            //Assert new client
            client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

            //Generate random new Client secret
            var clientSecret = ClientDtoMock.GenerateRandomClientSecret(0, clientEntity.Id);

            //Add new client secret
            var clientSecretId = await clientService.AddClientSecretAsync(clientSecret);

            //Get inserted client property
            var secret = await clientRepository.GetClientSecretAsync(clientSecretId);

            //Map entity to model
            var secretsDto = secret.ToModel();

            //Get new client secret
            var clientSecretsDto = await clientService.GetClientSecretAsync(secret.Id);

            //Assert
            clientSecretsDto.ShouldBeEquivalentTo(secretsDto, options => options.Excluding(o => o.ClientSecretId));

            //Delete client secret
            await clientService.DeleteClientSecretAsync(clientSecretsDto);

            //Get removed client secret
            var deleteClientSecret = await clientRepository.GetClientSecretAsync(secret.Id);

            //Assert after delete it
            deleteClientSecret.Should().BeNull();
        }
示例#6
0
        public async Task GetClientPropertyAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Generate random new client
                var client = ClientDtoMock.GenerateRandomClient(0);

                await clientService.AddClientAsync(client);

                //Get new client
                var clientEntity =
                    await context.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                var clientDto = await clientService.GetClientAsync(clientEntity.Id);

                //Assert new client
                client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

                //Generate random new Client property
                var clicentProperty = ClientDtoMock.GenerateRandomClientProperty(0, clientEntity.Id);

                //Add new client property
                await clientService.AddClientPropertyAsync(clicentProperty);

                //Get inserted client property
                var property = await context.ClientProperties.Where(x => x.Value == clicentProperty.Value && x.Client.Id == clientEntity.Id)
                               .SingleOrDefaultAsync();

                //Map entity to model
                var propertyDto = property.ToModel();

                //Get new client property
                var clientPropertiesDto = await clientService.GetClientPropertyAsync(property.Id);

                //Assert
                clientPropertiesDto.ShouldBeEquivalentTo(propertyDto, options => options.Excluding(o => o.ClientPropertyId));
            }
        }
示例#7
0
        public async Task UpdateClientAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Generate random new client without id
                var client = ClientDtoMock.GenerateRandomClient(0);

                //Add new client
                await clientService.AddClientAsync(client);

                //Get new client
                var clientEntity =
                    await context.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                var clientDto = await clientService.GetClientAsync(clientEntity.Id);

                //Assert new client
                client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

                //Detached the added item
                context.Entry(clientEntity).State = EntityState.Detached;

                //Generete new client with added item id
                var updatedClient = ClientDtoMock.GenerateRandomClient(clientDto.Id);

                //Update client
                await clientService.UpdateClientAsync(updatedClient);

                //Get updated client
                var updatedClientEntity = await context.Clients.Where(x => x.Id == updatedClient.Id).SingleAsync();

                var updatedClientDto = await clientService.GetClientAsync(updatedClientEntity.Id);

                //Assert updated client
                updatedClient.ShouldBeEquivalentTo(updatedClientDto, options => options.Excluding(o => o.Id));
            }
        }
        public async Task RemoveClientAsync()
        {
            using (var context = new ConfigurationDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Generate random new client without id
                var client = ClientDtoMock.GenerateRandomClient(0);

                //Add new client
                await clientService.AddClientAsync(client);

                //Get new client
                var clientEntity =
                    await context.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                var clientDto = await clientService.GetClientAsync(clientEntity.Id);

                //Assert new client
                client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

                //Detached the added item
                context.Entry(clientEntity).State = EntityState.Detached;

                //Remove client
                await clientService.RemoveClientAsync(clientDto);

                //Try Get Removed client
                var removeClientEntity = await context.Clients.Where(x => x.Id == clientEntity.Id)
                                         .SingleOrDefaultAsync();

                //Assert removed client - it might be null
                removeClientEntity.Should().BeNull();
            }
        }
示例#9
0
        public async Task GetClientPropertyAsync()
        {
            IClientRepository clientRepository = new ClientDapperRepository(_configuration);

            var localizerMock = new Mock <IClientServiceResources>();
            var localizer     = localizerMock.Object;

            IClientService clientService = new ClientService(clientRepository, localizer);

            //Generate random new client
            var client = ClientDtoMock.GenerateRandomClient(0);

            var clientId = await clientService.AddClientAsync(client);

            //Get new client
            var clientEntity = await clientRepository.GetClientAsync(clientId);

            var clientDto = await clientService.GetClientAsync(clientEntity.Id);

            //Assert new client
            client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));

            //Generate random new Client property
            var clientProperty = ClientDtoMock.GenerateRandomClientProperty(0, clientEntity.Id);

            //Add new client property
            var clientPropertyId = await clientService.AddClientPropertyAsync(clientProperty);

            //Get inserted client property
            var property = await clientRepository.GetClientPropertyAsync(clientPropertyId);

            //Map entity to model
            var propertyDto = property.ToModel();

            //Get new client property
            var clientPropertiesDto = await clientService.GetClientPropertyAsync(property.Id);

            //Assert
            clientPropertiesDto.ShouldBeEquivalentTo(propertyDto, options => options.Excluding(o => o.ClientPropertyId));
        }
示例#10
0
        public async Task AddClientAsync()
        {
            IClientRepository clientRepository = new ClientDapperRepository(_configuration);

            var localizerMock = new Mock <IClientServiceResources>();
            var localizer     = localizerMock.Object;

            IClientService clientService = new ClientService(clientRepository, localizer);

            //Generate random new client
            var client = ClientDtoMock.GenerateRandomClient(0);

            var clientId = await clientService.AddClientAsync(client);

            //Get new client
            var clientEntity = await clientRepository.GetClientAsync(clientId);

            var clientDto = await clientService.GetClientAsync(clientEntity.Id);

            //Assert new client
            client.ShouldBeEquivalentTo(clientDto, options => options.Excluding(o => o.Id));
        }
示例#11
0
        public async Task CloneClientAsync()
        {
            int clonedClientId;

            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                //Generate random new client
                var clientDto = ClientDtoMock.GenerateRandomClient(0);

                IClientRepository clientRepository = new ClientRepository(context);

                var localizerMock = new Mock <IClientServiceResources>();
                var localizer     = localizerMock.Object;

                IClientService clientService = new ClientService(clientRepository, localizer);

                //Add new client
                await clientService.AddClientAsync(clientDto);

                var clientId = await context.Clients.Where(x => x.ClientId == clientDto.ClientId).Select(x => x.Id)
                               .SingleOrDefaultAsync();

                var clientDtoToClone = await clientService.GetClientAsync(clientId);

                var clientCloneDto = ClientDtoMock.GenerateClientCloneDto(clientDtoToClone);

                //Try clone it
                clonedClientId = await clientService.CloneClientAsync(clientCloneDto);

                var cloneClientEntity = await context.Clients
                                        .Include(x => x.AllowedGrantTypes)
                                        .Include(x => x.RedirectUris)
                                        .Include(x => x.PostLogoutRedirectUris)
                                        .Include(x => x.AllowedScopes)
                                        .Include(x => x.ClientSecrets)
                                        .Include(x => x.Claims)
                                        .Include(x => x.IdentityProviderRestrictions)
                                        .Include(x => x.AllowedCorsOrigins)
                                        .Include(x => x.Properties)
                                        .Where(x => x.Id == clonedClientId).SingleOrDefaultAsync();

                var clientToCompare = await context.Clients
                                      .Include(x => x.AllowedGrantTypes)
                                      .Include(x => x.RedirectUris)
                                      .Include(x => x.PostLogoutRedirectUris)
                                      .Include(x => x.AllowedScopes)
                                      .Include(x => x.ClientSecrets)
                                      .Include(x => x.Claims)
                                      .Include(x => x.IdentityProviderRestrictions)
                                      .Include(x => x.AllowedCorsOrigins)
                                      .Include(x => x.Properties)
                                      .Where(x => x.Id == clientDtoToClone.Id).SingleOrDefaultAsync();

                //Assert cloned client
                cloneClientEntity.ShouldBeEquivalentTo(clientToCompare,
                                                       options => options.Excluding(o => o.Id)
                                                       .Excluding(o => o.ClientSecrets)
                                                       .Excluding(o => o.ClientId)
                                                       .Excluding(o => o.ClientName)

                                                       //Skip the collections because is not possible ignore property in list :-(
                                                       //Note: I've found the solution above - try ignore property of the list using SelectedMemberPath
                                                       .Excluding(o => o.AllowedGrantTypes)
                                                       .Excluding(o => o.RedirectUris)
                                                       .Excluding(o => o.PostLogoutRedirectUris)
                                                       .Excluding(o => o.AllowedScopes)
                                                       .Excluding(o => o.ClientSecrets)
                                                       .Excluding(o => o.Claims)
                                                       .Excluding(o => o.IdentityProviderRestrictions)
                                                       .Excluding(o => o.AllowedCorsOrigins)
                                                       .Excluding(o => o.Properties)
                                                       );


                //New client relations have new id's and client relations therefore is required ignore them
                cloneClientEntity.AllowedGrantTypes.ShouldBeEquivalentTo(clientToCompare.AllowedGrantTypes,
                                                                         option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                         .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.AllowedCorsOrigins.ShouldBeEquivalentTo(clientToCompare.AllowedCorsOrigins,
                                                                          option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                          .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.RedirectUris.ShouldBeEquivalentTo(clientToCompare.RedirectUris,
                                                                    option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                    .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.PostLogoutRedirectUris.ShouldBeEquivalentTo(clientToCompare.PostLogoutRedirectUris,
                                                                              option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                              .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.AllowedScopes.ShouldBeEquivalentTo(clientToCompare.AllowedScopes,
                                                                     option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                     .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.ClientSecrets.ShouldBeEquivalentTo(clientToCompare.ClientSecrets,
                                                                     option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                     .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.Claims.ShouldBeEquivalentTo(clientToCompare.Claims,
                                                              option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                              .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.IdentityProviderRestrictions.ShouldBeEquivalentTo(
                    clientToCompare.IdentityProviderRestrictions,
                    option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                    .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));

                cloneClientEntity.Properties.ShouldBeEquivalentTo(clientToCompare.Properties,
                                                                  option => option.Excluding(x => x.SelectedMemberPath.EndsWith("Id"))
                                                                  .Excluding(x => x.SelectedMemberPath.EndsWith("Client")));
            }
        }