Skip to content

Commit

Permalink
test(#45): RefreshTokenService 테스트
Browse files Browse the repository at this point in the history
- logout 테스트
- RefreshToken CRUD 테스트
  • Loading branch information
jusung-c committed Sep 20, 2023
1 parent ec944db commit c9a0b67
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."));
}
}
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());
}

}

0 comments on commit c9a0b67

Please sign in to comment.