コード例 #1
0
        public async Task AddApiScope()
        {
            //Get Services
            var serviceProvider    = GetServices();
            var dbContext          = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>();
            var apiResourceService = serviceProvider.GetRequiredService <IApiResourceService>();

            // Get controller
            var controller     = PrepareConfigurationController(serviceProvider);
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);
            await apiResourceService.AddApiResourceAsync(apiResourceDto);

            var resource = await dbContext.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

            var apiScopeDto = ApiResourceDtoMock.GenerateRandomApiScope(0, resource.Id);

            var result = await controller.ApiScopes(apiScopeDto);

            // Assert
            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            viewResult.ActionName.Should().Be("ApiScopes");

            var apiScope = await dbContext.ApiScopes.Where(x => x.Name == apiScopeDto.Name).SingleOrDefaultAsync();

            var addedApiScope = await apiResourceService.GetApiScopeAsync(resource.Id, apiScope.Id);

            apiScopeDto.Should().BeEquivalentTo(addedApiScope, opts => opts.Excluding(x => x.ApiResourceId).Excluding(x => x.ResourceName).Excluding(x => x.ApiScopeId));
        }
コード例 #2
0
        public async Task DeleteApiResource()
        {
            //Get Services
            var serviceProvider    = GetServices();
            var dbContext          = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>();
            var apiResourceService = serviceProvider.GetRequiredService <IApiResourceService>();

            // Get controller
            var controller     = PrepareConfigurationController(serviceProvider);
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);
            await apiResourceService.AddApiResourceAsync(apiResourceDto);

            var apiResourceId = await dbContext.ApiResources.Where(x => x.Name == apiResourceDto.Name).Select(x => x.Id).SingleOrDefaultAsync();

            apiResourceId.Should().NotBe(0);

            apiResourceDto.Id = apiResourceId;
            var result = await controller.ApiResourceDelete(apiResourceDto);

            // Assert
            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            viewResult.ActionName.Should().Be("ApiResources");

            var apiResource = await dbContext.ApiResources.Where(x => x.Id == apiResourceDto.Id).SingleOrDefaultAsync();

            apiResource.Should().BeNull();
        }
コード例 #3
0
        public void CanMapApiResourceDtoToEntity()
        {
            //Generate DTO
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(1);

            //Try map to entity
            var apiResource = apiResourceDto.ToEntity();

            apiResource.Should().NotBeNull();

            apiResource.ShouldBeEquivalentTo(apiResourceDto, options =>
                                             options.Excluding(o => o.Secrets)
                                             .Excluding(o => o.Scopes)
                                             .Excluding(o => o.Properties)
                                             .Excluding(o => o.Created)
                                             .Excluding(o => o.Updated)
                                             .Excluding(o => o.LastAccessed)
                                             .Excluding(o => o.NonEditable)
                                             .Excluding(o => o.AllowedAccessTokenSigningAlgorithms)
                                             .Excluding(o => o.UserClaims));

            //Assert collection
            apiResource.UserClaims.Select(x => x.Type).ShouldBeEquivalentTo(apiResourceDto.UserClaims);
            var allowedAlgList = AllowedSigningAlgorithmsConverter.Converter.Convert(apiResource.AllowedAccessTokenSigningAlgorithms, null);

            allowedAlgList.ShouldBeEquivalentTo(apiResourceDto.AllowedAccessTokenSigningAlgorithms);
        }
コード例 #4
0
        public async Task GetApiSecrets()
        {
            //Get Services
            var serviceProvider    = GetServices();
            var dbContext          = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>();
            var apiResourceService = serviceProvider.GetRequiredService <IApiResourceService>();

            // Get controller
            var controller     = PrepareConfigurationController(serviceProvider);
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);
            await apiResourceService.AddApiResourceAsync(apiResourceDto);

            var resource = await dbContext.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

            const int generateApiSecrets = 5;

            for (var i = 0; i < generateApiSecrets; i++)
            {
                var apiSecretsDto = ApiResourceDtoMock.GenerateRandomApiSecret(0, resource.Id);
                await apiResourceService.AddApiSecretAsync(apiSecretsDto);
            }

            var result = await controller.ApiSecrets(resource.Id, 1);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            viewResult.ViewName.Should().BeNullOrEmpty();
            viewResult.ViewData.Should().NotBeNull();

            var viewModel = Assert.IsType <ApiSecretsDto>(viewResult.ViewData.Model);

            viewModel.ApiSecrets.Count.Should().Be(generateApiSecrets);
        }
