public async Task <IActionResult> Add(ClientAddViewModel viewModel) { if (ModelState.IsValid) { var record = new Client { ClientId = viewModel.ClientId, ClientName = viewModel.ClientName, Description = viewModel.Description, Enabled = true, Created = DateTime.UtcNow, }; await _dbContext.AddAsync(record).ConfigureAwait(false); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction(nameof(Index))); } ModelState.AddModelError("", "Failed"); } return(View(viewModel)); }
public async Task OnPostAsync_AllRemoved() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; PropertiesModel properties; IActionResult post; var identityResourceId = Random.Next(); var identityResource = new IdentityResource { Id = identityResourceId, Properties = new List <IdentityResourceProperty> { new IdentityResourceProperty { Id = Random.Next() } } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(identityResource); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { properties = new PropertiesModel(context) { IdentityResource = new IdentityResource { Id = identityResourceId } }; post = await properties.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { identityResource = await context.IdentityResources .Include(x => x.Properties) .SingleOrDefaultAsync(x => x.Id.Equals(identityResourceId)) .ConfigureAwait(false); Assert.Empty(identityResource.Properties); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/Properties", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(IdentityResource.Id), key); Assert.Equal(properties.IdentityResource.Id, value); }); }
public async Task OnPostAsync_AllRemoved() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; RedirectUrisModel redirectUris; IActionResult post; var clientId = Random.Next(); var client = new Client { Id = clientId, RedirectUris = new List <ClientRedirectUri> { new ClientRedirectUri { Id = Random.Next() } } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { redirectUris = new RedirectUrisModel(context) { Client = new Client { Id = clientId } }; post = await redirectUris.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.RedirectUris) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); Assert.Empty(client.RedirectUris); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/RedirectUris", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(redirectUris.Client.Id, value); }); }
public async Task OnPostAsync_InvalidModel() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; IActionResult post; var client = new Client { Id = Random.Next() }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { var redirectUris = new RedirectUrisModel(context) { Client = new Client { Id = Random.Next() } }; post = await redirectUris.OnPostAsync().ConfigureAwait(false); } // Assert Assert.IsType <PageResult>(post); }
public async Task OnGetAsync_InvalidId() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync_InvalidId)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var role = new Role(); role.RoleClaims.Add(new RoleClaim()); role.RoleClaims.Add(new RoleClaim()); role.RoleClaims.Add(new RoleClaim()); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(role); await context.SaveChangesAsync().ConfigureAwait(false); } ClaimsModel model; IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _roleManager.Setup(x => x.Roles).Returns(context.Roles); model = new ClaimsModel(_roleManager.Object); get = await model.OnGetAsync(Guid.Empty).ConfigureAwait(false); } // Assert Assert.Null(model.Role); Assert.Null(model.Claims); Assert.IsType <NotFoundResult>(get); }
public async Task OnGetAsync_InvalidModel() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync_InvalidModel)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var user = new User(); var id = Guid.NewGuid(); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(user); await context.SaveChangesAsync().ConfigureAwait(false); } RolesModel model; IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _userManager.Setup(x => x.Users).Returns(context.Users); model = new RolesModel(_userManager.Object, _roleManager.Object); get = await model.OnGetAsync(id).ConfigureAwait(false); } // Assert Assert.Null(model.UserModel); Assert.Null(model.Roles); Assert.IsType <NotFoundResult>(get); }
public async Task <IdentityUserDto> AddProfileImage(Guid userId, string path) { ApplicationUser user = _dbContext.Users.FirstOrDefault(x => x.Id == userId); user.ProfileImagePath = path; await _dbContext.SaveChangesAsync(); return(_mapper.Map <IdentityUserDto>(user)); }
public async Task <IActionResult> Add(ApiScopeClaimAddViewModel viewmodel) { if (ModelState.IsValid) { var record = _mapper.Map <ApiScopeClaim>(viewmodel); _dbContext.ApiScopeClaims.Add(record); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction("Details", "ApiScopes", new { id = viewmodel.ScopeId })); } ModelState.AddModelError("", "Failed"); } return(View(viewmodel)); }
public async Task <IActionResult> Add(IdentityResourcePropertyAddViewModel viewmodel) { if (ModelState.IsValid) { var record = _mapper.Map <IdentityResourceProperty>(viewmodel); _dbContext.IdentityResourceProperties.Add(record); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction("Details", "IdentityResources", new { id = viewmodel.IdentityResourceId })); } ModelState.AddModelError("", "Failed"); } return(View(viewmodel)); }
public async Task OnPostAsync_AllRemoved() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var roleClaim = new RoleClaim { ClaimType = "Claim Type", ClaimValue = "Claim Value" }; var role = new Role { Name = "Role Name" }; role.RoleClaims.Add(roleClaim); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(role); await context.SaveChangesAsync().ConfigureAwait(false); } IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _roleManager.Setup(x => x.Roles).Returns(context.Roles); var model = new ClaimsModel(_roleManager.Object) { Role = new Role { Id = role.Id } }; get = await model.OnPostAsync().ConfigureAwait(false); } // Assert _roleManager.Verify( x => x.RemoveClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(roleClaim.ClaimType) && y.Value.Equals(roleClaim.ClaimValue))), Times.Once); var result = Assert.IsType <RedirectToPageResult>(get); Assert.Equal("../Details/Claims", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Role.Id), key); Assert.Equal(role.Id, value); }); }
public async Task OnPostAsync_AllRemoved() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_AllRemoved)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var userClaim = new UserClaim { ClaimType = "Claim Type", ClaimValue = "Claim Value" }; var user = new User { Email = "Email" }; user.Claims.Add(userClaim); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(user); await context.SaveChangesAsync().ConfigureAwait(false); } IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _userManager.Setup(x => x.Users).Returns(context.Users); var model = new ClaimsModel(_userManager.Object) { UserModel = new User { Id = user.Id } }; get = await model.OnPostAsync().ConfigureAwait(false); } // Assert _userManager.Verify( x => x.RemoveClaimsAsync( It.Is <User>(y => y.Id.Equals(user.Id)), It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim.ClaimType) && z.Value.Equals(userClaim.ClaimValue)) != null)), Times.Once); var result = Assert.IsType <RedirectToPageResult>(get); Assert.Equal("../Details/Claims", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(User.Id), key); Assert.Equal(user.Id, value); }); }
public async Task <IActionResult> Add(ClientPostLogoutRedirectUriAddViewModel viewmodel) { if (ModelState.IsValid) { var record = _mapper.Map <ClientPostLogoutRedirectUri>(viewmodel); record.Id = 0; _dbContext.ClientPostLogoutRedirectUris.Add(record); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction("Details", "Clients", new { id = viewmodel.ClientId })); } ModelState.AddModelError("", "Failed"); } return(View(viewmodel)); }
public async Task <IActionResult> Add(ApiResourceSecretAddViewModel viewmodel) { if (ModelState.IsValid) { var record = _mapper.Map <ApiResourceSecret>(viewmodel); record.Value = new IdentityServer4.Models.Secret(viewmodel.NewSecret.ToSha256()).Value; record.Created = DateTime.UtcNow; _dbContext.ApiResourceSecrets.Add(record); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction("Details", "ApiResources", new { id = viewmodel.ApiResourceId })); } ModelState.AddModelError("", "Failed"); } return(View(viewmodel)); }
public async Task <IActionResult> Add(ApiScopeAddViewModel viewModel) { if (ModelState.IsValid) { var record = _mapper.Map <ApiScope>(viewModel); record.Enabled = true; await _dbContext.AddAsync(record).ConfigureAwait(false); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction(nameof(Index))); } ModelState.AddModelError("", "Failed"); } return(View(viewModel)); }
public async Task <IActionResult> Create(UserClaimViewModel vm) { if (vm == null) { return(BadRequest()); } if (ModelState.IsValid) { var user = await _dbContext.Users.FindAsync(vm.UserId); if (user == null) { return(BadRequest()); } var obj = new IdentityUserClaim <string> { UserId = vm.UserId, ClaimType = vm.ClaimType, ClaimValue = vm.ClaimValue }; _dbContext.UserClaims.Add(obj); try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"User claim Id {obj.Id} created by {User?.Identity?.Name}."); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } } return(RedirectToAction("Edit", "Users", new { id = vm.UserId })); }
public async Task <IActionResult> Add(ApiResourceAddViewModel viewModel) { if (ModelState.IsValid) { var record = _mapper.Map <ApiResource>(viewModel); record.Enabled = true; record.ShowInDiscoveryDocument = true; record.Created = DateTime.UtcNow; await _dbContext.AddAsync(record).ConfigureAwait(false); var result = await _dbContext.SaveChangesAsync().ConfigureAwait(false); if (result > 0) { return(RedirectToAction(nameof(Index))); } ModelState.AddModelError("", "Failed"); } return(View(viewModel)); }
public async Task OnGetAsync() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var user = new User(); user.UserRoles.Add(new UserRole { Role = new Role() }); user.UserRoles.Add(new UserRole { Role = new Role() }); user.UserRoles.Add(new UserRole { Role = new Role() }); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(user); await context.SaveChangesAsync().ConfigureAwait(false); var rolesList = await context.Roles.ToListAsync().ConfigureAwait(false); _roleManager.Setup(x => x.Roles).Returns(rolesList.AsQueryable()); } RolesModel model; IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _userManager.Setup(x => x.Users).Returns(context.Users); model = new RolesModel(_userManager.Object, _roleManager.Object); get = await model.OnGetAsync(user.Id).ConfigureAwait(false); } // Assert Assert.NotNull(model.UserModel); Assert.Equal(user.Id, model.UserModel.Id); var roles = Assert.IsAssignableFrom <IEnumerable <Role> >(model.Roles); Assert.Equal(user.UserRoles.Count, roles.Count()); Assert.IsType <PageResult>(get); }
public async Task OnGetAsync() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var client = new Client { Id = Random.Next(), RedirectUris = new List <ClientRedirectUri> { new ClientRedirectUri(), new ClientRedirectUri(), new ClientRedirectUri() } }; RedirectUrisModel model; IActionResult get; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { model = new RedirectUrisModel(context); get = await model.OnGetAsync(client.Id).ConfigureAwait(false); } // Assert Assert.NotNull(model.Client); Assert.Equal(client.Id, model.Client.Id); var redirectUris = Assert.IsAssignableFrom <IEnumerable <ClientRedirectUri> >(model.RedirectUris); Assert.Equal(client.RedirectUris.Count, redirectUris.Count()); Assert.IsType <PageResult>(get); }
public async Task OnGetAsync() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var identityResource = new IdentityResource { Id = Random.Next(), Properties = new List <IdentityResourceProperty> { new IdentityResourceProperty(), new IdentityResourceProperty(), new IdentityResourceProperty() } }; PropertiesModel model; IActionResult get; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(identityResource); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { model = new PropertiesModel(context); get = await model.OnGetAsync(identityResource.Id).ConfigureAwait(false); } // Assert Assert.NotNull(model.IdentityResource); Assert.Equal(identityResource.Id, model.IdentityResource.Id); var properties = Assert.IsAssignableFrom <IEnumerable <IdentityResourceProperty> >(model.Properties); Assert.Equal(identityResource.Properties.Count, properties.Count()); Assert.IsType <PageResult>(get); }
public async Task OnGetAsync() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnGetAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var role = new Role { Id = Guid.NewGuid() }; role.RoleClaims.Add(new RoleClaim()); role.RoleClaims.Add(new RoleClaim()); role.RoleClaims.Add(new RoleClaim()); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(role); await context.SaveChangesAsync().ConfigureAwait(false); } ClaimsModel model; IActionResult get; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _roleManager.Setup(x => x.Roles).Returns(context.Roles); model = new ClaimsModel(_roleManager.Object); get = await model.OnGetAsync(role.Id).ConfigureAwait(false); } // Assert Assert.NotNull(model.Role); Assert.Equal(role.Id, model.Role.Id); var claims = Assert.IsAssignableFrom <IEnumerable <RoleClaim> >(model.Claims); Assert.Equal(role.RoleClaims.Count, claims.Count()); Assert.IsType <PageResult>(get); }
public async Task OnPostAsync_InvalidModel() { // Arrange var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync_InvalidModel)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var role = new Role { Id = Guid.NewGuid() }; var id = Guid.NewGuid(); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(role); await context.SaveChangesAsync().ConfigureAwait(false); } IActionResult post; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _roleManager.Setup(x => x.Roles).Returns(context.Roles); var model = new ClaimsModel(_roleManager.Object) { Role = new Role { Id = id } }; post = await model.OnPostAsync().ConfigureAwait(false); } // Assert _roleManager.Verify(x => x.UpdateAsync(It.IsAny <Role>()), Times.Never); Assert.IsType <PageResult>(post); }
public async Task <IActionResult> DeleteRole(UserRoleViewModel vm) { var userRole = await _dbContext.UserRoles.SingleOrDefaultAsync(x => x.UserId == vm.UserId && x.RoleId == vm.RoleId); if (userRole == null) { return(NotFound()); } _dbContext.UserRoles.Remove(userRole); try { await _dbContext.SaveChangesAsync(); _logger.LogInformation($"Role Id {vm.RoleId} removed from User Id {vm.UserId} by {User?.Identity?.Name}."); } catch (DbException ex) { _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message); throw; } return(RedirectToAction("Edit", "Users", new { id = vm.UserId })); }
public async Task OnPostAsync() { // Arrange const string scope1OriginalScope = "Original Scope"; const string scope1EditedScope = "Edited Scope"; const string newScopeScope = "New Scope"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; ScopesModel scopes; IActionResult post; var scope1 = new ClientScope { Id = Random.Next(), Scope = scope1OriginalScope }; var scope2 = new ClientScope { Id = Random.Next() }; var clientId = Random.Next(); var client = new Client { Id = clientId, AllowedScopes = new List <ClientScope> { scope1, scope2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { scopes = new ScopesModel(context) { Client = new Client { Id = clientId, AllowedScopes = new List <ClientScope> { new ClientScope { Id = scope1.Id, Scope = scope1EditedScope }, new ClientScope { Scope = newScopeScope } } } }; post = await scopes.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.AllowedScopes) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); scope1 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope1.Id)); scope2 = client.AllowedScopes.SingleOrDefault(x => x.Id.Equals(scope2.Id)); var newScope = client.AllowedScopes.SingleOrDefault(x => x.Scope.Equals(newScopeScope)); Assert.NotNull(scope1); Assert.Equal(scope1EditedScope, scope1.Scope); Assert.Null(scope2); Assert.NotNull(newScope); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/Scopes", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(scopes.Client.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string idPRestriction1OriginalProvider = "Original Provider"; const string idPRestriction1EditedProvider = "Edited Provider"; const string newIdPRestrictionProvider = "New Provider"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; IdPRestrictionsModel idPRestrictions; IActionResult post; var idPRestriction1 = new ClientIdPRestriction { Id = Random.Next(), Provider = idPRestriction1OriginalProvider }; var idPRestriction2 = new ClientIdPRestriction { Id = Random.Next() }; var clientId = Random.Next(); var client = new Client { Id = clientId, IdentityProviderRestrictions = new List <ClientIdPRestriction> { idPRestriction1, idPRestriction2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { idPRestrictions = new IdPRestrictionsModel(context) { Client = new Client { Id = clientId, IdentityProviderRestrictions = new List <ClientIdPRestriction> { new ClientIdPRestriction { Id = idPRestriction1.Id, Provider = idPRestriction1EditedProvider }, new ClientIdPRestriction { Provider = newIdPRestrictionProvider } } } }; post = await idPRestrictions.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.IdentityProviderRestrictions) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); idPRestriction1 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction1.Id)); idPRestriction2 = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Id.Equals(idPRestriction2.Id)); var newIdPRestriction = client.IdentityProviderRestrictions.SingleOrDefault(x => x.Provider.Equals(newIdPRestrictionProvider)); Assert.NotNull(idPRestriction1); Assert.Equal(idPRestriction1EditedProvider, idPRestriction1.Provider); Assert.Null(idPRestriction2); Assert.NotNull(newIdPRestriction); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/IdPRestrictions", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(idPRestrictions.Client.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string property1OriginalValue = "Original Value"; const string property1EditedValue = "Edited Value"; const string newPropertyValue = "New Value"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; PropertiesModel properties; IActionResult post; var property1 = new IdentityResourceProperty { Id = Random.Next(), Value = property1OriginalValue }; var property2 = new IdentityResourceProperty { Id = Random.Next() }; var identityResourceId = Random.Next(); var identityResource = new IdentityResource { Id = identityResourceId, Properties = new List <IdentityResourceProperty> { property1, property2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(identityResource); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { properties = new PropertiesModel(context) { IdentityResource = new IdentityResource { Id = identityResourceId, Properties = new List <IdentityResourceProperty> { new IdentityResourceProperty { Id = property1.Id, Value = property1EditedValue }, new IdentityResourceProperty { Value = newPropertyValue } } } }; post = await properties.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { identityResource = await context.IdentityResources .Include(x => x.Properties) .SingleOrDefaultAsync(x => x.Id.Equals(identityResourceId)) .ConfigureAwait(false); property1 = identityResource.Properties.SingleOrDefault(x => x.Id.Equals(property1.Id)); property2 = identityResource.Properties.SingleOrDefault(x => x.Id.Equals(property2.Id)); var newProperty = identityResource.Properties.SingleOrDefault(x => x.Value.Equals(newPropertyValue)); Assert.NotNull(property1); Assert.Equal(property1EditedValue, property1.Value); Assert.Null(property2); Assert.NotNull(newProperty); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/Properties", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(IdentityResource.Id), key); Assert.Equal(properties.IdentityResource.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string originalRoleClaim2Type = "Claim 2 Type"; const string newRoleClaim2Type = "New Claim Type"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var roleClaim1 = new RoleClaim { ClaimType = "Claim 1 Type", ClaimValue = "Claim 1 Value" }; var roleClaim2 = new RoleClaim { ClaimType = originalRoleClaim2Type, ClaimValue = "Claim 2 Value" }; var roleClaim3 = new RoleClaim { ClaimType = "Claim 3 Type", ClaimValue = "Claim 3 Value" }; var roleClaim4 = new RoleClaim { ClaimType = "Claim 4 Type", ClaimValue = "Claim 4 Value" }; var role = new Role { Name = "Role Name" }; role.RoleClaims.Add(roleClaim1); role.RoleClaims.Add(roleClaim2); role.RoleClaims.Add(roleClaim3); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(role); await context.SaveChangesAsync().ConfigureAwait(false); } IActionResult post; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _roleManager.Setup(x => x.Roles).Returns(context.Roles); var model = new ClaimsModel(_roleManager.Object) { Role = role }; model.Role.RoleClaims.Single(x => x.ClaimType.Equals(roleClaim2.ClaimType)).ClaimType = newRoleClaim2Type; model.Role.RoleClaims.Remove(roleClaim3); model.Role.RoleClaims.Add(roleClaim4); post = await model.OnPostAsync().ConfigureAwait(false); } // Assert _roleManager.Verify(x => x.RemoveClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(roleClaim1.ClaimType) && y.Value.Equals(roleClaim1.ClaimValue))), Times.Never); _roleManager.Verify(x => x.AddClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(roleClaim1.ClaimType) && y.Value.Equals(roleClaim1.ClaimValue))), Times.Never); _roleManager.Verify(x => x.RemoveClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(originalRoleClaim2Type) && y.Value.Equals(roleClaim2.ClaimValue))), Times.Once); _roleManager.Verify(x => x.AddClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(newRoleClaim2Type) && y.Value.Equals(roleClaim2.ClaimValue))), Times.Once); _roleManager.Verify( x => x.AddClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(roleClaim4.ClaimType) && y.Value.Equals(roleClaim4.ClaimValue))), Times.Once); _roleManager.Verify( x => x.RemoveClaimAsync( It.Is <Role>(y => y.Id.Equals(role.Id)), It.Is <Claim>(y => y.Type.Equals(roleClaim3.ClaimType) && y.Value.Equals(roleClaim3.ClaimValue))), Times.Once); var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/Claims", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Role.Id), key); Assert.Equal(role.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string originalUserClaim2Type = "Claim 2 Type"; const string newUserClaim2Type = "New Claim Type"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; var userClaim1 = new UserClaim { ClaimType = "Claim 1 Type", ClaimValue = "Claim 1 Value" }; var userClaim2 = new UserClaim { ClaimType = originalUserClaim2Type, ClaimValue = "Claim 2 Value" }; var userClaim3 = new UserClaim { ClaimType = "Claim 3 Type", ClaimValue = "Claim 3 Value" }; var userClaim4 = new UserClaim { ClaimType = "Claim 4 Type", ClaimValue = "Claim 4 Value" }; var user = new User { Email = "Email" }; user.Claims.Add(userClaim1); user.Claims.Add(userClaim2); user.Claims.Add(userClaim3); using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(user); await context.SaveChangesAsync().ConfigureAwait(false); } IActionResult post; // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { _userManager.Setup(x => x.Users).Returns(context.Users); var model = new ClaimsModel(_userManager.Object) { UserModel = user }; model.UserModel.Claims.Single(x => x.ClaimType.Equals(userClaim2.ClaimType)).ClaimType = newUserClaim2Type; model.UserModel.Claims.Remove(userClaim3); model.UserModel.Claims.Add(userClaim4); post = await model.OnPostAsync().ConfigureAwait(false); } // Assert _userManager.Verify(x => x.ReplaceClaimAsync( It.Is <User>(y => y.Id.Equals(user.Id)), It.Is <Claim>(y => y.Type.Equals(userClaim1.ClaimType) && y.Value.Equals(userClaim1.ClaimValue)), It.IsAny <Claim>()), Times.Never); _userManager.Verify(x => x.ReplaceClaimAsync( It.Is <User>(y => y.Id.Equals(user.Id)), It.Is <Claim>(y => y.Type.Equals(originalUserClaim2Type) && y.Value.Equals(userClaim2.ClaimValue)), It.Is <Claim>(y => y.Type.Equals(newUserClaim2Type) && y.Value.Equals(userClaim2.ClaimValue))), Times.Once); _userManager.Verify( x => x.AddClaimsAsync( It.Is <User>(y => y.Id.Equals(user.Id)), It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim4.ClaimType) && z.Value.Equals(userClaim4.ClaimValue)) != null)), Times.Once); _userManager.Verify( x => x.RemoveClaimsAsync( It.Is <User>(y => y.Id.Equals(user.Id)), It.Is <IEnumerable <Claim> >(y => y.SingleOrDefault(z => z.Type.Equals(userClaim3.ClaimType) && z.Value.Equals(userClaim3.ClaimValue)) != null)), Times.Once); var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/Claims", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(User.Id), key); Assert.Equal(user.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string claim1OriginalType = "Original Type"; const string claim1EditedType = "Edited Type"; const string newClaimType = "New Type"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; ClaimTypesModel claims; IActionResult post; var claim1 = new ApiResourceClaim { Id = Random.Next(), Type = claim1OriginalType }; var claim2 = new ApiResourceClaim { Id = Random.Next() }; var apiResourceId = Random.Next(); var apiResource = new ApiResource { Id = apiResourceId, UserClaims = new List <ApiResourceClaim> { claim1, claim2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(apiResource); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { claims = new ClaimTypesModel(context) { ApiResource = new ApiResource { Id = apiResourceId, UserClaims = new List <ApiResourceClaim> { new ApiResourceClaim { Id = claim1.Id, Type = claim1EditedType }, new ApiResourceClaim { Type = newClaimType } } } }; post = await claims.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { apiResource = await context.ApiResources .Include(x => x.UserClaims) .SingleOrDefaultAsync(x => x.Id.Equals(apiResourceId)) .ConfigureAwait(false); claim1 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim1.Id)); claim2 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim2.Id)); var newClaim = apiResource.UserClaims.SingleOrDefault(x => x.Type.Equals(newClaimType)); Assert.NotNull(claim1); Assert.Equal(claim1EditedType, claim1.Type); Assert.Null(claim2); Assert.NotNull(newClaim); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/ClaimTypes", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(ApiResource.Id), key); Assert.Equal(claims.ApiResource.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string corsOrigin1OriginalOrigin = "Original Origin"; const string corsOrigin1EditedOrigin = "Edited Origin"; const string newCorsOriginOrigin = "New Origin"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; CorsOriginsModel corsOrigins; IActionResult post; var corsOrigin1 = new ClientCorsOrigin { Id = Random.Next(), Origin = corsOrigin1OriginalOrigin }; var corsOrigin2 = new ClientCorsOrigin { Id = Random.Next() }; var clientId = Random.Next(); var client = new Client { Id = clientId, AllowedCorsOrigins = new List <ClientCorsOrigin> { corsOrigin1, corsOrigin2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { corsOrigins = new CorsOriginsModel(context) { Client = new Client { Id = clientId, AllowedCorsOrigins = new List <ClientCorsOrigin> { new ClientCorsOrigin { Id = corsOrigin1.Id, Origin = corsOrigin1EditedOrigin }, new ClientCorsOrigin { Origin = newCorsOriginOrigin } } } }; post = await corsOrigins.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.AllowedCorsOrigins) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); corsOrigin1 = client.AllowedCorsOrigins.SingleOrDefault(x => x.Id.Equals(corsOrigin1.Id)); corsOrigin2 = client.AllowedCorsOrigins.SingleOrDefault(x => x.Id.Equals(corsOrigin2.Id)); var newCorsOrigin = client.AllowedCorsOrigins.SingleOrDefault(x => x.Origin.Equals(newCorsOriginOrigin)); Assert.NotNull(corsOrigin1); Assert.Equal(corsOrigin1EditedOrigin, corsOrigin1.Origin); Assert.Null(corsOrigin2); Assert.NotNull(newCorsOrigin); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/CorsOrigins", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(corsOrigins.Client.Id, value); }); }
public async Task OnPostAsync() { // Arrange const string redirectUri1OriginalRedirectUri = "Original RedirectUri"; const string redirectUri1EditedRedirectUri = "Edited RedirectUri"; const string newRedirectUriRedirectUri = "New RedirectUri"; var databaseName = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}"; var options = new DbContextOptionsBuilder <IdentityServerDbContext>() .UseInMemoryDatabase(databaseName) .Options; RedirectUrisModel redirectUris; IActionResult post; var redirectUri1 = new ClientRedirectUri { Id = Random.Next(), RedirectUri = redirectUri1OriginalRedirectUri }; var redirectUri2 = new ClientRedirectUri { Id = Random.Next() }; var clientId = Random.Next(); var client = new Client { Id = clientId, RedirectUris = new List <ClientRedirectUri> { redirectUri1, redirectUri2 } }; using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { context.Add(client); await context.SaveChangesAsync().ConfigureAwait(false); } // Act using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { redirectUris = new RedirectUrisModel(context) { Client = new Client { Id = clientId, RedirectUris = new List <ClientRedirectUri> { new ClientRedirectUri { Id = redirectUri1.Id, RedirectUri = redirectUri1EditedRedirectUri }, new ClientRedirectUri { RedirectUri = newRedirectUriRedirectUri } } } }; post = await redirectUris.OnPostAsync().ConfigureAwait(false); } // Assert using (var context = new IdentityServerDbContext(options, _configurationStoreOptions.Object, _operationalStoreOptions.Object)) { client = await context.Clients .Include(x => x.RedirectUris) .SingleOrDefaultAsync(x => x.Id.Equals(clientId)) .ConfigureAwait(false); redirectUri1 = client.RedirectUris.SingleOrDefault(x => x.Id.Equals(redirectUri1.Id)); redirectUri2 = client.RedirectUris.SingleOrDefault(x => x.Id.Equals(redirectUri2.Id)); var newRedirectUri = client.RedirectUris.SingleOrDefault(x => x.RedirectUri.Equals(newRedirectUriRedirectUri)); Assert.NotNull(redirectUri1); Assert.Equal(redirectUri1EditedRedirectUri, redirectUri1.RedirectUri); Assert.Null(redirectUri2); Assert.NotNull(newRedirectUri); } var result = Assert.IsType <RedirectToPageResult>(post); Assert.Equal("../Details/RedirectUris", result.PageName); Assert.Collection(result.RouteValues, routeValue => { var(key, value) = routeValue; Assert.Equal(nameof(Client.Id), key); Assert.Equal(redirectUris.Client.Id, value); }); }