-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- updateMyInfo() - nullExceptionUpdateMyInfo() - search()
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.seoultech.synergybe.domain.common.CustomPasswordEncoder; | ||
import com.seoultech.synergybe.domain.common.RandomNumber; | ||
import com.seoultech.synergybe.domain.email.MailService; | ||
import com.seoultech.synergybe.domain.user.dto.request.CreateUserRequest; | ||
|
@@ -18,6 +19,7 @@ | |
import org.junit.jupiter.api.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -57,6 +59,9 @@ class UserServiceTest { | |
@Autowired | ||
private UserRefreshTokenFactory userRefreshTokenFactory; | ||
|
||
@Autowired | ||
private CustomPasswordEncoder passwordEncoder; | ||
|
||
@PostConstruct | ||
public void setAuthNumber() { | ||
this.authNumber = randomNumber.generateRandomNumber(); | ||
|
@@ -261,4 +266,83 @@ void invalidRefreshTokengenerateAccessTokenByRefreshToken() { | |
assertThrows(UserBadRequestException.class, | ||
() -> userService.generateAccessTokenByRefreshToken(request, response)); | ||
} | ||
|
||
@DisplayName("유저는 정보를 업데이트 한다.") | ||
@Test | ||
void updateMyInfo() { | ||
String initialEmail = "[email protected]"; | ||
String newEmail = "[email protected]"; | ||
String initialName = "Old Name"; | ||
String newName = "New Name"; | ||
String initialMajor = "Old Major"; | ||
String newMajor = "New Major"; | ||
String password = "password"; | ||
|
||
CreateUserRequest request = new CreateUserRequest(initialEmail, password, initialName, initialMajor, authNumber); | ||
redisUtil.setDataExpire(authNumber, initialEmail, 60*5L); | ||
|
||
// when | ||
ValidateNumberRequest validateRequest = new ValidateNumberRequest(initialEmail, authNumber); | ||
userService.validateNumber(validateRequest); | ||
String userToken = userService.createUser(request); | ||
|
||
// when | ||
userService.updateMyInfo(userToken, newEmail, newName, newMajor); | ||
// Then: 사용자 정보가 제대로 업데이트 되었는지 확인 | ||
User updatedUser = userRepository.findByUserToken(userToken).orElseThrow(); | ||
assertThat(updatedUser.getEmail().getEmail()).isEqualTo(newEmail); | ||
assertThat(updatedUser.getName().getName()).isEqualTo(newName); | ||
assertThat(updatedUser.getMajor().getMajor()).isEqualTo(newMajor); | ||
} | ||
|
||
@DisplayName("유저 정보를 업데이트시 null 입력시 예외가 발생한다.") | ||
@Test | ||
void nullExceptionUpdateMyInfo() { | ||
String initialEmail = "[email protected]"; | ||
String initialName = "Old Name"; | ||
String newName = "New Name"; | ||
String initialMajor = "Old Major"; | ||
String newMajor = "New Major"; | ||
String password = "password"; | ||
|
||
CreateUserRequest request = new CreateUserRequest(initialEmail, password, initialName, initialMajor, authNumber); | ||
redisUtil.setDataExpire(authNumber, initialEmail, 60*5L); | ||
|
||
ValidateNumberRequest validateRequest = new ValidateNumberRequest(initialEmail, authNumber); | ||
userService.validateNumber(validateRequest); | ||
String userToken = userService.createUser(request); | ||
|
||
// When | ||
assertThrows(NullPointerException.class, () -> { | ||
userService.updateMyInfo(userToken, null, newName, newMajor); | ||
}); | ||
|
||
// Then | ||
User updatedUser = userRepository.findByUserToken(userToken).orElseThrow(); | ||
assertThat(updatedUser.getEmail().getEmail()).isEqualTo(initialEmail); | ||
assertThat(updatedUser.getName().getName()).isEqualTo(initialName); | ||
assertThat(updatedUser.getMajor().getMajor()).isEqualTo(initialMajor); | ||
} | ||
|
||
@DisplayName("유저를 keyword로 검색한다.") | ||
@Test | ||
void search() { | ||
String name = "jonghun"; | ||
User user = User.builder() | ||
.id(1L) | ||
.userToken("userToken") | ||
.email("[email protected]") | ||
.password("password") | ||
.major("major") | ||
.name(name) | ||
.passwordEncoder(passwordEncoder) | ||
.build(); | ||
userRepository.save(user); | ||
|
||
// when | ||
Specification<User> searchUser = userService.search("jonghun"); | ||
|
||
// then | ||
assertThat(searchUser).isNotNull(); | ||
} | ||
} |