public async Task UpdateApiScope() { //Get Services var serviceProvider = GetServices(); var dbContext = serviceProvider.GetRequiredService <AdminDbContext>(); 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); await apiResourceService.AddApiScopeAsync(apiScopeDto); var apiScopeAdded = await dbContext.ApiScopes.Where(x => x.Name == apiScopeDto.Name).SingleOrDefaultAsync(); 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 apiScope = await dbContext.ApiScopes.Where(x => x.Id == apiScopeAdded.Id).SingleOrDefaultAsync(); var addedApiScope = await apiResourceService.GetApiScopeAsync(resource.Id, apiScope.Id); updatedApiScopeDto.Should().BeEquivalentTo(addedApiScope, opts => opts.Excluding(x => x.ApiResourceId) .Excluding(x => x.ResourceName) .Excluding(x => x.ApiScopeId)); }
public async Task GetApiScopeAsync() { 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)); } }
public async Task GetApiScopes() { //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 generateScopes = 5; // Add Api Scopes for (var i = 0; i < generateScopes; i++) { var apiScopeDto = ApiResourceDtoMock.GenerateRandomApiScope(0, resource.Id); await apiResourceService.AddApiScopeAsync(apiScopeDto); } var result = await controller.ApiScopes(resource.Id, 1, null); // Assert var viewResult = Assert.IsType <ViewResult>(result); viewResult.ViewName.Should().BeNullOrEmpty(); viewResult.ViewData.Should().NotBeNull(); var viewModel = Assert.IsType <ApiScopesDto>(viewResult.ViewData.Model); viewModel.Scopes.Count.Should().Be(generateScopes); }
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); }
public async Task UpdateApiScopeAsync() { 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 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)); } }
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.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 apiScopeDto = apiScope.ToModel(); //Get new api scope var newApiScope = await apiResourceService.GetApiScopeAsync(newApiResourceDto.Id, apiScopeDto.ApiScopeId); //Assert newApiScope.Should().BeEquivalentTo(apiScopeDto, 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) ); //Detached the added item context.Entry(apiScope).State = EntityState.Detached; //Update api scope var updatedApiScope = ApiResourceDtoMock.GenerateRandomApiScope(newApiResourceDto.Id, apiScopeDto.ApiScopeId); await apiResourceService.UpdateApiScopeAsync(updatedApiScope); var updatedApiScopeDto = await apiResourceService.GetApiScopeAsync(newApiResourceDto.Id, apiScopeDto.ApiScopeId); //Assert updated api scope updatedApiScope.Should().BeEquivalentTo(updatedApiScopeDto, 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) ); } }