public async Task AddApiScopeAsync() { using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions)) { var apiScopeService = GetApiScopeService(context); //Generate random new api scope var apiScopeDtoMock = ApiScopeDtoMock.GenerateRandomApiScope(0); //Add new api scope await apiScopeService.AddApiScopeAsync(apiScopeDtoMock); //Get inserted api scope var apiScope = await context.ApiScopes.Where(x => x.Name == apiScopeDtoMock.Name) .SingleOrDefaultAsync(); //Map entity to model var apiScopesDto = apiScope.ToModel(); //Get new api scope var newApiScope = await apiScopeService.GetApiScopeAsync(apiScopesDto.Id); //Assert newApiScope.ShouldBeEquivalentTo(apiScopesDto); } }
public async Task DeleteApiScope() { //Get Services var serviceProvider = GetServices(); var dbContext = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>(); var apiScopeService = serviceProvider.GetRequiredService <IApiScopeService>(); // Get controller var controller = PrepareConfigurationController(serviceProvider); var apiScopeDto = ApiScopeDtoMock.GenerateRandomApiScope(0); await apiScopeService.AddApiScopeAsync(apiScopeDto); var apiScopeId = await dbContext.ApiScopes.Where(x => x.Name == apiScopeDto.Name).Select(x => x.Id).SingleOrDefaultAsync(); apiScopeId.Should().NotBe(0); apiScopeDto.Id = apiScopeId; var result = await controller.ApiScopeDelete(apiScopeDto); // Assert var viewResult = Assert.IsType <RedirectToActionResult>(result); viewResult.ActionName.Should().Be("ApiScopes"); var apiScope = await dbContext.ApiScopes.Where(x => x.Id == apiScopeDto.Id).SingleOrDefaultAsync(); apiScope.Should().BeNull(); }
public async Task UpdateApiScope() { //Get Services var serviceProvider = GetServices(); var dbContext = serviceProvider.GetRequiredService <IdentityServerConfigurationDbContext>(); var apiScopeService = serviceProvider.GetRequiredService <IApiScopeService>(); // Get controller var controller = PrepareConfigurationController(serviceProvider); var apiScopeDto = ApiScopeDtoMock.GenerateRandomApiScope(0); await apiScopeService.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 = ApiScopeDtoMock.GenerateRandomApiScope(apiScopeAdded.Id); var result = await controller.ApiScope(updatedApiScopeDto); // Assert var viewResult = Assert.IsType <RedirectToActionResult>(result); viewResult.ActionName.Should().Be("ApiScope"); var apiScope = await dbContext.ApiScopes.Where(x => x.Id == apiScopeAdded.Id).SingleOrDefaultAsync(); var addedApiScope = await apiScopeService.GetApiScopeAsync(apiScope.Id); updatedApiScopeDto.Should().BeEquivalentTo(addedApiScope, opts => opts.Excluding(x => x.Id)); }
public async Task GetApiScopes() { //Get Services var serviceProvider = GetServices(); var apiScopeService = serviceProvider.GetRequiredService <IApiScopeService>(); // Get controller var controller = PrepareConfigurationController(serviceProvider); const int generateScopes = 5; // Add Api Scopes for (var i = 0; i < generateScopes; i++) { var apiScopeDto = ApiScopeDtoMock.GenerateRandomApiScope(0); await apiScopeService.AddApiScopeAsync(apiScopeDto); } var result = await controller.ApiScopes(string.Empty, 1); // 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 = ApiScopeDtoMock.GenerateRandomApiScope(1); //Try map to entity var apiScope = apiScopeDto.ToEntity(); apiScope.Should().NotBeNull(); apiScope.ShouldBeEquivalentTo(apiScopeDto, options => options.Excluding(o => o.UserClaims) .Excluding(o => o.Properties) .Excluding(o => o.Id)); //Assert collection apiScope.UserClaims.Select(x => x.Type).ShouldBeEquivalentTo(apiScopeDto.UserClaims); apiScope.Id.Should().Be(apiScopeDto.Id); }
public async Task UpdateApiScopeAsync() { using (var context = new IdentityServerConfigurationDbContext(_dbContextOptions, _storeOptions)) { var apiScopeService = GetApiScopeService(context); //Generate random new api scope var apiScopeDtoMock = ApiScopeDtoMock.GenerateRandomApiScope(0); //Add new api scope await apiScopeService.AddApiScopeAsync(apiScopeDtoMock); //Get inserted api scope var apiScope = await context.ApiScopes.Where(x => x.Name == apiScopeDtoMock.Name) .SingleOrDefaultAsync(); //Map entity to model var apiScopesDto = apiScope.ToModel(); //Get new api scope var newApiScope = await apiScopeService.GetApiScopeAsync(apiScopesDto.Id); //Assert newApiScope.Should().BeEquivalentTo(apiScopesDto); //Detached the added item context.Entry(apiScope).State = EntityState.Detached; //Update api scope var updatedApiScope = ApiScopeDtoMock.GenerateRandomApiScope(apiScopesDto.Id); await apiScopeService.UpdateApiScopeAsync(updatedApiScope); var updatedApiScopeDto = await apiScopeService.GetApiScopeAsync(apiScopesDto.Id); //Assert updated api scope updatedApiScope.Should().BeEquivalentTo(updatedApiScopeDto); } }