コード例 #5
0
        public async Task DeleteApiScopeAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.Should().BeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api scope
                var apiScopeDtoMock = ApiResourceDtoMock.GenerateRandomApiScope(0, newApiResourceDto.Id);

                //Add new api scope
                await apiResourceService.AddApiScopeAsync(newApiResourceDto.Id, apiScopeDtoMock);

                //Get inserted api scope
                var apiScope = await context.ApiScopes.FirstOrDefaultAsync(x => x.Name == apiScopeDtoMock.Name && x.Id == newApiResourceDto.Id);

                //Map entity to model
                var apiScopesDto = apiScope.ToModel();

                //Get new api scope
                var newApiScope = await apiResourceService.GetApiScopeAsync(newApiResourceDto.Id, apiScopesDto.ApiScopeId);

                //Assert
                newApiScope.Should().BeEquivalentTo(apiScopesDto, o =>
                                                    o.Excluding(x => x.ShowInDiscoveryDocument)
                                                    .Excluding(x => x.ApiResourceId)
                                                    .Excluding(x => x.ResourceName)
                                                    .Excluding(x => x.Description)
                                                    .Excluding(x => x.DisplayName)
                                                    .Excluding(x => x.PageSize)
                                                    .Excluding(x => x.TotalCount)
                                                    .Excluding(x => x.ApiScopeId)
                                                    .Excluding(x => x.Required)
                                                    .Excluding(x => x.Emphasize)
                                                    .Excluding(x => x.Scopes)
                                                    .Excluding(x => x.Name)
                                                    .Excluding(x => x.UserClaims)
                                                    );

                //Delete it
                await apiResourceService.DeleteApiScopeAsync(newApiScope);

                var deletedApiScope = await context.ApiScopes.FirstOrDefaultAsync(x => x.Name == apiScopeDtoMock.Name && x.Id == newApiResourceDto.Id);

                //Assert after deleting
                deletedApiScope.Should().BeNull();
            }
        }
コード例 #6
0
        public async Task GetApiResourceAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IApiResourceRepository apiResourceRepository = new ApiResourceRepository(context);
                IClientRepository      clientRepository      = new ClientRepository(context);

                var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
                var localizerApiResource     = localizerApiResourceMock.Object;

                var localizerClientResourceMock = new Mock <IClientServiceResources>();
                var localizerClientResource     = localizerClientResourceMock.Object;

                IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
                IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));
            }
        }
コード例 #7
0
        public async Task UpdateApiResourceAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.Should().BeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

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

                //Generete new api resuorce with added item id
                var updatedApiResource = ApiResourceDtoMock.GenerateRandomApiResource(apiResource.Id);

                //Update api resource
                await apiResourceService.UpdateApiResourceAsync(updatedApiResource);

                var updatedApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert updated api resuorce
                updatedApiResource.Should().BeEquivalentTo(updatedApiResourceDto, options => options.Excluding(o => o.Id));
            }
        }
コード例 #8
0
        public async Task DeleteApiResourcePropertyAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceRepository = GetApiResourceRepository(context);
                var clientRepository      = GetClientRepository(context);

                var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
                var localizerApiResource     = localizerApiResourceMock.Object;

                var localizerClientResourceMock = new Mock <IClientServiceResources>();
                var localizerClientResource     = localizerClientResourceMock.Object;

                var clientService      = GetClientService(clientRepository, localizerClientResource);
                var apiResourceService = GetApiResourceService(apiResourceRepository, localizerApiResource, clientService);

                //Generate random new api resource
                var apiResource = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResource);

                //Get new api resource
                var resource = await context.ApiResources.Where(x => x.Name == apiResource.Name).SingleOrDefaultAsync();

                var apiResourceDto = await apiResourceService.GetApiResourceAsync(resource.Id);

                //Assert new api resource
                apiResource.ShouldBeEquivalentTo(apiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api resource Property
                var apiResourcePropertiesDto = ApiResourceDtoMock.GenerateRandomApiResourceProperty(0, resource.Id);

                //Add new api resource Property
                await apiResourceService.AddApiResourcePropertyAsync(apiResourcePropertiesDto);

                //Get inserted api resource Property
                var property = await context.ApiResourceProperties.Where(x => x.Value == apiResourcePropertiesDto.Value && x.ApiResource.Id == resource.Id)
                               .SingleOrDefaultAsync();

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

                //Get new api resource Property
                var resourcePropertiesDto = await apiResourceService.GetApiResourcePropertyAsync(property.Id);

                //Assert
                resourcePropertiesDto.ShouldBeEquivalentTo(propertiesDto, options =>
                                                           options.Excluding(o => o.ApiResourcePropertyId)
                                                           .Excluding(o => o.ApiResourceName));

                //Delete api resource Property
                await apiResourceService.DeleteApiResourcePropertyAsync(resourcePropertiesDto);

                //Get removed api resource Property
                var apiResourceProperty = await context.ApiResourceProperties.Where(x => x.Id == property.Id).SingleOrDefaultAsync();

                //Assert after delete it
                apiResourceProperty.Should().BeNull();
            }
        }
