Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

인증 & 인가 관련 테스트 코드 리펙토링 #476

Merged
merged 20 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5e8529e
test: JwtDecoderTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
d04699a
test: JwtEncoderTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
979e1dc
test: 사용하지 않는 필드 제거와 final 제거
apptie Sep 28, 2023
440d2cd
test: KakaoOauth2TypeTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
1a7523d
test: KakaoUserInformationProviderTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
3533136
test: JpaBlackListTokenRepositoryTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
7932318
test: AuthenticationServiceFixture Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 28, 2023
870b4d3
test: AuthenticationUserServiceTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 29, 2023
99a9a2e
test: Fixture가 가지고 있던 테스트 관련 어노테이션을 기존 테스트 클래스로 변경
apptie Sep 29, 2023
1eeef52
test: BlackListTokenServiceTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 29, 2023
c641416
refactor: 로그인 메서드 네이밍 변경
apptie Sep 29, 2023
fbb1fd7
test: AuthenticationControllerTest Fixture 추가 및 테스트 케이스 리펙토링
apptie Sep 29, 2023
84b6e99
test: 누락된 테스트 케이스 추가
apptie Sep 29, 2023
357784d
test: AuthenticationServiceTest Fixture 및 테스트 케이스 수정
apptie Sep 29, 2023
524069c
test: AuthenticationControllerTest Fixture 및 테스트 케이스 수정
apptie Sep 29, 2023
737d88b
test: KakaoUserInformationProviderTest Fixture 및 테스트 케이스 수정
apptie Sep 29, 2023
d5d0221
test: JpaBlackListTokenRepositoryFixture Fixture 내용 변경
apptie Sep 29, 2023
ef0a0f5
test: AuthenticationServiceTest 관련 사용하지 않는 Fixture 삭제 및 필드 변경
apptie Sep 29, 2023
6d1d02b
test: KakaoUserInformationProviderTest 관련 테스트 대상을 Fixture에서 테스트 클래스로 변경
apptie Sep 30, 2023
ba5506e
ci: 브랜치 최신화
apptie Sep 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading