Skip to content

Commit

Permalink
#119 test: userService 테스트 코드 작성
Browse files Browse the repository at this point in the history
- updateMyInfo()
- nullExceptionUpdateMyInfo()
- search()
  • Loading branch information
rivkode committed Sep 5, 2024
1 parent 746521e commit 9efed07
Showing 1 changed file with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -57,6 +59,9 @@ class UserServiceTest {
@Autowired
private UserRefreshTokenFactory userRefreshTokenFactory;

@Autowired
private CustomPasswordEncoder passwordEncoder;

@PostConstruct
public void setAuthNumber() {
this.authNumber = randomNumber.generateRandomNumber();
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 9efed07

Please sign in to comment.