コード例 #9
0
        public async Task RemoveApiResourceAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.Should().BeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Remove api resource
                await apiResourceService.DeleteApiResourceAsync(newApiResourceDto);

                //Try get removed api resource
                var removeApiResource = await context.ApiResources.Where(x => x.Id == apiResource.Id)
                                        .SingleOrDefaultAsync();

                //Assert removed api resource
                removeApiResource.Should().BeNull();
            }
        }
コード例 #10
0
        public async Task RemoveApiResourceAsync()
        {
            IApiResourceRepository apiResourceRepository = new ApiResourceDapperRepository(_configuration);
            IClientRepository      clientRepository      = new ClientDapperRepository(_configuration);

            var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
            var localizerApiResource     = localizerApiResourceMock.Object;

            var localizerClientResourceMock = new Mock <IClientServiceResources>();
            var localizerClientResource     = localizerClientResourceMock.Object;

            IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
            IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

            //Generate random new api resource
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

            var apiResourceDtoId = await apiResourceService.AddApiResourceAsync(apiResourceDto);

            //Get new api resource
            var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResourceDtoId);

            //Assert new api resource
            apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

            //Remove api resource
            await apiResourceService.DeleteApiResourceAsync(newApiResourceDto);

            //Try get removed api resource
            var removeApiResource = await apiResourceRepository.GetApiResourceAsync(apiResourceDtoId);

            //Assert removed api resource
            removeApiResource.Should().BeNull();
        }
コード例 #11
0
        public async Task DeleteApiScopeAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceRepository = GetApiResourceRepository(context);
                var clientRepository      = GetClientRepository(context);

                var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
                var localizerApiResource     = localizerApiResourceMock.Object;

                var localizerClientResourceMock = new Mock <IClientServiceResources>();
                var localizerClientResource     = localizerClientResourceMock.Object;

                var clientService      = GetClientService(clientRepository, localizerClientResource);
                var apiResourceService = GetApiResourceService(apiResourceRepository, localizerApiResource, clientService);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api scope
                var apiScopeDtoMock = ApiResourceDtoMock.GenerateRandomApiScope(0, newApiResourceDto.Id);

                //Add new api scope
                await apiResourceService.AddApiScopeAsync(apiScopeDtoMock);

                //Get inserted api scope
                var apiScope = await context.ApiScopes.Where(x => x.Name == apiScopeDtoMock.Name && x.ApiResource.Id == newApiResourceDto.Id)
                               .SingleOrDefaultAsync();

                //Map entity to model
                var apiScopesDto = apiScope.ToModel();

                //Get new api scope
                var newApiScope = await apiResourceService.GetApiScopeAsync(apiScopesDto.ApiResourceId, apiScopesDto.ApiScopeId);

                //Assert
                newApiScope.ShouldBeEquivalentTo(apiScopesDto, o => o.Excluding(x => x.ResourceName));

                //Delete it
                await apiResourceService.DeleteApiScopeAsync(newApiScope);

                var deletedApiScope = await context.ApiScopes.Where(x => x.Name == apiScopeDtoMock.Name && x.ApiResource.Id == newApiResourceDto.Id)
                                      .SingleOrDefaultAsync();

                //Assert after deleting
                deletedApiScope.Should().BeNull();
            }
        }
