Search in sources :

Example 1 with PasswordsMismatchException

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);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) PasswordsMismatchException(com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException) PasswordsMismatchException(com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with PasswordsMismatchException

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;
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) AlreadyExistsException(com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException) PasswordsMismatchException(com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)2 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)2 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1