Skip to content

Commit

Permalink
fix: 테스트 구동 시 인증 설정 관련 빈 주입 오류 해결 (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
fromitive authored Jan 17, 2025
1 parent 78c8101 commit 6dfe2ad
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +27,7 @@ public class AuthClientConfig {
@Bean
@Profile("prod")
public AuthClient prodAuthClient() {
return new AuthClient(createRestClient());
return new ProdAuthClient(createRestClient());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zzang.chongdae.auth.service;

public class StubAuthClient implements AuthClient {

@Override
public String getUserInfo(String accessToken) {
return "stubUserInfo";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +12,8 @@
TestNicknameWordPickerConfig.class,
TestClockConfig.class,
TestNotificationConfig.class,
TestStorageConfig.class})
TestStorageConfig.class,
TestAuthClientConfig.class})
@TestConfiguration
public class TestConfig {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 6dfe2ad

Please sign in to comment.