コード例 #12
0
        public async Task DeleteApiSecretAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IApiResourceRepository apiResourceRepository = new ApiResourceRepository(context);
                IClientRepository      clientRepository      = new ClientRepository(context);

                var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
                var localizerApiResource     = localizerApiResourceMock.Object;

                var localizerClientResourceMock = new Mock <IClientServiceResources>();
                var localizerClientResource     = localizerClientResourceMock.Object;

                IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
                IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api secret
                var apiSecretsDtoMock = ApiResourceDtoMock.GenerateRandomApiSecret(0, newApiResourceDto.Id);

                //Add new api secret
                await apiResourceService.AddApiSecretAsync(apiSecretsDtoMock);

                //Get inserted api secret
                var apiSecret = await context.ApiSecrets.Where(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id)
                                .SingleOrDefaultAsync();

                //Map entity to model
                var apiSecretsDto = apiSecret.ToModel();

                //Get new api secret
                var newApiSecret = await apiResourceService.GetApiSecretAsync(apiSecretsDto.ApiSecretId);

                //Assert
                newApiSecret.ShouldBeEquivalentTo(apiSecretsDto, o => o.Excluding(x => x.ApiResourceName));

                //Delete it
                await apiResourceService.DeleteApiSecretAsync(newApiSecret);

                var deletedApiSecret = await context.ApiSecrets.Where(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id)
                                       .SingleOrDefaultAsync();

                //Assert after deleting
                deletedApiSecret.Should().BeNull();
            }
        }
コード例 #13
0
        public async Task UpdateApiScopeAsync()
        {
            IApiResourceRepository apiResourceRepository = new ApiResourceDapperRepository(_configuration);
            IClientRepository      clientRepository      = new ClientDapperRepository(_configuration);

            var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
            var localizerApiResource     = localizerApiResourceMock.Object;

            var localizerClientResourceMock = new Mock <IClientServiceResources>();
            var localizerClientResource     = localizerClientResourceMock.Object;

            IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
            IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

            //Generate random new api resource
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

            var apiResourceDtoId = await apiResourceService.AddApiResourceAsync(apiResourceDto);

            //Get new api resource
            var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResourceDtoId);

            //Assert new api resource
            apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

            //Generate random new api scope
            var apiScopeDtoMock = ApiResourceDtoMock.GenerateRandomApiScope(0, newApiResourceDto.Id);

            //Add new api scope
            var apiScopeId = await apiResourceService.AddApiScopeAsync(apiScopeDtoMock);

            //Get inserted api scope
            var apiScope = await apiResourceRepository.GetApiScopeAsync(apiResourceDtoId, apiScopeId);

            //Map entity to model
            var apiScopesDto = apiScope.ToModel();

            //Get new api scope
            var newApiScope = await apiResourceService.GetApiScopeAsync(apiScopesDto.ApiResourceId, apiScopesDto.ApiScopeId);

            //Assert
            newApiScope.ShouldBeEquivalentTo(apiScopesDto, o => o.Excluding(x => x.ResourceName));

            //Update api scope
            var updatedApiScope = ApiResourceDtoMock.GenerateRandomApiScope(apiScopesDto.ApiScopeId, apiScopesDto.ApiResourceId);

            await apiResourceService.UpdateApiScopeAsync(updatedApiScope);

            var updatedApiScopeDto = await apiResourceService.GetApiScopeAsync(apiScopesDto.ApiResourceId, apiScopesDto.ApiScopeId);

            //Assert updated api scope
            updatedApiScope.ShouldBeEquivalentTo(updatedApiScopeDto, o => o.Excluding(x => x.ResourceName));
        }
コード例 #14
0
        public async Task UpdateApiScopeAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api scope
                var apiScopeDtoMock = ApiResourceDtoMock.GenerateRandomApiScope(0, newApiResourceDto.Id);

                //Add new api scope
                await apiResourceService.AddApiScopeAsync(apiScopeDtoMock);

                //Get inserted api scope
                var apiScope = await context.ApiScopes.Where(x => x.Name == apiScopeDtoMock.Name && x.ApiResource.Id == newApiResourceDto.Id)
                               .SingleOrDefaultAsync();

                //Map entity to model
                var apiScopesDto = apiScope.ToModel();

                //Get new api scope
                var newApiScope = await apiResourceService.GetApiScopeAsync(apiScopesDto.ApiResourceId, apiScopesDto.ApiScopeId);

                //Assert
                newApiScope.ShouldBeEquivalentTo(apiScopesDto, o => o.Excluding(x => x.ResourceName));

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

                //Update api scope
                var updatedApiScope = ApiResourceDtoMock.GenerateRandomApiScope(apiScopesDto.ApiScopeId, apiScopesDto.ApiResourceId);

                await apiResourceService.UpdateApiScopeAsync(updatedApiScope);

                var updatedApiScopeDto = await apiResourceService.GetApiScopeAsync(apiScopesDto.ApiResourceId, apiScopesDto.ApiScopeId);

                //Assert updated api scope
                updatedApiScope.ShouldBeEquivalentTo(updatedApiScopeDto, o => o.Excluding(x => x.ResourceName));
            }
        }
