Example #1
0
        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.Should().BeEquivalentTo(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.Should().BeEquivalentTo(newRoleDto);

                var userRoleDto = IdentityDtoMock.GenerateRandomUserRole(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();
            }
        }