public async Task AddApiSecret()
        {
            //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 apiSecretDto = ApiResourceDtoMock.GenerateRandomApiResourceSecret(0, resource.Id);

            var result = await controller.ApiSecrets(apiSecretDto);

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

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

            var apiSecret = await dbContext.ApiResourceSecrets.SingleOrDefaultAsync(x => x.Value == apiSecretDto.Value);

            var addedApiScope = await apiResourceService.GetApiSecretAsync(apiSecret.Id);

            apiSecretDto.Should().BeEquivalentTo(addedApiScope, opts =>
                                                 opts.Excluding(x => x.ApiResourceId)
                                                 .Excluding(x => x.ApiSecretId)
                                                 .Excluding(x => x.ApiResourceName)
                                                 .Excluding(x => x.ApiResourceSecrets));
        }
        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.SingleOrDefaultAsync(x => x.Name == apiResourceDto.Name);

            const int generateApiSecrets = 5;

            for (var i = 0; i < generateApiSecrets; i++)
            {
                var apiSecretDto = ApiResourceDtoMock.GenerateRandomApiResourceSecret(0, resource.Id);
                await apiResourceService.AddApiResourceSecretAsync(apiSecretDto);
            }

            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 <ApiResourceSecretsDto>(viewResult.ViewData.Model);

            viewModel.ApiResourceSecrets.Count.Should().Be(generateApiSecrets);
        }
        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.GenerateRandomApiResourceSecret(0, newApiResourceDto.Id);

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

                //Get inserted api secret
                var apiResourceSecret = await context.ApiResourceSecrets.FirstOrDefaultAsync(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id);

                //Map entity to model
                var apiResourceSecretDto = apiResourceSecret.ToModel();

                //Get new api secret
                var newApiResourceSecret = await apiResourceService.GetApiSecretAsync(apiResourceSecretDto.ApiResourceId);

                //Assert
                newApiResourceSecret.Should().BeEquivalentTo(apiResourceSecretDto, o =>
                                                             o.Excluding(x => x.Description)
                                                             .Excluding(o => o.ApiResourceSecrets));

                //Delete it
                await apiResourceService.DeleteApiResourceSecretAsync(newApiResourceSecret);

                var deletedApiSecret = await context.ApiResourceSecrets.FirstOrDefaultAsync(x => x.Value == apiSecretsDtoMock.Value && x.ApiResource.Id == newApiResourceDto.Id);

                //Assert after deleting
                deletedApiSecret.Should().BeNull();
            }
        }
        public void CanMapApiSecretDtoToEntity()
        {
            //Generate DTO
            var apiSecretsDto = ApiResourceDtoMock.GenerateRandomApiResourceSecret(1, 1);

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

            apiSecret.Should().NotBeNull();

            apiSecret.Should().BeEquivalentTo(apiSecretsDto, options =>
                                              options.Excluding(o => o.Created)
                                              .Excluding(o => o.ApiSecretId)
                                              .Excluding(o => o.ApiResourceName)
                                              .Excluding(o => o.TypeList)
                                              .Excluding(o => o.HashType)
                                              .Excluding(o => o.HashTypes)
                                              .Excluding(o => o.ApiResourceSecrets)
                                              .Excluding(o => o.TotalCount)
                                              .Excluding(o => o.PageSize));

            apiSecret.Id.Should().Be(apiSecretsDto.ApiSecretId);
        }
        public async Task DeleteApiSecret()
        {
            //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 apiSecretDto = ApiResourceDtoMock.GenerateRandomApiResourceSecret(0, resource.Id);
            await apiResourceService.AddApiResourceSecretAsync(apiSecretDto);

            var apiSecretId = await dbContext.ApiResourceSecrets
                              .Where(x => x.Value == apiSecretDto.Value)
                              .Select(x => x.Id)
                              .SingleOrDefaultAsync();

            apiSecretId.Should().NotBe(0);

            apiSecretDto.ApiSecretId = apiSecretId;
            var result = await controller.ApiSecretDelete(apiSecretDto);

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

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

            var apiSecret = await dbContext.ApiResourceSecrets.SingleOrDefaultAsync(x => x.Id == apiSecretDto.ApiSecretId);

            apiSecret.Should().BeNull();
        }