コード例 #1
0
        public async Task DeleteUserRoleAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <int> .GenerateRandomUser(0);

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                //Generate random new role
                var roleDto = IdentityDtoMock <int> .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.Id.ToString());

                //Assert new role
                roleDto.ShouldBeEquivalentTo(newRoleDto);

                var userRoleDto = IdentityDtoMock <int> .GenerateRandomUserRole <RoleDto <int> >(roleDto.Id, userDto.Id);

                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();
            }
        }
コード例 #2
0
        public async Task AddUserAsync()
        {
            using (var context = new AdminDbContext(_dbContextOptions, _storeOptions, _operationalStore))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);
                var mapper          = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <int> .GenerateRandomUser(0);

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);
            }
        }
コード例 #3
0
        public async Task DeleteUser()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            await identityService.CreateUserAsync(userDto);

            var userId = await dbContext.Users.Where(x => x.UserName == userDto.UserName).Select(x => x.Id).SingleOrDefaultAsync();

            userDto.Id = userId;

            var result = await controller.UserDelete(userDto);

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

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

            var user = await dbContext.Users.Where(x => x.Id == userDto.Id).SingleOrDefaultAsync();

            user.Should().BeNull();
        }
コード例 #4
0
        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.Empty);

                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);
            }
        }
コード例 #5
0
        public async Task AddUser()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            var result = await controller.UserProfile(userDto);

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

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

            var user = await dbContext.Users.Where(x => x.UserName == userDto.UserName).SingleOrDefaultAsync();

            userDto.Id = user.Id;

            var addedUser = await identityService.GetUserAsync(userDto.Id);

            userDto.Should().BeEquivalentTo(addedUser, opts => opts.Excluding(x => x.Id));
        }
コード例 #6
0
        public async Task AddRoleClaim()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var roleDto    = IdentityDtoMock <string> .GenerateRandomRole();

            await identityService.CreateRoleAsync(roleDto);

            var role = await dbContext.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync();

            roleDto.Id = role.Id;

            var roleClaimsDto = IdentityDtoMock <string> .GenerateRandomRoleClaim(0, role.Id);

            var result = await controller.RoleClaims(roleClaimsDto);

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

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

            var roleClaim = await dbContext.RoleClaims.Where(x => x.ClaimValue == roleClaimsDto.ClaimValue).SingleOrDefaultAsync();

            var addedRoleClaim = await identityService.GetRoleClaimAsync(role.Id.ToString(), roleClaim.Id);

            roleClaimsDto.Should().BeEquivalentTo(addedRoleClaim, opts => opts.Excluding(x => x.ClaimId)
                                                  .Excluding(x => x.RoleName));
        }
コード例 #7
0
        public async Task UpdateRole()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var roleDto    = IdentityDtoMock <string> .GenerateRandomRole();

            await identityService.CreateRoleAsync(roleDto);

            var roleId = await dbContext.Roles.Where(x => x.Name == roleDto.Name).Select(x => x.Id).SingleOrDefaultAsync();

            var updatedRoleDto = IdentityDtoMock <string> .GenerateRandomRole(roleId);

            var result = await controller.Role(updatedRoleDto);

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

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

            var updatedRole = await identityService.GetRoleAsync(updatedRoleDto.Id.ToString());

            updatedRoleDto.Should().BeEquivalentTo(updatedRole, opts => opts.Excluding(x => x.Id));
        }
コード例 #8
0
        public async Task GetRole()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var roleDto    = IdentityDtoMock <string> .GenerateRandomRole();

            await identityService.CreateRoleAsync(roleDto);

            var roleId = await dbContext.Roles.Where(x => x.Name == roleDto.Name).Select(x => x.Id).SingleOrDefaultAsync();

            var result = await controller.Role(roleId);

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

            viewResult.ViewName.Should().BeNull();
            viewResult.ViewData.Should().NotBeNull();

            var viewModel = Assert.IsType <RoleDto <string> >(viewResult.ViewData.Model);

            roleDto.Id = roleId;
            var addedRole = await identityService.GetRoleAsync(roleDto.Id.ToString());

            viewModel.Should().BeEquivalentTo(addedRole);
        }
