use of com.laurentiuspilca.ssc6.entities.User 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;
}
use of com.laurentiuspilca.ssc6.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserController method changeUserInfo.
@PutMapping("/api/v1/user/{id}")
@PreAuthorize("hasAnyAuthority('MODERATOR', 'ADMIN')")
public ResponseEntity<?> changeUserInfo(@PathVariable Long id, @Valid @RequestBody UserChangeInfoDTO newUserInfo) {
User user = userService.getUserById(id);
String userRole = user.getRole().name();
boolean isModified = false;
if (Role.isCLIENT(userRole)) {
isModified = clientService.changeClientInfo(id, newUserInfo);
}
// for admin and moderator we can change only full name
String newFullName = newUserInfo.getFullName();
isModified = userService.changeFullName(id, newFullName);
log.debug("User with id " + id + " modified " + isModified);
return createModifyResponse(isModified);
}
use of com.laurentiuspilca.ssc6.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changeEmailAlreadyExists.
@Test
public void changeEmailAlreadyExists() {
Long userId = 1L;
User penny = UserUtils.clientPennyTeller(userId);
Long anotherId = 2L;
User leonard = UserUtils.moderatorLeonardHofstadter(anotherId);
String newPennyEmail = leonard.getEmail();
when(userRepoMock.findByEmail(newPennyEmail)).thenReturn(leonard);
String password = "password";
EmailChangeDTO changeDTO = new EmailChangeDTO();
changeDTO.setEmail(newPennyEmail);
changeDTO.setPassword(password);
Exception exception = assertThrows(AlreadyExistsException.class, () -> {
userService.changeEmail(penny, changeDTO);
});
String resultMessage = exception.getMessage();
String perfectMessage = new AlreadyExistsException(newPennyEmail).getMessage();
assertEquals(perfectMessage, resultMessage);
verify(userRepoMock, times(1)).findByEmail(newPennyEmail);
verify(userRepoMock, never()).save(any(User.class));
}
use of com.laurentiuspilca.ssc6.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changePasswordOldPassIncorrect.
@Test
public void changePasswordOldPassIncorrect() {
Long userId = 1L;
User rajesh = UserUtils.clientRajeshKoothrappali(userId);
String oldPass = "wrongOldPass";
String newPass = "koothrappali";
PasswordChangeDTO passwordDTO = new PasswordChangeDTO();
passwordDTO.setNewPassword(newPass);
passwordDTO.setNewPasswordConfirm(newPass);
passwordDTO.setOldPassword(oldPass);
Exception exception = assertThrows(PasswordsMismatchException.class, () -> {
userService.changePassword(rajesh, passwordDTO);
});
verify(userRepoMock, never()).save(any(User.class));
}
use of com.laurentiuspilca.ssc6.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method getAllAdminsSuccess.
@Test
public void getAllAdminsSuccess() {
Long userId = 1L;
User sheldonAdmin = UserUtils.adminSheldonCooper(userId);
List<User> admins = new ArrayList<>();
admins.add(sheldonAdmin);
when(userRepoMock.findByRole(Role.ADMIN)).thenReturn(admins);
List<UserInfoDTO> resultDTO = userService.getAllAdmins();
List<UserInfoDTO> perfectDTO = new ArrayList<>();
perfectDTO.add(UserUtils.createUserDTO(sheldonAdmin));
assertEquals(perfectDTO, resultDTO);
verify(userRepoMock, times(1)).findByRole(Role.ADMIN);
}
Aggregations