コード例 #15
0
        public async Task DeleteApiSecretAsync()
        {
            IApiResourceRepository apiResourceRepository = new ApiResourceDapperRepository(_configuration);
            IClientRepository      clientRepository      = new ClientDapperRepository(_configuration);

            var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
            var localizerApiResource     = localizerApiResourceMock.Object;

            var localizerClientResourceMock = new Mock <IClientServiceResources>();
            var localizerClientResource     = localizerClientResourceMock.Object;

            IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
            IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

            //Generate random new api resource
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

            var apiResourceDtoId = await apiResourceService.AddApiResourceAsync(apiResourceDto);

            //Get new api resource
            var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResourceDtoId);

            //Assert new api resource
            apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

            //Generate random new api secret
            var apiSecretsDtoMock = ApiResourceDtoMock.GenerateRandomApiSecret(0, newApiResourceDto.Id);

            //Add new api secret
            var apiSecretId = await apiResourceService.AddApiSecretAsync(apiSecretsDtoMock);

            //Get inserted api secret
            var apiSecret = await apiResourceRepository.GetApiSecretAsync(apiSecretId);

            //Map entity to model
            var apiSecretsDto = apiSecret.ToModel();

            //Get new api secret
            var newApiSecret = await apiResourceService.GetApiSecretAsync(apiSecretsDto.ApiSecretId);

            //Assert
            newApiSecret.ShouldBeEquivalentTo(apiSecretsDto, o => o.Excluding(x => x.ApiResourceName));

            //Delete it
            await apiResourceService.DeleteApiSecretAsync(newApiSecret);

            var deletedApiSecret = await apiResourceRepository.GetApiSecretAsync(apiSecretId);

            //Assert after deleting
            deletedApiSecret.Should().BeNull();
        }
コード例 #16
0
        public void CanMapApiSecretDtoToEntity()
        {
            //Generate DTO
            var apiSecretsDto = ApiResourceDtoMock.GenerateRandomApiSecret(1, 1);

            //Try map to entity
            var apiSecret = apiSecretsDto.ToEntity();

            apiSecret.Should().NotBeNull();

            apiSecret.Should().BeEquivalentTo(apiSecretsDto);

            apiSecret.Id.Should().Be(apiSecretsDto.ApiSecretId);
        }
