From 2588651175c116e664041f9bb7afdeaf802b8468 Mon Sep 17 00:00:00 2001 From: birdie Date: Sun, 26 Nov 2023 14:50:24 +0900 Subject: [PATCH] test: user test --- .../yonseigolf/server/user/entity/User.java | 1 - .../server/user/service/UserServiceTest.java | 87 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/main/java/yonseigolf/server/user/entity/User.java b/src/main/java/yonseigolf/server/user/entity/User.java index c965e68..cc76d80 100644 --- a/src/main/java/yonseigolf/server/user/entity/User.java +++ b/src/main/java/yonseigolf/server/user/entity/User.java @@ -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); diff --git a/src/test/java/yonseigolf/server/user/service/UserServiceTest.java b/src/test/java/yonseigolf/server/user/service/UserServiceTest.java index 6743a54..92f45c1 100644 --- a/src/test/java/yonseigolf/server/user/service/UserServiceTest.java +++ b/src/test/java/yonseigolf/server/user/service/UserServiceTest.java @@ -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; @@ -32,6 +36,10 @@ class UserServiceTest { private UserRepository userRepository; @Autowired private UserService userService; + @Autowired + private RefreshTokenRepository refreshTokenRepository; + @Autowired + private JwtService jwtService; @Test @DisplayName("사용자는 회원가입을 할 수 있다.") @@ -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(); } }