public async Task TestChangePassword()
        {
            var client         = _factory.WithMockUser("change-password").CreateClient();
            var userManager    = _factory.GetRequiredService <UserManager <User> >();
            var passwordHasher = _factory.GetRequiredService <IPasswordHasher <User> >();

            var currentPassword = TestUtil.RandomAlphabetic(60);
            var user            = new User
            {
                Login        = "******",
                Email        = "*****@*****.**",
                PasswordHash = passwordHasher.HashPassword(null, currentPassword)
            };

            await userManager.CreateAsync(user);

            var response = await client.PostAsync("/api/account/change-password", TestUtil.ToJsonContent(
                                                      new PasswordChangeDto
            {
                CurrentPassword = currentPassword,
                NewPassword     = "******"
            }));

            response.StatusCode.Should().Be(HttpStatusCode.OK);

            var updatedUser = await userManager.FindByNameAsync(user.Login);

            //TODO FIX database refresh to prevent the usage of context/Reload
            updatedUser = Fixme.ReloadUser(_factory, updatedUser);
            passwordHasher.VerifyHashedPassword(updatedUser, updatedUser.PasswordHash, "new password").Should()
            .Be(PasswordVerificationResult.Success);
        }
        public async Task TestActivateAccount()
        {
            var client         = _factory.CreateClient();
            var userManager    = _factory.GetRequiredService <UserManager <User> >();
            var passwordHasher = _factory.GetRequiredService <IPasswordHasher <User> >();

            const string activationKey = "some activation key";
            var          user          = new User
            {
                Login         = "******",
                Email         = "*****@*****.**",
                PasswordHash  = passwordHasher.HashPassword(null, TestUtil.RandomAlphabetic(60)),
                Activated     = false,
                ActivationKey = activationKey
            };

            await userManager.CreateAsync(user);

            var response = await client.GetAsync($"/api/activate?key={activationKey}");

            response.StatusCode.Should().Be(HttpStatusCode.OK);

            user = await userManager.FindByNameAsync(user.Login);

            //TODO FIX database refresh to prevent the usage of context/Reload
            user = Fixme.ReloadUser(_factory, user);
            user.Activated.Should().Be(true);
        }
        public async Task TestFinishPasswordResetTooSmall()
        {
            var client         = _factory.CreateClient();
            var userManager    = _factory.GetRequiredService <UserManager <User> >();
            var passwordHasher = _factory.GetRequiredService <IPasswordHasher <User> >();

            var user = new User
            {
                Login        = "******",
                Email        = "*****@*****.**",
                PasswordHash = passwordHasher.HashPassword(null, TestUtil.RandomAlphabetic(60)),
                ResetDate    = DateTime.Now.Add(60.Seconds()),
                ResetKey     = TestUtil.RandomAlphabetic(60)
            };

            await userManager.CreateAsync(user);

            var keyAndPassword = new KeyAndPasswordDto
            {
                Key         = user.ResetKey,
                NewPassword = TestUtil.RandomAlphabetic(3)
            };

            var response = await client.PostAsync("/api/account/reset-password/finish",
                                                  TestUtil.ToJsonContent(keyAndPassword));

            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

            var updatedUser = await userManager.FindByNameAsync(user.Login);

            //TODO FIX database refresh to prevent the usage of context/Reload
            updatedUser = Fixme.ReloadUser(_factory, updatedUser);
            passwordHasher.VerifyHashedPassword(updatedUser, updatedUser.PasswordHash, keyAndPassword.NewPassword)
            .Should().Be(PasswordVerificationResult.Failed);
        }
        public async Task TestSaveAccount()
        {
            var client         = _factory.WithMockUser("save-account").CreateClient();
            var userManager    = _factory.GetRequiredService <UserManager <User> >();
            var passwordHasher = _factory.GetRequiredService <IPasswordHasher <User> >();

            var user = new User
            {
                Login        = "******",
                Email        = "*****@*****.**",
                PasswordHash = passwordHasher.HashPassword(null, TestUtil.RandomAlphabetic(60)),
                Activated    = true
            };

            await userManager.CreateAsync(user);

            var userDto = new UserDto
            {
                Login     = "******",
                FirstName = "firstname",
                LastName  = "lastname",
                Email     = "*****@*****.**",
                Activated = false,
                ImageUrl  = "http://placehold.it/50x50",
                LangKey   = Constants.DefaultLangKey,
                Roles     = new HashSet <string> {
                    RolesConstants.USER
                }
            };

            var response = await client.PostAsync("/api/account", TestUtil.ToJsonContent(userDto));

            response.StatusCode.Should().Be(HttpStatusCode.OK);

            var updatedUser = await userManager.Users
                              .Include(it => it.UserRoles)
                              .ThenInclude(r => r.Role)
                              .SingleOrDefaultAsync(it => it.UserName == user.Login);

            //TODO FIX database refresh to prevent the usage of context/Reload
            updatedUser = Fixme.ReloadUser(_factory, updatedUser);
            updatedUser.FirstName.Should().Be(userDto.FirstName);
            updatedUser.LastName.Should().Be(userDto.LastName);
            updatedUser.Email.Should().Be(userDto.Email);
            updatedUser.LangKey.Should().Be(userDto.LangKey);
            updatedUser.ImageUrl.Should().Be(userDto.ImageUrl);
            updatedUser.PasswordHash.Should().Be(user.PasswordHash);
            updatedUser.Activated.Should().BeTrue();
            updatedUser.UserRoles.IsNullOrEmpty().Should().BeTrue();
        }
Example #5
0
        public async Task UpdateUserLogin()
        {
            // Initialize the database
            await _userManager.CreateAsync(_user);

            var databaseSizeBeforeUpdate = _userManager.Users.Count();

            //Update the user
            var updatedUser = await _userManager.FindByIdAsync(_user.Id);

            //TODO Add CreatedBy, CreatedDate, ModifiedBy, ModidfieDate
            var managedUserDto = new ManagedUserDto
            {
                Id        = updatedUser.Id,
                Login     = UpdatedLogin,
                Password  = UpdatedPassword,
                FirstName = UpdatedFirstname,
                LastName  = UpdatedLastname,
                Email     = UpdatedEmail,
                Activated = updatedUser.Activated,
                ImageUrl  = UpdatedImageurl,
                LangKey   = UpdatedLangkey,
                Roles     = new HashSet <string>
                {
                    RolesConstants.USER
                }
            };

            var response = await _client.PutAsync("/api/users", TestUtil.ToJsonContent(managedUserDto));

            response.StatusCode.Should().Be(HttpStatusCode.OK);

            // Validate the User in the database
            var userList = _userManager.Users.ToList();

            userList.Count().Should().Be(databaseSizeBeforeUpdate);
            //            var testUser = userList[userList.Count - 1];
            //            TODO FIX database refresh to prevent the usage of context/Reload
            var testUser = Fixme.ReloadUser(_factory, updatedUser);

            testUser.Login.Should().Be(UpdatedLogin);
            testUser.FirstName.Should().Be(UpdatedFirstname);
            testUser.LastName.Should().Be(UpdatedLastname);
            testUser.Email.Should().Be(UpdatedEmail);
            testUser.ImageUrl.Should().Be(UpdatedImageurl);
            testUser.LangKey.Should().Be(UpdatedLangkey);
        }