Skip to content

Commit

Permalink
test: #473 인증 & 인가 관련 테스트 코드 리펙토링 (#476)
Browse files Browse the repository at this point in the history
* test: JwtDecoderTest Fixture 추가 및 테스트 케이스 리펙토링

* test: JwtEncoderTest Fixture 추가 및 테스트 케이스 리펙토링

* test: 사용하지 않는 필드 제거와 final 제거

* test: KakaoOauth2TypeTest Fixture 추가 및 테스트 케이스 리펙토링

* test: KakaoUserInformationProviderTest Fixture 추가 및 테스트 케이스 리펙토링

* test: JpaBlackListTokenRepositoryTest Fixture 추가 및 테스트 케이스 리펙토링

* test: AuthenticationServiceFixture Fixture 추가 및 테스트 케이스 리펙토링

* test: AuthenticationUserServiceTest Fixture 추가 및 테스트 케이스 리펙토링

* test: Fixture가 가지고 있던 테스트 관련 어노테이션을 기존 테스트 클래스로 변경

* test: BlackListTokenServiceTest Fixture 추가 및 테스트 케이스 리펙토링

* refactor: 로그인 메서드 네이밍 변경

* test: AuthenticationControllerTest Fixture 추가 및 테스트 케이스 리펙토링

* test: 누락된 테스트 케이스 추가

* test: AuthenticationServiceTest Fixture 및 테스트 케이스 수정

* test: AuthenticationControllerTest Fixture 및 테스트 케이스 수정

* test: KakaoUserInformationProviderTest Fixture 및 테스트 케이스 수정

* test: JpaBlackListTokenRepositoryFixture Fixture 내용 변경

* test: AuthenticationServiceTest 관련 사용하지 않는 Fixture 삭제 및 필드 변경

* test: KakaoUserInformationProviderTest 관련 테스트 대상을 Fixture에서 테스트 클래스로 변경
  • Loading branch information
