Search in sources :

Example 1 with EmailChangeDTO

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

Example 2 with EmailChangeDTO

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

Example 3 with EmailChangeDTO

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

Example 4 with EmailChangeDTO

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

Example 5 with EmailChangeDTO

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;
}
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)6 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 EmailChangeDTO (com.ncedu.fooddelivery.api.v1.dto.user.EmailChangeDTO)3 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)2 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)2