コード例 #9
0
        public async Task GetUser()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            //Add user
            await identityService.CreateUserAsync(userDto);

            //Get inserted userid
            var userId = await dbContext.Users.Where(x => x.UserName == userDto.UserName).Select(x => x.Id).SingleOrDefaultAsync();

            //Try call controller action
            var result = await controller.UserProfile(userId);

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

            viewResult.ViewName.Should().Be("UserProfile");
            viewResult.ViewData.Should().NotBeNull();

            var viewModel = Assert.IsType <UserDto <string> >(viewResult.ViewData.Model);

            userDto.Id = userId;
            var addedUser = await identityService.GetUserAsync(userDto.Id.ToString());

            viewModel.Should().BeEquivalentTo(addedUser);
        }
コード例 #10
0
        public async Task DeleteUserRoleAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.ShouldBeEquivalentTo(newRoleDto);

                var userRoleDto = IdentityDtoMock <string> .GenerateRandomUserRole <RoleDto <string> >(roleDto.Id, userDto.Id);

                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();
            }
        }
コード例 #11
0
        public async Task AddUserRoleAsync()
        {
            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();
            }
        }
コード例 #12
0
        public async Task DeleteUserProviderAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.Should().BeEquivalentTo(newUserDto);

                var userProvider = IdentityMock.GenerateRandomUserProviders(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
                                                                            newUserDto.Id);

                //Add new user login
                await context.UserLogins.AddAsync(userProvider);

                await context.SaveChangesAsync();

                //Get added user provider
                var addedUserProvider = await context.UserLogins.Where(x => x.ProviderKey == userProvider.ProviderKey && x.LoginProvider == userProvider.LoginProvider).SingleOrDefaultAsync();

                addedUserProvider.Should().NotBeNull();

                var userProviderDto = IdentityDtoMock <string> .GenerateRandomUserProviders(userProvider.ProviderKey, userProvider.LoginProvider,
                                                                                            userProvider.UserId);

                await identityService.DeleteUserProvidersAsync(userProviderDto);

                //Get deleted user provider
                var deletedUserProvider = await context.UserLogins.Where(x => x.ProviderKey == userProvider.ProviderKey && x.LoginProvider == userProvider.LoginProvider).SingleOrDefaultAsync();

                deletedUserProvider.Should().BeNull();
            }
        }
コード例 #13
0
        public async Task DeleteUserClaimAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.Should().BeEquivalentTo(newUserDto);

                //Generate random new user claim
                var userClaimDto = IdentityDtoMock <string> .GenerateRandomUserClaim(0, userDto.Id);

                await identityService.CreateUserClaimsAsync(userClaimDto);

                //Get new user claim
                var claim = await context.UserClaims.Where(x => x.ClaimType == userClaimDto.ClaimType && x.ClaimValue == userClaimDto.ClaimValue).SingleOrDefaultAsync();

                userClaimDto.ClaimId = claim.Id;

                var newUserClaim = await identityService.GetUserClaimAsync(userDto.Id.ToString(), claim.Id);

                //Assert new user claim
                userClaimDto.Should().BeEquivalentTo(newUserClaim);

                await identityService.DeleteUserClaimsAsync(userClaimDto);

                //Get deleted user claim
                var deletedClaim = await context.UserClaims.Where(x => x.ClaimType == userClaimDto.ClaimType && x.ClaimValue == userClaimDto.ClaimValue).SingleOrDefaultAsync();

                deletedClaim.Should().BeNull();
            }
        }
コード例 #14
0
        public async Task RemoveRoleClaimAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.Should().BeEquivalentTo(newRoleDto);

                //Generate random new role claim
                var roleClaimDto = IdentityDtoMock <string> .GenerateRandomRoleClaim(0, roleDto.Id);

                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.ToString(), roleClaimDto.ClaimId);

                //Assert new role
                roleClaimDto.Should().BeEquivalentTo(newRoleClaimDto, options => options.Excluding(o => o.RoleName));

                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();
            }
        }