コード例 #17
0
        public async Task DeleteApiSecretAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.Should().BeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api secret
                var apiSecretsDtoMock = ApiResourceDtoMock.GenerateRandomApiSecret(0, newApiResourceDto.Id);

                //Add new api secret
                await apiResourceService.AddApiSecretAsync(apiSecretsDtoMock);

                //Get inserted api secret
                var apiSecret = await context.ApiSecrets.Where(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id)
                                .SingleOrDefaultAsync();

                //Map entity to model
                var apiSecretsDto = apiSecret.ToModel();

                //Get new api secret
                var newApiSecret = await apiResourceService.GetApiSecretAsync(apiSecretsDto.ApiSecretId);

                // Assert
                newApiSecret.Should().BeEquivalentTo(apiSecretsDto, o => o.Excluding(x => x.ApiResourceName).Excluding(x => x.Value));

                apiSecretsDto.Value.Should().Be(apiSecretsDtoMock.Value);

                //Delete it
                await apiResourceService.DeleteApiSecretAsync(newApiSecret);

                var deletedApiSecret = await context.ApiSecrets.Where(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id)
                                       .SingleOrDefaultAsync();

                //Assert after deleting
                deletedApiSecret.Should().BeNull();
            }
        }
        public async Task UpdateApiScope()
        {
            //Get Services
            var serviceProvider    = GetServices();
            var dbContext          = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>();
            var apiResourceService = serviceProvider.GetRequiredService <IApiResourceService>();

            // Get controller
            var controller     = PrepareConfigurationController(serviceProvider);
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);
            await apiResourceService.AddApiResourceAsync(apiResourceDto);

            var resource = await dbContext.ApiResources.SingleOrDefaultAsync(x => x.Name == apiResourceDto.Name);

            var apiScopeDto = ApiResourceDtoMock.GenerateRandomApiScope(0, resource.Id);

            await apiResourceService.AddApiScopeAsync(resource.Id, apiScopeDto);

            var apiScopeAdded = await dbContext.ApiScopes.SingleOrDefaultAsync(x => x.Name == apiScopeDto.Name);

            dbContext.Entry(apiScopeAdded).State = EntityState.Detached;

            apiScopeAdded.Should().NotBeNull();

            var updatedApiScopeDto = ApiResourceDtoMock.GenerateRandomApiScope(apiScopeAdded.Id, resource.Id);
            var result             = await controller.ApiScopes(updatedApiScopeDto);

            // Assert
            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            viewResult.ActionName.Should().Be("ApiScopes");

            var addedApiScopesDto = await apiResourceService.GetApiScopesAsync(resource.Id);

            updatedApiScopeDto.Should().BeEquivalentTo(addedApiScopesDto, opts =>
                                                       opts.Excluding(x => x.ApiResourceId)
                                                       .Excluding(x => x.ResourceName)
                                                       .Excluding(x => x.ApiScopeId)
                                                       .Excluding(x => x.UserClaims)
                                                       .Excluding(x => x.Name)
                                                       .Excluding(x => x.DisplayName)
                                                       .Excluding(x => x.Description)
                                                       .Excluding(x => x.Required)
                                                       .Excluding(x => x.Emphasize)
                                                       .Excluding(x => x.PageSize)
                                                       .Excluding(x => x.TotalCount)
                                                       .Excluding(x => x.ShowInDiscoveryDocument));
        }
コード例 #19
0
        public void CanMapApiResourceDtoToEntity()
        {
            //Generate DTO
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(1);

            //Try map to entity
            var apiResource = apiResourceDto.ToEntity();

            apiResource.Should().NotBeNull();

            apiResource.Should().BeEquivalentTo(apiResourceDto, options =>
                                                options.Excluding(o => o.UserClaims));

            //Assert collection
            apiResource.UserClaims.Select(x => x.Type).Should().BeEquivalentTo(apiResourceDto.UserClaims);
        }
コード例 #20
0
        public void CanMapApiSecretDtoToEntity()
        {
            //Generate DTO
            var apiSecretsDto = ApiResourceDtoMock.GenerateRandomApiSecret(1, 1);

            //Try map to entity
            var apiSecret = apiSecretsDto.ToEntity();

            apiSecret.Should().NotBeNull();

            apiSecret.ShouldBeEquivalentTo(apiSecretsDto, options =>
                                           options.Excluding(o => o.ApiResource)
                                           .Excluding(o => o.Created)
                                           .Excluding(o => o.Id));

            apiSecret.Id.Should().Be(apiSecretsDto.ApiSecretId);
        }
コード例 #21
0
        public async Task UpdateApiResourceAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                IApiResourceRepository apiResourceRepository = new ApiResourceRepository(context);
                IClientRepository      clientRepository      = new ClientRepository(context);

                var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
                var localizerApiResource     = localizerApiResourceMock.Object;

                var localizerClientResourceMock = new Mock <IClientServiceResources>();
                var localizerClientResource     = localizerClientResourceMock.Object;

                IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
                IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

                //Generate random new api resource
                var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResourceDto);

                //Get new api resource
                var apiResource = await context.ApiResources.Where(x => x.Name == apiResourceDto.Name).SingleOrDefaultAsync();

                var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert new api resource
                apiResourceDto.Should().BeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

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

                //Generete new api resuorce with added item id
                var updatedApiResource = ApiResourceDtoMock.GenerateRandomApiResource(apiResource.Id);

                //Update api resource
                await apiResourceService.UpdateApiResourceAsync(updatedApiResource);

                var updatedApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResource.Id);

                //Assert updated api resuorce
                updatedApiResource.Should().BeEquivalentTo(updatedApiResourceDto, options => options.Excluding(o => o.Id));
            }
        }
