Skip to content

Commit

Permalink
test: user test
Browse files Browse the repository at this point in the history
  • Loading branch information
birdieHyun committed Nov 26, 2023
1 parent d3cd16a commit 2588651
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/java/yonseigolf/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void validateRefreshToken(JwtService jwtUtil) {
// refresh token이 없을 경우 발급한다.
if (this.refreshToken == null) {
return;
// throw new RefreshTokenExpiredException("Refresh Token이 존재하지 않습니다.");
}
// refresh token이 만료된 경우 재발급한다.
this.refreshToken.isBeforeExpired(jwtUtil);
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/yonseigolf/server/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import yonseigolf.server.user.dto.response.AdminResponse;
import yonseigolf.server.user.dto.response.LoggedInUser;
import yonseigolf.server.user.dto.response.SingleUserResponse;
import yonseigolf.server.user.entity.RefreshToken;
import yonseigolf.server.user.entity.User;
import yonseigolf.server.user.entity.UserClass;
import yonseigolf.server.user.entity.UserRole;
import yonseigolf.server.user.repository.RefreshTokenRepository;
import yonseigolf.server.user.repository.UserRepository;

import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
Expand All @@ -32,6 +36,10 @@ class UserServiceTest {
private UserRepository userRepository;
@Autowired
private UserService userService;
@Autowired
private RefreshTokenRepository refreshTokenRepository;
@Autowired
private JwtService jwtService;

@Test
@DisplayName("사용자는 회원가입을 할 수 있다.")
Expand Down Expand Up @@ -191,6 +199,85 @@ void notExistingUserExceptionTest() {
assertThatThrownBy(() -> userService.updateUserClass(notExistingUserId, UserClass.OB))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("존재하지 않는 유저입니다.");
}

@Test
@DisplayName("refresh token에 이상이 없는 경우 예외가 발생하지 않는다.")
void noThrowRefreshTokenTest() {
// given
User user = User.builder()
.kakaoId(1L)
.name("이름")
.phoneNumber("010-1234-5678")
.studentId(1)
.major("컴퓨터과학과")
.semester(1)
.role(UserRole.MEMBER)
.userClass(UserClass.OB)
.build();
User savedUser = userRepository.save(user);

userService.saveRefreshToken(savedUser.getId(),"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsb2dpbl9tZW1iZXIiLCJ1c2VyUHJvZmlsZSI6eyJpZCI6MzUsIm5hbWUiOiLsnoTrj5ntmIQiLCJhZG1pblN0YXR1cyI6dHJ1ZSwibWVtYmVyU3RhdHVzIjp0cnVlfSwiZXhwIjoxNzAwOTc5MjU5fQ.MegKm0Oj7wYcKMtanyLqt0x5L4X7VC_tfBwJvfGSG1c");


// when & then
userService.validateRefreshToken(savedUser.getId(), jwtService);
}

@Test
@DisplayName("refresh token validate test")
void validateRefreshTokenTest() {
// given
RefreshToken refreshToken = RefreshToken
.builder()
.refreshToken("refreshToken")
.build();
RefreshToken savedRefreshToken = refreshTokenRepository.save(refreshToken);

User user = User.builder()
.kakaoId(1L)
.name("이름")
.phoneNumber("010-1234-5678")
.studentId(1)
.major("컴퓨터과학과")
.semester(1)
.role(UserRole.MEMBER)
.userClass(UserClass.OB)
.refreshToken(savedRefreshToken)
.build();
User savedUser = userRepository.save(user);

// when
userService.invalidateRefreshToken(savedUser.getId());
User findUser = userRepository.findById(savedUser.getId()).orElseGet(() -> User.builder().build());

// then
assertThat(findUser.getRefreshToken()).isNull();
}

@Test
@DisplayName("refresh token을 통해 access token을 재생성 할 수 있다.")
void refreshAccessTokenTest() {
// given
User user = User.builder()
.kakaoId(1L)
.name("이름")
.phoneNumber("010-1234-5678")
.studentId(1)
.major("컴퓨터과학과")
.semester(1)
.role(UserRole.MEMBER)
.userClass(UserClass.OB)
.build();
User savedUser = userRepository.save(user);
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsb2dpbl9tZW1iZXIiLCJ1c2VyUHJvZmlsZSI6eyJpZCI6MzUsIm5hbWUiOiLsnoTrj5ntmIQiLCJhZG1pblN0YXR1cyI6dHJ1ZSwibWVtYmVyU3RhdHVzIjp0cnVlfSwiZXhwIjoxNzAwOTc5MjU5fQ.MegKm0Oj7wYcKMtanyLqt0x5L4X7VC_tfBwJvfGSG1c";

userService.saveRefreshToken(savedUser.getId(), token);

// when
String accessToken = userService.generateAccessToken(savedUser.getId(), jwtService, new Date(new Date().getTime() + 1800000));

// then
assertThat(accessToken).isNotNull();
}
}

0 comments on commit 2588651

Please sign in to comment.