-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 13 commits
5e8529e
d04699a
979e1dc
440d2cd
1a7523d
3533136
7932318
870b4d3
99a9a2e
1eeef52
c641416
fbb1fd7
84b6e99
357784d
524069c
737d88b
d5d0221
ef0a0f5
6d1d02b
ba5506e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package com.ddang.ddang.authentication.application.fixture; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import com.ddang.ddang.authentication.application.AuthenticationService; | ||
import com.ddang.ddang.authentication.application.BlackListTokenService; | ||
import com.ddang.ddang.authentication.domain.Oauth2UserInformationProviderComposite; | ||
import com.ddang.ddang.authentication.domain.TokenDecoder; | ||
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.OAuth2UserInformationProvider; | ||
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type; | ||
import com.ddang.ddang.device.application.DeviceTokenService; | ||
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; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
public class AuthenticationServiceFixture { | ||
|
||
@MockBean | ||
protected Oauth2UserInformationProviderComposite 소셜_회원_정보_제공자_묶음; | ||
|
||
@MockBean | ||
protected OAuth2UserInformationProvider 소셜_회원_정보_제공자; | ||
|
||
protected AuthenticationService authenticationService; | ||
protected AuthenticationService profileImageAuthenticationService; | ||
protected JpaProfileImageRepository 프로필_이미지_저장소 = mock(JpaProfileImageRepository.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 이슈는 밑에 Service를 테스트 클래스로 이동시키면서 사라졌고 영어 이름을 사용하는 식으로 진행했습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 계속 제가 Fixture로 뭘 쓸지 헷갈려하고 있는데 일단 스프링 빈으로 등록된건 모조리 제외하는 식으로 하는게 맞겠네요 |
||
|
||
protected Oauth2Type 지원하는_소셜_로그인 = Oauth2Type.KAKAO; | ||
protected Oauth2Type 지원하지_않는_소셜_로그인 = Oauth2Type.KAKAO; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문아래는 지원하지 않는 소셜 로그인인데 왜 동일한 걸까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 선택추가적으로 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
그 이유는 Oauth2Type 필드가 하나밖에 존재하지 않기 때문입니다...null로 표현하는것보다는 그냥 동일하게 표현하는게 낫다고 판단했습니다
이건...좋을 것 같네요 적용하겠습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 하지만, 현재 로직( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 이렇게 한 이유는 로직상 null이 올 수 없기 때문입니다 현재 테스트 케이스에서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 그렇군요! |
||
|
||
protected String 유효한_소셜_로그인_토큰 = "Bearer accessToken"; | ||
protected String 유효하지_않은_소셜_로그인_토큰 = "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"; | ||
|
||
@MockBean | ||
private DeviceTokenService deviceTokenService; | ||
|
||
@Autowired | ||
private JpaUserRepository userRepository; | ||
|
||
@Autowired | ||
private JpaProfileImageRepository profileImageRepository; | ||
|
||
@Autowired | ||
private TokenEncoder tokenEncoder; | ||
|
||
@Autowired | ||
private TokenDecoder tokenDecoder; | ||
|
||
@Autowired | ||
private BlackListTokenService blackListTokenService; | ||
|
||
@Autowired | ||
private JpaDeviceTokenRepository deviceTokenRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
authenticationService = new AuthenticationService( | ||
deviceTokenService, | ||
소셜_회원_정보_제공자_묶음, | ||
userRepository, | ||
profileImageRepository, | ||
tokenEncoder, | ||
tokenDecoder, | ||
blackListTokenService, | ||
deviceTokenRepository | ||
); | ||
|
||
profileImageAuthenticationService = new AuthenticationService( | ||
deviceTokenService, | ||
소셜_회원_정보_제공자_묶음, | ||
userRepository, | ||
프로필_이미지_저장소, | ||
tokenEncoder, | ||
tokenDecoder, | ||
blackListTokenService, | ||
deviceTokenRepository | ||
); | ||
|
||
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(사용자); | ||
userRepository.save(탈퇴한_사용자); | ||
|
||
탈퇴한_사용자.withdrawal(); | ||
|
||
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필수
AuthenticationServiceTest에서는 AuthenticationService가 테스트하려는 객체이므로 픽스처 클래스보다는 테스트 클래스에 있는 것이 더 적절할 것 같습니다!
선택
서비스 테스트만 봤을 때
profileImageAuthenticationService
라는 이름으로는 프로필 이미지를 찾을 수 없는 상황을 가정한 service라는 것을 파악하기 힘들었습니다.네이밍이 좀 길어지긴 하겠지만
profileImageNotFoundAuthenticationService
라던가nullProfileImageAuthenticationService
라든가ProfileImageErrorAuthenticationService
등등 좀 더 상황을 구체적으로 나타내는 네이밍이면 좋을 것 같습니다!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이동했습니다
profileImageNotFoundAuthenticationService로 변경하도록 하겠습니다