-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- logout 테스트 - RefreshToken CRUD 테스트
- Loading branch information
Showing
2 changed files
with
210 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 |
---|---|---|
|
@@ -255,4 +255,50 @@ void userSimpleInfoTestWhenInvalidToken() throws Exception { | |
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(400)); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그아웃 실패 테스트 - 잘못된 토큰으로 요청시 예외 발생") | ||
void logoutTestWhenInvalidToken() throws Exception { | ||
String accessToken = "strangeToken"; | ||
String refreshToken = "strangeToken"; | ||
|
||
// when | ||
mockMvc.perform( | ||
get("/auth/logout") | ||
.header("Authorization", "Bearer " + accessToken + " " + refreshToken)) | ||
|
||
// then | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(400)); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그아웃 성공 테스트") | ||
void logoutSuccessTest() throws Exception { | ||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
.role(UserRole.USER) | ||
.email("[email protected]") | ||
.profileImageUrl("https://google.com") | ||
.build(); | ||
User savedUser = userRepository.save(user); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
String accessToken = jwtService.generateAccessToken(map, savedUser); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
|
||
// when | ||
mockMvc.perform(get("/auth/logout") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.header("Authorization", "Bearer " + accessToken + " " + refreshToken)) | ||
// then | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.resCode").value(200)) | ||
.andExpect(jsonPath("$.resObj").value("Logout successfully.")); | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
...ore/auth-api/src/test/java/com/heachi/auth/api/service/token/RefreshTokenServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.heachi.auth.api.service.token; | ||
|
||
import com.heachi.admin.common.exception.ExceptionMessage; | ||
import com.heachi.admin.common.exception.refreshToken.RefreshTokenException; | ||
import com.heachi.auth.api.service.jwt.JwtService; | ||
import com.heachi.mysql.define.user.User; | ||
import com.heachi.mysql.define.user.constant.UserRole; | ||
import com.heachi.mysql.define.user.repository.UserRepository; | ||
import com.heachi.redis.define.refreshToken.RefreshToken; | ||
import com.heachi.redis.define.refreshToken.repository.RefreshTokenRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
import java.lang.reflect.Member; | ||
import java.util.HashMap; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
class RefreshTokenServiceTest { | ||
|
||
@Autowired | ||
private RefreshTokenService refreshTokenService; | ||
|
||
@Autowired | ||
private RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private JwtService jwtService; | ||
|
||
@AfterEach | ||
void tearDown() { | ||
userRepository.deleteAllInBatch(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Refresh Token 저장 & 조회 테스트") | ||
void redisRefreshTokenGenerate() { | ||
// given | ||
RefreshToken saveToken = RefreshToken.builder() | ||
.refreshToken("TestToken") | ||
.email("[email protected]") | ||
.build(); | ||
|
||
refreshTokenService.saveRefreshToken(saveToken); | ||
|
||
// when | ||
Optional<RefreshToken> testToken = refreshTokenRepository.findById("TestToken"); | ||
|
||
// then | ||
assertThat(testToken.get().getRefreshToken()).isEqualTo("TestToken"); | ||
assertThat(testToken.get().getEmail()).isEqualTo("[email protected]"); | ||
} | ||
|
||
|
||
@Test | ||
@DisplayName("Refresh Token 삭제 테스트 - 로그아웃시 Refresh Token이 Redis에서 삭제된다.") | ||
void redisRefreshTokenDeleteWhenLogout() { | ||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
.role(UserRole.USER) | ||
.email("[email protected]") | ||
.profileImageUrl("https://google.com") | ||
.build(); | ||
User savedUser = userRepository.save(user); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
String accessToken = jwtService.generateAccessToken(map, savedUser); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
refreshTokenService.saveRefreshToken(RefreshToken.builder() | ||
.refreshToken(refreshToken) | ||
.email(savedUser.getEmail()) | ||
.build()); | ||
|
||
refreshTokenService.logout(refreshToken); | ||
|
||
// when | ||
Optional<RefreshToken> findToken = refreshTokenRepository.findById(refreshToken); | ||
|
||
// then | ||
assertThat(findToken.isEmpty()).isTrue(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Redis에 존재하지 않는 Refresh Token으로 요청할 경우 예외가 발생한다.") | ||
void LogoutWhenRedisNotExistRefreshToken() { | ||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
.role(UserRole.USER) | ||
.email("[email protected]") | ||
.profileImageUrl("https://google.com") | ||
.build(); | ||
User savedUser = userRepository.save(user); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
refreshTokenService.saveRefreshToken(RefreshToken.builder() | ||
.refreshToken(refreshToken) | ||
.email(savedUser.getEmail()) | ||
.build()); | ||
|
||
// RTK 삭제 | ||
refreshTokenRepository.deleteById(refreshToken); | ||
|
||
// when | ||
RefreshTokenException exception = assertThrows(RefreshTokenException.class, | ||
() -> refreshTokenService.logout(refreshToken)); | ||
|
||
assertThat(exception.getMessage()).isEqualTo(ExceptionMessage.JWT_NOT_EXIST_RTK.getText()); | ||
} | ||
|
||
@Test | ||
@DisplayName("유효하지 않은 Refresh Token으로 요청할 경우 예외가 발생한다.") | ||
void LogoutWhenRedisInvalidRefreshToken() { | ||
// given | ||
User user = User.builder() | ||
.name("김민수") | ||
.role(UserRole.USER) | ||
.email("[email protected]") | ||
.profileImageUrl("https://google.com") | ||
.build(); | ||
User savedUser = userRepository.save(user); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("role", savedUser.getRole().name()); | ||
map.put("name", savedUser.getName()); | ||
map.put("profileImageUrl", savedUser.getProfileImageUrl()); | ||
String refreshToken = jwtService.generateRefreshToken(map, savedUser); | ||
|
||
refreshTokenService.saveRefreshToken(RefreshToken.builder() | ||
.refreshToken(refreshToken) | ||
.email("[email protected]") | ||
.build()); | ||
|
||
refreshTokenRepository.deleteById(refreshToken); | ||
|
||
|
||
// when | ||
RefreshTokenException exception = assertThrows(RefreshTokenException.class, | ||
() -> refreshTokenService.logout(refreshToken)); | ||
|
||
assertThat(exception.getMessage()).isEqualTo(ExceptionMessage.JWT_NOT_EXIST_RTK.getText()); | ||
} | ||
|
||
} |