Search in sources :

Example 71 with User

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;
}
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)

Example 72 with User

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);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 73 with User

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

Example 74 with User

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

Example 75 with User

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);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) ArrayList(java.util.ArrayList) UserInfoDTO(com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)49 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)49 File (com.ncedu.fooddelivery.api.v1.entities.File)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 MultipartFile (org.springframework.web.multipart.MultipartFile)18 Path (java.nio.file.Path)16 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)12 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 BufferedImage (java.awt.image.BufferedImage)8 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)7 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 PersistenceContext (javax.persistence.PersistenceContext)6 User (org.example.app0.entities.User)6 User (org.example.app1.entities.User)6 User (org.example.app6.entities.User)5 EmailChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO)4 PasswordChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.PasswordChangeDTO)4 IOException (java.io.IOException)4