예제 #1
0
        public void ShouldHaveValidationErrorWithEmptyOldPassword(string OldPassword)
        {
            UpdatePasswordUserRequestModel requestModel = new UpdatePasswordUserRequestModel()
            {
                OldPassword = OldPassword
            };

            _validator.ShouldHaveValidationErrorFor(r => r.OldPassword, requestModel).WithErrorMessage("old password cannot be empty.");
        }
예제 #2
0
        public void ShouldHaveValidationErrorWithGreaterThan60CharactersOldPassword(string OldPassword)
        {
            UpdatePasswordUserRequestModel requestModel = new UpdatePasswordUserRequestModel()
            {
                OldPassword = OldPassword
            };

            _validator.ShouldHaveValidationErrorFor(r => r.OldPassword, requestModel).WithErrorMessage("old password must be less than 60 characters.");
        }
예제 #3
0
        public void ShouldNotHaveValidationErrorWithEqualNewPassword(string newPassword, string newPasswordConfirmation)
        {
            UpdatePasswordUserRequestModel requestModel = new UpdatePasswordUserRequestModel()
            {
                NewPassword             = newPassword,
                NewPasswordConfirmation = newPasswordConfirmation
            };

            _validator.ShouldNotHaveValidationErrorFor(r => r.NewPasswordConfirmation, requestModel);
        }
예제 #4
0
        public void ShouldHaveValidationErrorWithNotEqualNewPassword(string newPassword, string newPasswordConfirmation)
        {
            UpdatePasswordUserRequestModel requestModel = new UpdatePasswordUserRequestModel()
            {
                NewPassword             = newPassword,
                NewPasswordConfirmation = newPasswordConfirmation
            };

            _validator.ShouldHaveValidationErrorFor(r => r.NewPasswordConfirmation, requestModel).WithErrorMessage("new password confirmation must be equal to new password.");
        }
예제 #5
0
        public async Task <IActionResult> UpdatePassword(UpdatePasswordUserRequestModel model)
        {
            try
            {
                await _userService.UpdatePassword(this.GetUserIdFromToken(), model);

                return(Ok());
            }
            catch (Exception exception)
            {
                return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception));
            }
        }
예제 #6
0
        public async Task ShouldReturnOkOnCallUpdateUserPassword()
        {
            Guid id = await InsertUserOnDatabase();

            _httpClient.InsertAuthorizationTokenOnRequestHeader(_authorizationTokenHelper.CreateToken(id));
            UpdatePasswordUserRequestModel model = new UpdatePasswordUserRequestModel()
            {
                NewPassword = "******", NewPasswordConfirmation = "NEWPASSWORD", OldPassword = "******"
            };

            HttpResponseMessage httpResponse = await _httpClient.PutAsync("../users/password", _createRequestHelper.CreateStringContent(model));

            Assert.Equal((int)HttpStatusCode.OK, (int)httpResponse.StatusCode);
        }
예제 #7
0
        public async Task ShouldInvalidPasswordExceptionOnTryUpdateUserPasswordWithIncorrectOldPassword()
        {
            UpdatePasswordUserRequestModel model = new UpdatePasswordUserRequestModel()
            {
                OldPassword             = "******",
                NewPassword             = "******",
                NewPasswordConfirmation = "new password"
            };

            _userRepositoryMock.GetById(Arg.Any <Guid>()).Returns(_fakeConfirmedInsertedUser);
            _hashUtilsMock.CompareHash(Arg.Any <string>(), Arg.Any <string>()).Returns(false);

            Exception exception = await Record.ExceptionAsync(() => _userService.UpdatePassword(_fakeConfirmedInsertedUser.Id.ToString(), model));

            Assert.IsType <InvalidPasswordException>(exception);
        }
예제 #8
0
        public async Task ShouldThrowResourceNotFoundExceptionOnTryUpdateNotExistsUserPassword()
        {
            User notExistsUser = null;
            UpdatePasswordUserRequestModel model = new UpdatePasswordUserRequestModel()
            {
                OldPassword             = "******",
                NewPassword             = "******",
                NewPasswordConfirmation = "new password"
            };

            _userRepositoryMock.GetById(Arg.Any <Guid>()).Returns(notExistsUser);

            Exception exception = await Record.ExceptionAsync(() => _userService.UpdatePassword(Guid.NewGuid().ToString(), model));

            Assert.IsType <ResourceNotFoundException>(exception);
        }
예제 #9
0
        public async Task ShouldUpdateUserPassword()
        {
            UpdatePasswordUserRequestModel model = new UpdatePasswordUserRequestModel()
            {
                OldPassword             = "******",
                NewPassword             = "******",
                NewPasswordConfirmation = "new password"
            };

            _userRepositoryMock.GetById(Arg.Any <Guid>()).Returns(_fakeConfirmedInsertedUser);
            _hashUtilsMock.CompareHash(Arg.Any <string>(), Arg.Any <string>()).Returns(true);

            Exception exception = await Record.ExceptionAsync(() => _userService.UpdatePassword(_fakeConfirmedInsertedUser.Id.ToString(), model));

            Assert.Null(exception);
            await _userRepositoryMock.Received(1).Save();
        }
예제 #10
0
        public async Task UpdatePassword(string userId, UpdatePasswordUserRequestModel model)
        {
            await new UpdatePasswordUserValidator().ValidateRequestModelAndThrow(model);

            User user = await _userRepository.GetById(Guid.Parse(userId));

            ThrowIfUserIsNullOrNotConfirmed(user);
            if (!_hashUtils.CompareHash(model.OldPassword, user.Password))
            {
                throw new InvalidPasswordException();
            }

            user.UpdatePassword(_hashUtils.GenerateHash(model.NewPassword));

            _userRepository.Update(user);
            await _userRepository.Save();
        }