use of com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException in project 2021-msk-food-delivery by netcracker-edu.
the class ProfileControllerTest method changePasswordMismatchError.
@Test
public void changePasswordMismatchError() {
PasswordChangeDTO pwdChangeDTO = new PasswordChangeDTO();
pwdChangeDTO.setOldPassword("password");
pwdChangeDTO.setNewPassword("qwerty123");
pwdChangeDTO.setNewPasswordConfirm("INCORRECT");
User user = new User();
user.setId(1L);
when(userServiceMock.changePassword(user, pwdChangeDTO)).thenThrow(new PasswordsMismatchException());
Exception exception = assertThrows(PasswordsMismatchException.class, () -> {
profileController.changeUserPassword(pwdChangeDTO, user);
});
String resultMessage = exception.getMessage();
String perfectMessage = new PasswordsMismatchException().getMessage();
verify(userServiceMock, times(1)).changePassword(user, pwdChangeDTO);
assertEquals(perfectMessage, resultMessage);
}
use of com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceImpl method changeEmail.
@Override
public boolean changeEmail(User user, EmailChangeDTO newEmailInfo) {
String newUserEmail = newEmailInfo.getEmail();
User userWithNewEmail = userRepo.findByEmail(newUserEmail);
// user with new email also exist throw exception!
if (userWithNewEmail != null) {
throw new AlreadyExistsException(newUserEmail);
}
String inputPassword = newEmailInfo.getPassword();
String userEncodedPassword = user.getPassword();
boolean isPasswordsSame = encoder.matches(inputPassword, userEncodedPassword);
if (!isPasswordsSame) {
throw new PasswordsMismatchException();
}
user.setEmail(newUserEmail);
userRepo.save(user);
return true;
}
Aggregations