コード例 #22
0
        public async Task GetApiResourcePropertyAsync()
        {
            using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions))
            {
                var apiResourceService = GetApiResourceService(context);

                //Generate random new api resource
                var apiResource = ApiResourceDtoMock.GenerateRandomApiResource(0);

                await apiResourceService.AddApiResourceAsync(apiResource);

                //Get new api resource
                var resource = await context.ApiResources.Where(x => x.Name == apiResource.Name).SingleOrDefaultAsync();

                var apiResourceDto = await apiResourceService.GetApiResourceAsync(resource.Id);

                //Assert new api resource
                apiResource.Should().BeEquivalentTo(apiResourceDto, options => options.Excluding(o => o.Id));

                //Generate random new api resource property
                var apiResourceProperty = ApiResourceDtoMock.GenerateRandomApiResourceProperty(0, resource.Id);

                //Add new api resource property
                await apiResourceService.AddApiResourcePropertyAsync(apiResourceProperty);

                //Get inserted api resource property
                var property = await context.ApiResourceProperties.Where(x => x.Value == apiResourceProperty.Value && x.ApiResource.Id == resource.Id)
                               .SingleOrDefaultAsync();

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

                //Get new api resource property
                var apiResourcePropertiesDto = await apiResourceService.GetApiResourcePropertyAsync(property.Id);

                //Assert
                apiResourcePropertiesDto.Should().BeEquivalentTo(propertyDto, options =>
                                                                 options.Excluding(o => o.ApiResourcePropertyId)
                                                                 .Excluding(o => o.ApiResourceName));
            }
        }
コード例 #23
0
        public void CanMapApiScopeDtoToEntity()
        {
            //Generate DTO
            var apiScopeDto = ApiResourceDtoMock.GenerateRandomApiScope(1, 1);

            //Try map to entity
            var apiScope = apiScopeDto.ToEntity();

            apiScope.Should().NotBeNull();

            apiScope.Should().BeEquivalentTo(apiScopeDto, options =>
                                             options.Excluding(o => o.UserClaims)
                                             .Excluding(o => o.Scopes)
                                             .Excluding(o => o.UserClaimsItems)
                                             .Excluding(o => o.TotalCount)
                                             .Excluding(o => o.PageSize)
                                             .Excluding(o => o.ResourceName)
                                             .Excluding(o => o.ApiResourceId)
                                             .Excluding(o => o.ApiScopeId));

            //Assert collection
            apiScope.UserClaims.Select(x => x.Type).Should().BeEquivalentTo(apiScopeDto.UserClaims);
            apiScope.Id.Should().Be(apiScopeDto.ApiScopeId);
        }
コード例 #24
0
        public async Task UpdateApiResourceAsync()
        {
            IApiResourceRepository apiResourceRepository = new ApiResourceDapperRepository(_configuration);
            IClientRepository      clientRepository      = new ClientDapperRepository(_configuration);

            var localizerApiResourceMock = new Mock <IApiResourceServiceResources>();
            var localizerApiResource     = localizerApiResourceMock.Object;

            var localizerClientResourceMock = new Mock <IClientServiceResources>();
            var localizerClientResource     = localizerClientResourceMock.Object;

            IClientService      clientService      = new ClientService(clientRepository, localizerClientResource);
            IApiResourceService apiResourceService = new ApiResourceService(apiResourceRepository, localizerApiResource, clientService);

            //Generate random new api resource
            var apiResourceDto = ApiResourceDtoMock.GenerateRandomApiResource(0);

            var apiResourceDtoId = await apiResourceService.AddApiResourceAsync(apiResourceDto);

            //Get new api resource
            var newApiResourceDto = await apiResourceService.GetApiResourceAsync(apiResourceDtoId);

            //Assert new api resource
            apiResourceDto.ShouldBeEquivalentTo(newApiResourceDto, options => options.Excluding(o => o.Id));

            //Generete new api resuorce with added item id
            var updatedApiResource = ApiResourceDtoMock.GenerateRandomApiResource(newApiResourceDto.Id);

            //Update api resource
            await apiResourceService.UpdateApiResourceAsync(updatedApiResource);

            var updatedApiResourceDto = await apiResourceService.GetApiResourceAsync(newApiResourceDto.Id);

            //Assert updated api resuorce
            updatedApiResource.ShouldBeEquivalentTo(updatedApiResourceDto, options => options.Excluding(o => o.Id));
        }