use of com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProfileControllerTest method changeEmailWithBadValues.
@Test
public void changeEmailWithBadValues() {
EmailChangeDTO emailChangeDTO = new EmailChangeDTO();
emailChangeDTO.setEmail("wolowitz.bigbang");
emailChangeDTO.setPassword("small");
User user = new User();
user.setId(1L);
when(userServiceMock.changeEmail(user, emailChangeDTO)).thenReturn(false);
ResponseEntity<?> resultResponse = profileController.changeUserEmail(emailChangeDTO, user);
ResponseEntity<?> perfectResponse = createModifyResponse(false);
assertEquals(perfectResponse, resultResponse);
}
use of com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProfileControllerTest method changeEmail.
@Test
public void changeEmail() {
EmailChangeDTO emailChangeDTO = new EmailChangeDTO();
emailChangeDTO.setEmail("wolowitz@bigbang.theory");
emailChangeDTO.setPassword("bigBangTheory");
User user = new User();
user.setId(1L);
when(userServiceMock.changeEmail(user, emailChangeDTO)).thenReturn(true);
ResponseEntity<?> resultResponse = profileController.changeUserEmail(emailChangeDTO, user);
ResponseEntity<?> perfectResponse = createModifyResponse(true);
assertEquals(perfectResponse, resultResponse);
}
use of com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changeEmailPasswordMismatch.
@Test
public void changeEmailPasswordMismatch() {
Long userId = 1L;
User howard = UserUtils.courierHowardWolowitz(userId);
String newEmail = "happy-howard@bigbang.theory";
when(userRepoMock.findByEmail(newEmail)).thenReturn(null);
String password = "wrong-password";
EmailChangeDTO changeDTO = new EmailChangeDTO();
changeDTO.setEmail(newEmail);
changeDTO.setPassword(password);
assertThrows(PasswordsMismatchException.class, () -> {
userService.changeEmail(howard, changeDTO);
});
verify(userRepoMock, times(1)).findByEmail(newEmail);
verify(userRepoMock, never()).save(any(User.class));
}
use of com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changeEmailSuccess.
@Test
public void changeEmailSuccess() {
Long userId = 1L;
User howard = UserUtils.courierHowardWolowitz(userId);
String newEmail = "happy-howard@bigbang.theory";
User howardWithNewEmail = UserUtils.courierHowardWolowitz(userId);
howardWithNewEmail.setEmail(newEmail);
when(userRepoMock.findByEmail(newEmail)).thenReturn(null);
when(userRepoMock.save(howardWithNewEmail)).thenReturn(howardWithNewEmail);
String password = "password";
EmailChangeDTO changeDTO = new EmailChangeDTO();
changeDTO.setEmail(newEmail);
changeDTO.setPassword(password);
boolean result = userService.changeEmail(howard, changeDTO);
assertTrue(result);
verify(userRepoMock, times(1)).findByEmail(newEmail);
verify(userRepoMock, times(1)).save(howardWithNewEmail);
}
use of com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO 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