public async Task AddRoleAsync() { using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore)) { var testUserManager = GetTestUserManager(context); var testRoleManager = GetTestRoleManager(context); IIdentityRepository identityRepository = new IdentityRepository(context, testUserManager, testRoleManager); var localizerIdentityResource = new IdentityServiceResources(); IIdentityService identityService = new IdentityService(identityRepository, localizerIdentityResource); //Generate random new role var roleDto = IdentityDtoMock.GenerateRandomRole(Guid.NewGuid()); await identityService.CreateRoleAsync(roleDto); //Get new role var role = await context.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync(); roleDto.Id = role.Id; var newRoleDto = await identityService.GetRoleAsync(roleDto); //Assert new role roleDto.Should().BeEquivalentTo(newRoleDto); } }
public async Task DeleteUserRoleAsync() { using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore)) { var testUserManager = GetTestUserManager(context); var testRoleManager = GetTestRoleManager(context); IIdentityRepository identityRepository = new IdentityRepository(context, testUserManager, testRoleManager); var localizerIdentityResource = new IdentityServiceResources(); IIdentityService identityService = new IdentityService(identityRepository, localizerIdentityResource); //Generate random new user var userDto = IdentityDtoMock.GenerateRandomUser(Guid.NewGuid()); await identityService.CreateUserAsync(userDto); //Get new user var user = await context.Users.Where(x => x.UserName == userDto.UserName).SingleOrDefaultAsync(); userDto.Id = user.Id; var newUserDto = await identityService.GetUserAsync(userDto); //Assert new user userDto.ShouldBeEquivalentTo(newUserDto); //Generate random new role var roleDto = IdentityDtoMock.GenerateRandomRole(Guid.NewGuid()); await identityService.CreateRoleAsync(roleDto); //Get new role var role = await context.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync(); roleDto.Id = role.Id; var newRoleDto = await identityService.GetRoleAsync(roleDto); //Assert new role roleDto.ShouldBeEquivalentTo(newRoleDto); var userRoleDto = IdentityDtoMock.GenerateRandomUserRole(roleDto.Id.Value, userDto.Id.Value); await identityService.CreateUserRoleAsync(userRoleDto); //Get new role var userRole = await context.UserRoles.Where(x => x.RoleId == roleDto.Id && x.UserId == userDto.Id).SingleOrDefaultAsync(); userRole.Should().NotBeNull(); await identityService.DeleteUserRoleAsync(userRoleDto); //Get deleted role var userRoleDeleted = await context.UserRoles.Where(x => x.RoleId == roleDto.Id && x.UserId == userDto.Id).SingleOrDefaultAsync(); userRoleDeleted.Should().BeNull(); } }
public async Task RemoveRoleClaimAsync() { using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore)) { var testUserManager = GetTestUserManager(context); var testRoleManager = GetTestRoleManager(context); IIdentityRepository identityRepository = new IdentityRepository(context, testUserManager, testRoleManager); var localizerIdentityResource = new IdentityServiceResources(); IIdentityService identityService = new IdentityService(identityRepository, localizerIdentityResource); //Generate random new role var roleDto = IdentityDtoMock.GenerateRandomRole(Guid.NewGuid()); await identityService.CreateRoleAsync(roleDto); //Get new role var role = await context.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync(); roleDto.Id = role.Id; var newRoleDto = await identityService.GetRoleAsync(roleDto); //Assert new role roleDto.ShouldBeEquivalentTo(newRoleDto); //Generate random new role claim var roleClaimDto = IdentityDtoMock.GenerateRandomRoleClaim(0, roleDto.Id.Value); await identityService.CreateRoleClaimsAsync(roleClaimDto); //Get new role claim var roleClaim = await context.RoleClaims.Where(x => x.ClaimType == roleClaimDto.ClaimType && x.ClaimValue == roleClaimDto.ClaimValue).SingleOrDefaultAsync(); roleClaimDto.ClaimId = roleClaim.Id; var newRoleClaimDto = await identityService.GetRoleClaimAsync(roleDto.Id.Value, roleClaimDto.ClaimId); //Assert new role roleClaimDto.ShouldBeEquivalentTo(newRoleClaimDto); await identityService.DeleteRoleClaimsAsync(roleClaimDto); var roleClaimToDelete = await context.RoleClaims.Where(x => x.ClaimType == roleClaimDto.ClaimType && x.ClaimValue == roleClaimDto.ClaimValue).SingleOrDefaultAsync(); //Assert removed role claim roleClaimToDelete.Should().BeNull(); } }
public async Task UpdateRoleAsync() { using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore)) { var testUserManager = GetTestUserManager(context); var testRoleManager = GetTestRoleManager(context); IIdentityRepository identityRepository = new IdentityRepository(context, testUserManager, testRoleManager); var localizerIdentityResource = new IdentityServiceResources(); IIdentityService identityService = new IdentityService(identityRepository, localizerIdentityResource); //Generate random new role var roleDto = IdentityDtoMock.GenerateRandomRole(Guid.NewGuid()); await identityService.CreateRoleAsync(roleDto); //Get new role var role = await context.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync(); roleDto.Id = role.Id; var newRoleDto = await identityService.GetRoleAsync(roleDto); //Assert new role roleDto.ShouldBeEquivalentTo(newRoleDto); //Detached the added item context.Entry(role).State = EntityState.Detached; //Generete new role with added item id var roleDtoForUpdate = IdentityDtoMock.GenerateRandomRole(role.Id); //Update role await identityService.UpdateRoleAsync(roleDtoForUpdate); var updatedRole = await identityService.GetRoleAsync(roleDtoForUpdate); //Assert updated role roleDtoForUpdate.ShouldBeEquivalentTo(updatedRole); } }
public async Task <IActionResult> CreateRole(CreateRoleModel model) { var validationResult = _service.ValidateCreateRole(User, model); if (!validationResult.Valid) { return(BadRequest(validationResult.Result)); } var result = await _service.CreateRoleAsync(model); if (result.Succeeded) { return(Ok(new AppResultBuilder().Success(result))); } foreach (var err in result.Errors) { ModelState.AddModelError(err.Code, err.Description); } return(BadRequest(new AppResultBuilder() .FailValidation(ModelState))); }
public async Task DeleteRoleAsync() { using (var context = new AdminDbContext(_dbContextOptions)) { var testUserManager = GetTestUserManager(context); var testRoleManager = GetTestRoleManager(context); IIdentityRepository identityRepository = new IdentityRepository(context, testUserManager, testRoleManager); var localizerIdentityResource = new IdentityServiceResources(); IIdentityService identityService = new IdentityService(identityRepository, localizerIdentityResource); //Generate random new role var roleDto = IdentityDtoMock.GenerateRandomRole(0); await identityService.CreateRoleAsync(roleDto); //Get new role var role = await context.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync(); roleDto.Id = role.Id; var newRoleDto = await identityService.GetRoleAsync(roleDto); //Assert new role roleDto.ShouldBeEquivalentTo(newRoleDto); //Remove role await identityService.DeleteRoleAsync(newRoleDto); //Try Get Removed role var removeRole = await context.Roles.Where(x => x.Id == role.Id) .SingleOrDefaultAsync(); //Assert removed role removeRole.Should().BeNull(); } }