コード例 #15
0
        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.Should().BeEquivalentTo(newRoleDto);

                //Generate random new role claim
                var roleClaimDto = IdentityDtoMock.GenerateRandomRoleClaim(0, roleDto.Id);

                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, roleClaimDto.ClaimId);

                //Assert new role
                roleClaimDto.Should().BeEquivalentTo(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();
            }
        }
コード例 #16
0
        public async Task DeleteUser()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            await identityService.CreateUserAsync(userDto);

            var userId = await dbContext.Users.Where(x => x.UserName == userDto.UserName).Select(x => x.Id).SingleOrDefaultAsync();

            userDto.Id = userId;

            // A user cannot delete own account
            var subjectClaim = new Claim(IdentityModel.JwtClaimTypes.Subject, userDto.Id);

            ProvideControllerContextWithClaimsPrincipal(controller, subjectClaim);

            var result = await controller.UserDelete(userDto);

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

            viewResult.ActionName.Should().Be("UserDelete", "Users cannot delete their own account");

            var user = await dbContext.Users.Where(x => x.Id == userDto.Id).SingleOrDefaultAsync();

            user.Should().NotBeNull();

            subjectClaim = new Claim(IdentityModel.JwtClaimTypes.Subject, "1");
            ProvideControllerContextWithClaimsPrincipal(controller, subjectClaim);
            result = await controller.UserDelete(userDto);

            // Assert
            viewResult = Assert.IsType <RedirectToActionResult>(result);
            viewResult.ActionName.Should().Be("Users");

            user = await dbContext.Users.Where(x => x.Id == userDto.Id).SingleOrDefaultAsync();

            user.Should().BeNull();
        }
コード例 #17
0
        public async Task DeleteUserProviderAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                await identityService.CreateUserAsync(userDto);

                //Get new user
                var user = await context.Users.Where(x => x.Email == userDto.Email).SingleOrDefaultAsync();

                userDto.Id = user.Id;

                var newUserDto = await identityService.GetUserAsync(userDto.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                var userProvider = IdentityMock.GenerateRandomUserProviders(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
                                                                            newUserDto.Id);

                //Add new user login
                await context.UserLogins.AddAsync(userProvider);

                await context.SaveChangesAsync();

                //Get added user provider
                var addedUserProvider = await context.UserLogins.Where(x => x.ProviderKey == userProvider.ProviderKey && x.LoginProvider == userProvider.LoginProvider).SingleOrDefaultAsync();

                addedUserProvider.Should().NotBeNull();

                var userProviderDto = IdentityDtoMock <string> .GenerateRandomUserProviders(userProvider.ProviderKey, userProvider.LoginProvider,
                                                                                            userProvider.UserId);

                await identityService.DeleteUserProvidersAsync(userProviderDto);

                //Get deleted user provider
                var deletedUserProvider = await context.UserLogins.Where(x => x.ProviderKey == userProvider.ProviderKey && x.LoginProvider == userProvider.LoginProvider).SingleOrDefaultAsync();

                deletedUserProvider.Should().BeNull();
            }
        }
コード例 #18
0
        public async Task AddUserClaimAsync()
        {
            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.Should().BeEquivalentTo(newUserDto);

                //Generate random new user claim
                var userClaimDto = IdentityDtoMock.GenerateRandomUserClaim(0, userDto.Id);

                await identityService.CreateUserClaimsAsync(userClaimDto);

                //Get new user claim
                var claim = await context.UserClaims.Where(x => x.ClaimType == userClaimDto.ClaimType && x.ClaimValue == userClaimDto.ClaimValue).SingleOrDefaultAsync();

                userClaimDto.ClaimId = claim.Id;

                var newUserClaim = await identityService.GetUserClaimAsync(userDto.Id, claim.Id);

                //Assert new user claim
                userClaimDto.Should().BeEquivalentTo(newUserClaim);
            }
        }
