Example #1
0
        public async Task CreateUserAsync_GivenRequestIsNull_ThrowArgumentNullException()
        {
            // Arrange
            UserCreationRequest request = null;

            // Act + Assert
            ArgumentNullException exception =
                await Assert.ThrowsAsync <ArgumentNullException>(() => _identityService.CreateUserAsync(request))
                .ConfigureAwait(false);

            Assert.NotNull(exception);
            Assert.Equal(string.Format(ErrorMessages.NullExceptionMessage, "request"), exception.Message);
        }
Example #2
0
        public async Task AddUserAsync()
        {
            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);
            }
        }
Example #3
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.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();
            }
        }
Example #4
0
        public async Task <IActionResult> RegisterAsync(RegisterRequest request)
        {
            var res = await identityService.CreateUserAsync(Guid.NewGuid(), request.Username, request.Username, request.Password);

            if (res.Successful)
            {
                return(Ok());
            }

            return(BadRequest(res.Errors));
        }
Example #5
0
        public async Task DeleteUserProviderAsync()
        {
            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);

                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.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();
            }
        }
Example #6
0
        public async Task DeleteUserClaimAsync()
        {
            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 user claim
                var userClaimDto = IdentityDtoMock.GenerateRandomUserClaim(0, userDto.Id.Value);

                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.Value, 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();
            }
        }
Example #7
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = await _service.GetExternalAuthenticationSchemesAsync();

            if (ModelState.IsValid)
            {
                var result = await _service.CreateUserAsync(Input.Name, Input.Email, Input.Password);

                if (result.UserCreateResponse.Succeeded)
                {
                    _logger.LogWarning($"User '{Input.Email}' created a new account with password.");

                    var code = result.ConfirmationToken;
                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = result.UserId, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(
                        Input.Email,
                        _locService.GetLocalizedString("Confirm your email"),
                        _locService.GetLocalizedString("Please confirm your account by") + $" <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>" + _locService.GetLocalizedString("clicking here") + "</a>.");

                    if (_service.RequireConfirmedAccount())
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _service.SignUserById(result.UserId, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.UserCreateResponse.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Example #8
0
        public async Task UpdateUserAsync()
        {
            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);

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

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

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

                var updatedUser = await identityService.GetUserAsync(userDtoForUpdate);

                //Assert updated user
                userDtoForUpdate.ShouldBeEquivalentTo(updatedUser);
            }
        }
Example #9
0
        public async Task <ActionResult> SignUp([FromBody] SignUpViewModel signUpModel)
        {
            var user = await _signUpService.SignUpAsync(signUpModel.MapTo <User>());

            if (user == null)
            {
                return(Conflict());
            }

            var payload = new CreateUserPayload
            {
                DomainId = user.Id,
                Email    = user.Email,
                Role     = signUpModel.Role,
                Password = signUpModel.Password
            };

            await _identityService.CreateUserAsync(payload);

            return(Ok());
        }
Example #10
0
        public async Task <IActionResult> Create(ManageAccountViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.RoleTypes = Util.GetSelectList(_identityService.GetRoles());
                return(View(model));
            }
            var result = await _identityService.CreateUserAsync(model.Username, model.Password, model.Role, model.Email);

            if (result.Succeeded)
            {
                return(RedirectToAction("Index"));
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
            model.RoleTypes = Util.GetSelectList(_identityService.GetRoles());
            return(View(model));
        }
Example #11
0
        public async Task DeleteUserAsync()
        {
            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 user
                var userDto = IdentityDtoMock.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);

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

                //Remove user
                await identityService.DeleteUserAsync(newUserDto);

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

                //Assert removed user
                removeUser.Should().BeNull();
            }
        }