From 6dfe2ad9fd38ba73634c7e7e842c210040b3201c Mon Sep 17 00:00:00 2001 From: MooSong Lee <46563149+fromitive@users.noreply.github.com> Date: Sat, 18 Jan 2025 07:37:09 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC?= =?UTF-8?q?=EB=8F=99=20=EC=8B=9C=20=EC=9D=B8=EC=A6=9D=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EA=B4=80=EB=A0=A8=20=EB=B9=88=20=EC=A3=BC=EC=9E=85=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0=20(#694)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/config/AuthClientConfig.java | 5 ++- .../chongdae/auth/service/AuthClient.java | 29 +-------------- .../chongdae/auth/service/AuthService.java | 2 +- .../service/{ => client}/DevAuthClient.java | 6 +-- .../auth/service/client/ProdAuthClient.java | 37 +++++++++++++++++++ .../auth/config/TestAuthClientConfig.java | 15 ++++++++ .../auth/integration/AuthIntegrationTest.java | 2 +- .../chongdae/auth/service/StubAuthClient.java | 9 +++++ .../chongdae/global/config/TestConfig.java | 4 +- .../member/service/NicknameGeneratorTest.java | 4 +- 10 files changed, 77 insertions(+), 36 deletions(-) rename backend/src/main/java/com/zzang/chongdae/auth/service/{ => client}/DevAuthClient.java (69%) create mode 100644 backend/src/main/java/com/zzang/chongdae/auth/service/client/ProdAuthClient.java create mode 100644 backend/src/test/java/com/zzang/chongdae/auth/config/TestAuthClientConfig.java create mode 100644 backend/src/test/java/com/zzang/chongdae/auth/service/StubAuthClient.java diff --git a/backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientConfig.java b/backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientConfig.java index 70d519c99..8968566eb 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientConfig.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientConfig.java @@ -1,7 +1,8 @@ package com.zzang.chongdae.auth.config; import com.zzang.chongdae.auth.service.AuthClient; -import com.zzang.chongdae.auth.service.DevAuthClient; +import com.zzang.chongdae.auth.service.client.DevAuthClient; +import com.zzang.chongdae.auth.service.client.ProdAuthClient; import java.time.Duration; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -26,7 +27,7 @@ public class AuthClientConfig { @Bean @Profile("prod") public AuthClient prodAuthClient() { - return new AuthClient(createRestClient()); + return new ProdAuthClient(createRestClient()); } @Bean diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java index af9ad6166..4c13bd417 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java @@ -1,31 +1,6 @@ package com.zzang.chongdae.auth.service; -import com.zzang.chongdae.auth.exception.KakaoLoginExceptionHandler; -import com.zzang.chongdae.auth.service.dto.KakaoLoginResponseDto; -import com.zzang.chongdae.member.domain.AuthProvider; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpHeaders; -import org.springframework.web.client.RestClient; +public interface AuthClient { -@RequiredArgsConstructor -public class AuthClient { - - private static final String BEARER_HEADER_FORMAT = "Bearer %s"; - private static final String GET_KAKAO_USER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; - - private final RestClient restClient; - - public String getKakaoUserInfo(String accessToken) { - KakaoLoginResponseDto responseDto = restClient.get() - .uri(GET_KAKAO_USER_INFO_URI) - .header(HttpHeaders.AUTHORIZATION, createAuthorization(accessToken)) - .retrieve() - .onStatus(new KakaoLoginExceptionHandler()) - .body(KakaoLoginResponseDto.class); - return AuthProvider.KAKAO.buildLoginId(responseDto.id().toString()); // TODO: NPE 처리 고려하기 - } - - private String createAuthorization(String accessToken) { - return BEARER_HEADER_FORMAT.formatted(accessToken); - } + String getUserInfo(String accessToken); } diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java index c7071a565..1c9993d02 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/AuthService.java @@ -34,7 +34,7 @@ public class AuthService { @WriterDatabase @Transactional public AuthInfoDto kakaoLogin(KakaoLoginRequest request) { - String loginId = authClient.getKakaoUserInfo(request.accessToken()); + String loginId = authClient.getUserInfo(request.accessToken()); AuthProvider provider = AuthProvider.KAKAO; MemberEntity member = memberRepository.findByLoginId(loginId) .orElseGet(() -> signup(provider, loginId, request.fcmToken())); diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/DevAuthClient.java b/backend/src/main/java/com/zzang/chongdae/auth/service/client/DevAuthClient.java similarity index 69% rename from backend/src/main/java/com/zzang/chongdae/auth/service/DevAuthClient.java rename to backend/src/main/java/com/zzang/chongdae/auth/service/client/DevAuthClient.java index 7baffe052..e0ec0ae08 100644 --- a/backend/src/main/java/com/zzang/chongdae/auth/service/DevAuthClient.java +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/client/DevAuthClient.java @@ -1,16 +1,16 @@ -package com.zzang.chongdae.auth.service; +package com.zzang.chongdae.auth.service.client; import com.zzang.chongdae.global.exception.MarketException; import org.springframework.web.client.RestClient; -public class DevAuthClient extends AuthClient { +public class DevAuthClient extends ProdAuthClient { public DevAuthClient(RestClient restClient) { super(restClient); } @Override - public String getKakaoUserInfo(String accessToken) { + protected String getKakaoUserInfo(String accessToken) { try { return super.getKakaoUserInfo(accessToken); } catch (MarketException e) { diff --git a/backend/src/main/java/com/zzang/chongdae/auth/service/client/ProdAuthClient.java b/backend/src/main/java/com/zzang/chongdae/auth/service/client/ProdAuthClient.java new file mode 100644 index 000000000..5fa880a83 --- /dev/null +++ b/backend/src/main/java/com/zzang/chongdae/auth/service/client/ProdAuthClient.java @@ -0,0 +1,37 @@ +package com.zzang.chongdae.auth.service.client; + +import com.zzang.chongdae.auth.exception.KakaoLoginExceptionHandler; +import com.zzang.chongdae.auth.service.AuthClient; +import com.zzang.chongdae.auth.service.dto.KakaoLoginResponseDto; +import com.zzang.chongdae.member.domain.AuthProvider; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpHeaders; +import org.springframework.web.client.RestClient; + +@RequiredArgsConstructor +public class ProdAuthClient implements AuthClient { + + private static final String BEARER_HEADER_FORMAT = "Bearer %s"; + private static final String GET_KAKAO_USER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; + + private final RestClient restClient; + + @Override + public String getUserInfo(String accessToken) { + return getKakaoUserInfo(accessToken); + } + + protected String getKakaoUserInfo(String accessToken) { + KakaoLoginResponseDto responseDto = restClient.get() + .uri(GET_KAKAO_USER_INFO_URI) + .header(HttpHeaders.AUTHORIZATION, createAuthorization(accessToken)) + .retrieve() + .onStatus(new KakaoLoginExceptionHandler()) + .body(KakaoLoginResponseDto.class); + return AuthProvider.KAKAO.buildLoginId(responseDto.id().toString()); // TODO: NPE 처리 고려하기 + } + + private String createAuthorization(String accessToken) { + return BEARER_HEADER_FORMAT.formatted(accessToken); + } +} diff --git a/backend/src/test/java/com/zzang/chongdae/auth/config/TestAuthClientConfig.java b/backend/src/test/java/com/zzang/chongdae/auth/config/TestAuthClientConfig.java new file mode 100644 index 000000000..d9f216027 --- /dev/null +++ b/backend/src/test/java/com/zzang/chongdae/auth/config/TestAuthClientConfig.java @@ -0,0 +1,15 @@ +package com.zzang.chongdae.auth.config; + +import com.zzang.chongdae.auth.service.AuthClient; +import com.zzang.chongdae.auth.service.StubAuthClient; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +@TestConfiguration +public class TestAuthClientConfig { + + @Bean + AuthClient testAuthConfig() { + return new StubAuthClient(); + } +} diff --git a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java index e16e34d5d..ae6ad3df7 100644 --- a/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java +++ b/backend/src/test/java/com/zzang/chongdae/auth/integration/AuthIntegrationTest.java @@ -69,7 +69,7 @@ class KakaoLogin { @BeforeEach void setUp() { member = memberFixture.createMember("dora"); - BDDMockito.given(authClient.getKakaoUserInfo(any())) + BDDMockito.given(authClient.getUserInfo(any())) .willReturn(member.getLoginId()); } diff --git a/backend/src/test/java/com/zzang/chongdae/auth/service/StubAuthClient.java b/backend/src/test/java/com/zzang/chongdae/auth/service/StubAuthClient.java new file mode 100644 index 000000000..f14f11044 --- /dev/null +++ b/backend/src/test/java/com/zzang/chongdae/auth/service/StubAuthClient.java @@ -0,0 +1,9 @@ +package com.zzang.chongdae.auth.service; + +public class StubAuthClient implements AuthClient { + + @Override + public String getUserInfo(String accessToken) { + return "stubUserInfo"; + } +} diff --git a/backend/src/test/java/com/zzang/chongdae/global/config/TestConfig.java b/backend/src/test/java/com/zzang/chongdae/global/config/TestConfig.java index c8591a2e4..c1dc1a43f 100644 --- a/backend/src/test/java/com/zzang/chongdae/global/config/TestConfig.java +++ b/backend/src/test/java/com/zzang/chongdae/global/config/TestConfig.java @@ -1,5 +1,6 @@ package com.zzang.chongdae.global.config; +import com.zzang.chongdae.auth.config.TestAuthClientConfig; import com.zzang.chongdae.member.config.TestNicknameWordPickerConfig; import com.zzang.chongdae.notification.config.TestNotificationConfig; import com.zzang.chongdae.offering.config.TestCrawlerConfig; @@ -11,7 +12,8 @@ TestNicknameWordPickerConfig.class, TestClockConfig.class, TestNotificationConfig.class, - TestStorageConfig.class}) + TestStorageConfig.class, + TestAuthClientConfig.class}) @TestConfiguration public class TestConfig { } diff --git a/backend/src/test/java/com/zzang/chongdae/member/service/NicknameGeneratorTest.java b/backend/src/test/java/com/zzang/chongdae/member/service/NicknameGeneratorTest.java index 156c582de..e35a25e95 100644 --- a/backend/src/test/java/com/zzang/chongdae/member/service/NicknameGeneratorTest.java +++ b/backend/src/test/java/com/zzang/chongdae/member/service/NicknameGeneratorTest.java @@ -1,5 +1,6 @@ package com.zzang.chongdae.member.service; +import com.zzang.chongdae.auth.config.TestAuthClientConfig; import com.zzang.chongdae.member.config.TestNicknameWordPickerConfig; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -10,7 +11,8 @@ import org.springframework.test.context.ActiveProfiles; @ActiveProfiles("test") -@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {TestNicknameWordPickerConfig.class}) +@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {TestNicknameWordPickerConfig.class, + TestAuthClientConfig.class}) public class NicknameGeneratorTest { @Autowired