コード例 #19
0
        public async Task DeleteUserClaimAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                await identityService.CreateUserAsync(userDto);

                //Get new user
                var user = await context.Users.Where(x => x.Email == userDto.Email).SingleOrDefaultAsync();

                userDto.Id = user.Id;

                var newUserDto = await identityService.GetUserAsync(userDto.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                //Generate random new user claim
                var userClaimDto = IdentityDtoMock <string> .GenerateRandomUserClaim(0, userDto.Id);

                await identityService.CreateUserClaimsAsync(userClaimDto);

                //Get new user claim
                var claim = await context.UserClaims.Where(x => x.ClaimType == userClaimDto.ClaimType && x.ClaimValue == userClaimDto.ClaimValue).SingleOrDefaultAsync();

                userClaimDto.ClaimId = claim.Id;

                var newUserClaim = await identityService.GetUserClaimAsync(userDto.Id.ToString(), claim.Id);

                //Assert new user claim
                userClaimDto.ShouldBeEquivalentTo(newUserClaim);

                await identityService.DeleteUserClaimsAsync(userClaimDto);

                //Get deleted user claim
                var deletedClaim = await context.UserClaims.Where(x => x.ClaimType == userClaimDto.ClaimType && x.ClaimValue == userClaimDto.ClaimValue).SingleOrDefaultAsync();

                deletedClaim.Should().BeNull();
            }
        }
コード例 #20
0
        public async Task UpdateUserAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                //Detached the added item
                context.Entry(user).State = EntityState.Detached;

                //Generete new user with added item id
                var userDtoForUpdate = IdentityDtoMock <string> .GenerateRandomUser(user.Id);

                //Update user
                await identityService.UpdateUserAsync(userDtoForUpdate);

                var updatedUser = await identityService.GetUserAsync(userDtoForUpdate.Id.ToString());

                //Assert updated user
                userDtoForUpdate.ShouldBeEquivalentTo(updatedUser);
            }
        }
コード例 #21
0
        public async Task UpdateRoleAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.Should().BeEquivalentTo(newRoleDto);

                //Detached the added item
                context.Entry(role).State = EntityState.Detached;

                //Generete new role with added item id
                var roleDtoForUpdate = IdentityDtoMock <string> .GenerateRandomRole(role.Id);

                //Update role
                await identityService.UpdateRoleAsync(roleDtoForUpdate);

                var updatedRole = await identityService.GetRoleAsync(roleDtoForUpdate.Id.ToString());

                //Assert updated role
                roleDtoForUpdate.Should().BeEquivalentTo(updatedRole);
            }
        }
コード例 #22
0
        public async Task AddUserRole()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            await identityService.CreateUserAsync(userDto);

            var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

            await identityService.CreateRoleAsync(roleDto);

            var user = await dbContext.Users.Where(x => x.UserName == userDto.UserName).SingleOrDefaultAsync();

            userDto.Id = user.Id;

            var role = await dbContext.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync();

            roleDto.Id = role.Id;

            var userRoleDto = IdentityDtoMock <string> .GenerateRandomUserRole <RoleDto <string> >(roleDto.Id, user.Id);

            var result = await controller.UserRoles(userRoleDto);

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

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

            var userRole = await dbContext.UserRoles.Where(x => x.RoleId == roleDto.Id && x.UserId == userDto.Id).SingleOrDefaultAsync();

            userRoleDto.ShouldBeEquivalentTo(userRole, opts => opts.Excluding(x => x.Roles)
                                             .Excluding(x => x.RolesList)
                                             .Excluding(x => x.PageSize)
                                             .Excluding(x => x.TotalCount)
                                             .Excluding(x => x.UserName));
        }
コード例 #23
0
        public async Task AddUserAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);
            }
        }
コード例 #24
0
        public async Task AddRoleAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.ShouldBeEquivalentTo(newRoleDto);
            }
        }
コード例 #25
0
        public async Task DeleteRoleAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.Should().BeEquivalentTo(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();
            }
        }
コード例 #26
0
        public async Task DeleteUserAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var testUserManager = GetTestUserManager(context);
                var testRoleManager = GetTestRoleManager(context);

                var mapper = GetMapper();

                var identityRepository        = GetIdentityRepository(context, testUserManager, testRoleManager, mapper);
                var localizerIdentityResource = new IdentityServiceResources();
                var identityService           = GetIdentityService(identityRepository, localizerIdentityResource, mapper);

                //Generate random new user
                var userDto = IdentityDtoMock <string> .GenerateRandomUser();

                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.Id.ToString());

                //Assert new user
                userDto.ShouldBeEquivalentTo(newUserDto);

                //Remove user
                await identityService.DeleteUserAsync(newUserDto.Id.ToString(), newUserDto);

                //Try Get Removed user
                var removeUser = await context.Users.Where(x => x.Id == user.Id)
                                 .SingleOrDefaultAsync();

                //Assert removed user
                removeUser.Should().BeNull();
            }
        }