apptie authored and swonny committed Oct 6, 2023
1 parent cce14b3 commit 5e14721
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 554 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AuthenticationController {
private final BlackListTokenService blackListTokenService;

@PostMapping("/login/{oauth2Type}")
public ResponseEntity<Object> validate(
public ResponseEntity<Object> login(
@PathVariable final Oauth2Type oauth2Type,
@RequestBody final LoginTokenRequest request
) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,62 +1,35 @@
package com.ddang.ddang.authentication.application;

import static org.assertj.core.api.Assertions.assertThat;

import com.ddang.ddang.authentication.application.fixture.AuthenticationUserServiceFixture;
import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.assertj.core.api.Assertions.assertThat;

@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AuthenticationUserServiceTest {

@Autowired
JpaUserRepository userRepository;

class AuthenticationUserServiceTest extends AuthenticationUserServiceFixture {

@Autowired
AuthenticationUserService authenticationUserService;

@Test
void 회원탈퇴한_회원의_id를_전달하면_참을_반환한다() {
// given
final User user = User.builder()
.name("회원")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

user.withdrawal();
userRepository.save(user);

// when
final boolean actual = authenticationUserService.isWithdrawal(user.getId());
final boolean actual = authenticationUserService.isWithdrawal(탈퇴한_사용자.getId());

// then
assertThat(actual).isTrue();
}

@Test
void 회원탈퇴하지_않거나_회원가입하지_않은_회원의_id를_전달하면_거짓을_반환한다() {
// given
final User user = User.builder()
.name("회원")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(user);

// when
final boolean actual = authenticationUserService.isWithdrawal(user.getId());
final boolean actual = authenticationUserService.isWithdrawal(사용자.getId());

// then
assertThat(actual).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.ddang.ddang.authentication.domain.TokenEncoder;
import com.ddang.ddang.authentication.application.fixture.BlackListTokenServiceFixture;
import com.ddang.ddang.authentication.domain.TokenType;
import com.ddang.ddang.authentication.domain.exception.EmptyTokenException;
import com.ddang.ddang.authentication.infrastructure.persistence.JpaBlackListTokenRepository;
import com.ddang.ddang.configuration.IsolateDatabase;
import java.time.LocalDateTime;
import java.util.Map;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -21,28 +18,15 @@
@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class BlackListTokenServiceTest {
class BlackListTokenServiceTest extends BlackListTokenServiceFixture {

@Autowired
BlackListTokenService blackListTokenService;

@Autowired
JpaBlackListTokenRepository blackListTokenRepository;

@Autowired
TokenEncoder tokenEncoder;

@Test
void 유효한_accessToken과_refreshToken을_전달하면_블랙리스트로_등록한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken =
"Bearer " + tokenEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);
final String refreshToken =
"Bearer " + tokenEncoder.encode(LocalDateTime.now(), TokenType.REFRESH, privateClaims);

// when & then
assertDoesNotThrow(() -> blackListTokenService.registerBlackListToken(accessToken, refreshToken));
assertDoesNotThrow(() -> blackListTokenService.registerBlackListToken(유효한_액세스_토큰, 유효한_리프레시_토큰));
}

@ParameterizedTest
Expand All @@ -59,27 +43,17 @@ class BlackListTokenServiceTest {

@Test
void 블랙리스트로_등록된_토큰인지_확인할때_이미_블랙리스트로_등록된_토큰을_전달하면_참을_반환한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = tokenEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);
final String refreshToken = tokenEncoder.encode(LocalDateTime.now(), TokenType.REFRESH, privateClaims);

blackListTokenService.registerBlackListToken(accessToken, refreshToken);

// when
final boolean actual = blackListTokenService.existsBlackListToken(TokenType.ACCESS, accessToken);
final boolean actual = blackListTokenService.existsBlackListToken(TokenType.ACCESS, 만료된_액세스_토큰);

// then
assertThat(actual).isTrue();
}

@Test
void 블랙리스트로_등록된_토큰인지_확인할때_블랙리스트로_등록되지_않은_토큰을_전달하면_거짓을_반환한다() {
// given
final String invalidAccessToken = "invalidAccessToken";

// when
final boolean actual = blackListTokenService.existsBlackListToken(TokenType.ACCESS, invalidAccessToken);
final boolean actual = blackListTokenService.existsBlackListToken(TokenType.ACCESS, 유효한_액세스_토큰);

// then
assertThat(actual).isFalse();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.ddang.ddang.authentication.application.fixture;

import com.ddang.ddang.authentication.domain.TokenEncoder;
import com.ddang.ddang.authentication.domain.TokenType;
import com.ddang.ddang.authentication.domain.dto.UserInformationDto;
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.infrastructure.persistence.JpaProfileImageRepository;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
public class AuthenticationServiceFixture {

protected Oauth2Type 지원하는_소셜_로그인_타입 = Oauth2Type.KAKAO;
protected Oauth2Type 지원하지_않는_소셜_로그인_타입 = Oauth2Type.KAKAO;

protected String 유효한_소셜_로그인_토큰 = "Bearer accessToken";
protected String 만료된_소셜_로그인_토큰;

protected String 디바이스_토큰 = "deviceToken";

protected User 사용자;
protected User 탈퇴한_사용자;
protected User 이미지가_없는_사용자;

protected UserInformationDto 사용자_회원_정보 = new UserInformationDto(12345L);
protected UserInformationDto 탈퇴한_사용자_회원_정보 = new UserInformationDto(54321L);
protected UserInformationDto 가입하지_않은_사용자_회원_정보 = new UserInformationDto(-99999L);

protected String 유효한_액세스_토큰;
protected String 유효하지_않은_액세스_토큰 = "Bearer invalidAccessToken";
protected String 탈퇴한_사용자_액세스_토큰;
protected String 이미지가_없는_사용자_액세스_토큰;
protected String 존재하지_않는_사용자_액세스_토큰;
protected String 유효한_리프레시_토큰;
protected String 만료된_리프레시_토큰;
protected String 유효하지_않은_타입의_리프레시_토큰 = "invalidRefreshToken";

@Autowired
private JpaUserRepository userRepository;

@Autowired
private JpaProfileImageRepository profileImageRepository;

@Autowired
private TokenEncoder tokenEncoder;

@Autowired
private JpaDeviceTokenRepository deviceTokenRepository;

@BeforeEach
void fixtureSetUp() {
profileImageRepository.save(new ProfileImage("default_profile_image.png", "default_profile_image.png"));

사용자 = User.builder()
.name("kakao12345")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(0.0d)
.oauthId("12345")
.build();

탈퇴한_사용자 = User.builder()
.name("kakao12346")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(0.0d)
.oauthId("12346")
.build();

이미지가_없는_사용자 = User.builder()
.name("kakao12347")
.profileImage(null)
.reliability(0.0d)
.oauthId("12347")
.build();

userRepository.save(사용자);
탈퇴한_사용자.withdrawal();
userRepository.save(탈퇴한_사용자);

final DeviceToken deviceToken = new DeviceToken(사용자, 디바이스_토큰);
deviceTokenRepository.save(deviceToken);

유효한_리프레시_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
Map.of("userId", 1L)
);

만료된_리프레시_토큰 = tokenEncoder.encode(
LocalDateTime.ofInstant(Instant.parse("2023-01-01T22:21:20Z"), ZoneId.of("UTC")),
TokenType.REFRESH,
Map.of("userId", 1L)
);

만료된_소셜_로그인_토큰 = tokenEncoder.encode(
Instant.parse("2000-08-10T15:30:00Z").atZone(ZoneId.of("UTC")).toLocalDateTime(),
TokenType.ACCESS,
Map.of("userId", 1L)
);

유효한_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", 1L)
);

탈퇴한_사용자_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", 탈퇴한_사용자.getId())
);

이미지가_없는_사용자_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", 가입하지_않은_사용자_회원_정보.id())
);

존재하지_않는_사용자_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", -99999L)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ddang.ddang.authentication.application.fixture;

import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
public class AuthenticationUserServiceFixture {

@Autowired
private JpaUserRepository userRepository;

protected User 사용자;
protected User 탈퇴한_사용자;

@BeforeEach
void setUp() {
사용자 = User.builder()
.name("kakao12345")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(0.0d)
.oauthId("12345")
.build();

탈퇴한_사용자 = User.builder()
.name("kakao12346")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(0.0d)
.oauthId("12346")
.build();

userRepository.save(사용자);
userRepository.save(탈퇴한_사용자);

탈퇴한_사용자.withdrawal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.ddang.ddang.authentication.application.fixture;

import com.ddang.ddang.authentication.domain.BlackListToken;
import com.ddang.ddang.authentication.domain.TokenEncoder;
import com.ddang.ddang.authentication.domain.TokenType;
import com.ddang.ddang.authentication.infrastructure.persistence.JpaBlackListTokenRepository;
import java.time.LocalDateTime;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
public class BlackListTokenServiceFixture {

@Autowired
private JpaBlackListTokenRepository blackListTokenRepository;

@Autowired
private TokenEncoder tokenEncoder;

protected String 유효한_액세스_토큰;
protected String 유효한_리프레시_토큰;
protected String 만료된_액세스_토큰;
protected String 만료된_리프레시_토큰;

@BeforeEach
void setUp() {
유효한_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", 1L)
);
유효한_리프레시_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
Map.of("userId", 1L)
);
만료된_액세스_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
Map.of("userId", 2L)
);
만료된_리프레시_토큰 = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
Map.of("userId", 2L)
);

blackListTokenRepository.save(new BlackListToken(TokenType.ACCESS, 만료된_액세스_토큰));
blackListTokenRepository.save(new BlackListToken(TokenType.REFRESH, 만료된_리프레시_토큰));
}
}
Loading

0 comments on commit 5e14721

Please sign in to comment.