PasswordOldPasswordMatchesRule implements PasswordValidationRule { @Override public void validate(PasswordChangeDTO passwordChangeDTO) throws PasswordOldMatchValidationException { Assert.notNull(passwordChangeDTO); Assert.notNull(passwordChangeDTO.getOldPassword()); UserDetails userDetails; try { userDetails = authenticationService.getCurrentUserDetails(); } catch (UserNotLoggedInException e) { log.error("User is not logged in: ", e); throw new PasswordOldMatchValidationException(); } if (!(bCryptPasswordEncoder .matches(passwordChangeDTO.getOldPassword(), userDetails.getPassword()))) { throw new PasswordOldMatchValidationException(); } } PasswordOldPasswordMatchesRule(AuthenticationService authenticationService); @Override void validate(PasswordChangeDTO passwordChangeDTO);  }
@Test(expected = IllegalArgumentException.class) public void isValidShouldNotBeValidWhenPasswordChangeDTOIsNull() throws PasswordOldMatchValidationException { passwordOldPasswordMatchesRule.validate(null); }
@Test(expected = PasswordOldMatchValidationException.class) public void isValidShouldNotBeValidWhenUserIsNotLoggedIn() throws UserNotLoggedInException, PasswordOldMatchValidationException { PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO.builder().newPassword(NEW_PASSWORD) .newPasswordConfirm(NEW_PASSWORD_CONFIRM).oldPassword(PASSWORD_CHANGE_DTO_OLD_PASSWORD) .build(); given(authenticationService.getCurrentUserDetails()).willThrow(UserNotLoggedInException.class); passwordOldPasswordMatchesRule.validate(passwordChangeDTO); }
@Test(expected = IllegalArgumentException.class) public void isValidShouldNotBeValidWhenPasswordChangeDTOsPasswordFieldIsNull() throws PasswordOldMatchValidationException { PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO.builder().newPassword(NEW_PASSWORD) .newPasswordConfirm(NEW_PASSWORD_CONFIRM).oldPassword(null).build(); passwordOldPasswordMatchesRule.validate(passwordChangeDTO); }
@Test(expected = PasswordOldMatchValidationException.class) public void isValidShouldNotBeValidWhenUserDetailsPasswordFieldIsNull() throws UserNotLoggedInException, PasswordOldMatchValidationException { PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO.builder().newPassword(NEW_PASSWORD) .newPasswordConfirm(NEW_PASSWORD_CONFIRM).oldPassword(PASSWORD_CHANGE_DTO_OLD_PASSWORD) .build(); UserDetailsAdapter userDetailsAdapterWithNullPassword = new UserDetailsAdapter(USER_ID, null, USER_NAME); given(authenticationService.getCurrentUserDetails()) .willReturn(userDetailsAdapterWithNullPassword); passwordOldPasswordMatchesRule.validate(passwordChangeDTO); }
@Test public void isValidShouldBeValidWhenOldPasswordMatches() throws PasswordOldMatchValidationException { PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO.builder().newPassword(NEW_PASSWORD) .newPasswordConfirm(NEW_PASSWORD_CONFIRM).oldPassword(PASSWORD_CHANGE_DTO_OLD_PASSWORD) .build(); passwordOldPasswordMatchesRule.validate(passwordChangeDTO); }
@Test(expected = PasswordOldMatchValidationException.class) public void isValidShouldNotBeValidWhenOldPasswordsAreDifferent() throws PasswordOldMatchValidationException { PasswordChangeDTO passwordChangeDTO = PasswordChangeDTO.builder().newPassword(NEW_PASSWORD) .newPasswordConfirm(NEW_PASSWORD_CONFIRM) .oldPassword(PASSWORD_CHANGE_DTO_DIFFERENT_OLD_PASSWORD).build(); passwordOldPasswordMatchesRule.validate(passwordChangeDTO); }