コード例 #27
0
        public async Task DeleteUserRole()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminDbContext>();
            var identityService = serviceProvider.GetRequiredService <IIdentityService>();

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock.GenerateRandomUser(0);
            await identityService.CreateUserAsync(userDto);

            var roleDto = IdentityDtoMock.GenerateRandomRole(0);
            await identityService.CreateRoleAsync(roleDto);

            var user = await dbContext.Users.Where(x => x.UserName == userDto.UserName).SingleOrDefaultAsync();

            userDto.Id = user.Id;

            var role = await dbContext.Roles.Where(x => x.Name == roleDto.Name).SingleOrDefaultAsync();

            roleDto.Id = role.Id;

            var userRoleDto = IdentityDtoMock.GenerateRandomUserRole(roleDto.Id, user.Id);

            await identityService.CreateUserRoleAsync(userRoleDto);

            var result = await controller.UserRolesDelete(userRoleDto);

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

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

            var userRole = await dbContext.UserRoles.Where(x => x.RoleId == roleDto.Id && x.UserId == userDto.Id).SingleOrDefaultAsync();

            userRole.Should().BeNull();
        }
コード例 #28
0
        public async Task AddRoleClaimAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //Assert new role
                roleDto.ShouldBeEquivalentTo(newRoleDto);

                //Generate random new role claim
                var roleClaimDto = IdentityDtoMock <string> .GenerateRandomRoleClaim(0, roleDto.Id);

                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.ToString(), roleClaimDto.ClaimId);

                //Assert new role
                roleClaimDto.ShouldBeEquivalentTo(newRoleClaimDto, options => options.Excluding(o => o.RoleName));
            }
        }
コード例 #29
0
        public async Task UserProvidersDelete()
        {
            //Get Services
            var serviceProvider = GetServices();
            var dbContext       = serviceProvider.GetRequiredService <AdminIdentityDbContext>();
            var identityService = GetIdentityService(serviceProvider);

            // Get controller
            var controller = PrepareIdentityController(serviceProvider);
            var userDto    = IdentityDtoMock <string> .GenerateRandomUser();

            await identityService.CreateUserAsync(userDto);

            var userId = await dbContext.Users.Where(x => x.UserName == userDto.UserName).Select(x => x.Id).SingleOrDefaultAsync();

            var randomProviderKey   = Guid.NewGuid().ToString();
            var randomProviderLogin = Guid.NewGuid().ToString();

            var provider = IdentityMock.GenerateRandomUserProviders(randomProviderKey, randomProviderLogin, userId);

            await dbContext.UserLogins.AddAsync(provider);

            await dbContext.SaveChangesAsync();

            var providersDto = IdentityDtoMock <string> .GenerateRandomUserProviders(randomProviderKey, randomProviderLogin, userId);

            var result = await controller.UserProvidersDelete(providersDto);

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

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

            var userProvider = await dbContext.UserLogins.Where(x => x.ProviderKey == randomProviderKey).SingleOrDefaultAsync();

            userProvider.Should().BeNull();
        }
コード例 #30
0
        public async Task UpdateRoleAsync()
        {
            using (var context = new AdminIdentityDbContext(_dbContextOptions))
            {
                var identityService = GetIdentityService(context);

                //Generate random new role
                var roleDto = IdentityDtoMock <string> .GenerateRandomRole();

                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.Id.ToString());

                //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 <string> .GenerateRandomRole(role.Id);

                //Update role
                await identityService.UpdateRoleAsync(roleDtoForUpdate);

                var updatedRole = await identityService.GetRoleAsync(roleDtoForUpdate.Id.ToString());

                //Assert updated role
                roleDtoForUpdate.ShouldBeEquivalentTo(updatedRole);
            }
        }