diff --git a/backend/src/main/java/com/festago/auth/application/OAuth2Client.java b/backend/src/main/java/com/festago/auth/application/OAuth2Client.java index 597cba378..ad2d3fad4 100644 --- a/backend/src/main/java/com/festago/auth/application/OAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/application/OAuth2Client.java @@ -5,9 +5,7 @@ public interface OAuth2Client { - String getAccessToken(String code); - - UserInfo getUserInfo(String accessToken); + UserInfo getUserInfo(String code); SocialType getSocialType(); } diff --git a/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java b/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java index 3980691f3..f028d3b99 100644 --- a/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java +++ b/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java @@ -28,8 +28,7 @@ public class MemberAuthFacadeService { public LoginV1Response oAuth2Login(SocialType socialType, String code) { OAuth2Client oAuth2Client = oAuth2Clients.getClient(socialType); - String oAuth2AccessToken = oAuth2Client.getAccessToken(code); - UserInfo userInfo = oAuth2Client.getUserInfo(oAuth2AccessToken); + UserInfo userInfo = oAuth2Client.getUserInfo(code); return login(userInfo); } diff --git a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java b/backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java similarity index 63% rename from backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java rename to backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java index 3fbfe7585..e1a44b028 100644 --- a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java +++ b/backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java @@ -4,12 +4,9 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) -public record KakaoAccessTokenResponse( - String tokenType, +public record KakaoOAuth2TokenResponse( String accessToken, - Integer expiresIn, - String refreshToken, - Integer refreshTokenExpiresIn + String idToken ) { } diff --git a/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java b/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java deleted file mode 100644 index e82122847..000000000 --- a/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.festago.auth.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.festago.auth.domain.SocialType; -import com.festago.auth.domain.UserInfo; - -public record KakaoUserInfo( - String id, - @JsonProperty("kakao_account") KakaoAccount kakaoAccount -) { - - public UserInfo toUserInfo() { - return UserInfo.builder() - .socialId(id) - .socialType(SocialType.KAKAO) - .nickname(kakaoAccount.profile.nickname) - .profileImage(kakaoAccount.profile.thumbnailImageUrl) - .build(); - } - - public record KakaoAccount( - Profile profile - ) { - - public record Profile( - String nickname, - @JsonProperty("thumbnail_image_url") - String thumbnailImageUrl - ) { - - } - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java index 1470b330e..8010da157 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java @@ -7,7 +7,6 @@ import com.festago.common.exception.ErrorCode; import java.util.HashMap; import java.util.Map; -import java.util.function.Supplier; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @@ -15,26 +14,21 @@ @Profile("!prod") public class FestagoOAuth2Client implements OAuth2Client { - private static final String PROFILE_IMAGE = "https://placehold.co/150x150"; - - private final Map> userInfoMap = new HashMap<>(); + private final Map userInfoMap = new HashMap<>(); public FestagoOAuth2Client() { - userInfoMap.put("1", () -> new UserInfo("1", getSocialType(), "member1", PROFILE_IMAGE)); - userInfoMap.put("2", () -> new UserInfo("2", getSocialType(), "member2", PROFILE_IMAGE)); - userInfoMap.put("3", () -> new UserInfo("3", getSocialType(), "member3", PROFILE_IMAGE)); - } - - @Override - public String getAccessToken(String code) { - return code; + userInfoMap.put("1", new UserInfo("1", getSocialType(), "member1", null)); + userInfoMap.put("2", new UserInfo("2", getSocialType(), "member2", null)); + userInfoMap.put("3", new UserInfo("3", getSocialType(), "member3", null)); } @Override - public UserInfo getUserInfo(String accessToken) { - return userInfoMap.getOrDefault(accessToken, () -> { + public UserInfo getUserInfo(String code) { + UserInfo userInfo = userInfoMap.get(code); + if (userInfo == null) { throw new BadRequestException(ErrorCode.OAUTH2_INVALID_TOKEN); - }).get(); + } + return userInfo; } @Override diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java index 36f64c87a..3421faf25 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java @@ -3,6 +3,7 @@ import com.festago.auth.application.OAuth2Client; import com.festago.auth.domain.SocialType; import com.festago.auth.domain.UserInfo; +import com.festago.auth.infrastructure.openid.KakaoOpenIdClient; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -10,17 +11,13 @@ @RequiredArgsConstructor public class KakaoOAuth2Client implements OAuth2Client { - private final KakaoOAuth2AccessTokenClient accessTokenClient; - private final KakaoOAuth2UserInfoClient userInfoClient; + private final KakaoOAuth2TokenClient kakaoOAuth2TokenClient; + private final KakaoOpenIdClient kakaoOpenIdClient; @Override - public String getAccessToken(String code) { - return accessTokenClient.getAccessToken(code); - } - - @Override - public UserInfo getUserInfo(String accessToken) { - return userInfoClient.getUserInfo(accessToken); + public UserInfo getUserInfo(String code) { + String idToken = kakaoOAuth2TokenClient.getIdToken(code); + return kakaoOpenIdClient.getUserInfo(idToken); } @Override diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java similarity index 73% rename from backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java rename to backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java index 66c074b54..59ea6a145 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java @@ -1,6 +1,6 @@ package com.festago.auth.infrastructure.oauth2; -import com.festago.auth.dto.KakaoAccessTokenResponse; +import com.festago.auth.dto.KakaoOAuth2TokenResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpHeaders; @@ -9,7 +9,7 @@ import org.springframework.web.client.RestTemplate; @Component -public class KakaoOAuth2AccessTokenClient { +public class KakaoOAuth2TokenClient { private static final String URL = "https://kauth.kakao.com/oauth/token"; @@ -19,7 +19,7 @@ public class KakaoOAuth2AccessTokenClient { private final String redirectUri; private final String clientSecret; - public KakaoOAuth2AccessTokenClient( + public KakaoOAuth2TokenClient( @Value("${festago.oauth2.kakao.grant-type}") String grantType, @Value("${festago.oauth2.kakao.rest-api-key}") String clientId, @Value("${festago.oauth2.kakao.redirect-uri}") String redirectUri, @@ -35,12 +35,13 @@ public KakaoOAuth2AccessTokenClient( .build(); } - public String getAccessToken(String code) { - HttpHeaders headers = getAccessTokenHeaders(code); - return requestAccessToken(headers); + public String getIdToken(String code) { + KakaoOAuth2TokenResponse response = restTemplate.postForEntity(URL, getHeaders(code), + KakaoOAuth2TokenResponse.class).getBody(); + return response.idToken(); } - private HttpHeaders getAccessTokenHeaders(String code) { + private HttpHeaders getHeaders(String code) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.set("grant_type", grantType); @@ -50,10 +51,4 @@ private HttpHeaders getAccessTokenHeaders(String code) { headers.set("code", code); return headers; } - - private String requestAccessToken(HttpHeaders headers) { - KakaoAccessTokenResponse response = restTemplate.postForEntity(URL, headers, - KakaoAccessTokenResponse.class).getBody(); - return response.accessToken(); - } } diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java deleted file mode 100644 index a725e5d92..000000000 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.festago.auth.infrastructure.oauth2; - -import com.festago.auth.domain.UserInfo; -import com.festago.auth.dto.KakaoUserInfo; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class KakaoOAuth2UserInfoClient { - - private static final String URL = "https://kapi.kakao.com/v2/user/me"; - - private final RestTemplate restTemplate; - - public KakaoOAuth2UserInfoClient(RestTemplateBuilder restTemplateBuilder) { - this.restTemplate = restTemplateBuilder - .errorHandler(new KakaoOAuth2UserInfoErrorHandler()) - .build(); - } - - public UserInfo getUserInfo(String accessToken) { - HttpHeaders headers = getUserInfoHeaders(accessToken); - return requestUserInfo(headers); - } - - private HttpHeaders getUserInfoHeaders(String accessToken) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.setBearerAuth(accessToken); - return headers; - } - - private UserInfo requestUserInfo(HttpHeaders headers) { - KakaoUserInfo kakaoUserInfo = restTemplate.postForEntity(URL, new HttpEntity<>(headers), - KakaoUserInfo.class) - .getBody(); - return kakaoUserInfo.toUserInfo(); - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java deleted file mode 100644 index 74e139bf6..000000000 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.festago.auth.infrastructure.oauth2; - -import com.festago.common.exception.BadRequestException; -import com.festago.common.exception.ErrorCode; -import com.festago.common.exception.InternalServerException; -import java.io.IOException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatusCode; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.web.client.DefaultResponseErrorHandler; - -@Slf4j -public class KakaoOAuth2UserInfoErrorHandler extends DefaultResponseErrorHandler { - - @Override - public void handleError(ClientHttpResponse response) throws IOException { - HttpStatusCode statusCode = response.getStatusCode(); - handle4xxError(statusCode); - handle5xxError(statusCode); - log.error("카카오 OAuth2 요청 중 알 수 없는 문제가 발생했습니다."); - throw new InternalServerException(ErrorCode.INTERNAL_SERVER_ERROR); - } - - private void handle4xxError(HttpStatusCode statusCode) { - if (statusCode.is4xxClientError()) { - throw new BadRequestException(ErrorCode.OAUTH2_INVALID_TOKEN); - } - } - - private void handle5xxError(HttpStatusCode statusCode) { - if (statusCode.is5xxServerError()) { - log.warn("카카오 OAuth2 요청에 500 에러가 발생했습니다."); - throw new InternalServerException(ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE); - } - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java b/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java index 09e1c4cd1..dffcb064c 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java @@ -48,8 +48,6 @@ public UserInfo getUserInfo(String idToken) { return UserInfo.builder() .socialType(SocialType.KAKAO) .socialId(payload.getSubject()) - .nickname(payload.get("nickname", String.class)) - .profileImage(payload.get("picture", String.class)) .build(); } diff --git a/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java b/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java index dea7fe991..7390080d5 100644 --- a/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java +++ b/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java @@ -9,9 +9,7 @@ import com.festago.auth.application.OAuth2Clients; import com.festago.auth.application.OAuth2Clients.OAuth2ClientsBuilder; import com.festago.auth.infrastructure.oauth2.FestagoOAuth2Client; -import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenClient; import com.festago.auth.infrastructure.oauth2.KakaoOAuth2Client; -import com.festago.auth.infrastructure.oauth2.KakaoOAuth2UserInfoClient; import com.festago.common.exception.BadRequestException; import com.festago.common.exception.UnexpectedException; import org.junit.jupiter.api.DisplayNameGeneration; @@ -53,10 +51,7 @@ class OAuth2ClientsTest { void 여러_타입의_클라이언트가_주어졌을때_타입으로_찾기_성공() { // given FestagoOAuth2Client festagoOAuth2Client = new FestagoOAuth2Client(); - KakaoOAuth2Client kakaoOAuth2Client = new KakaoOAuth2Client( - mock(KakaoOAuth2AccessTokenClient.class), - mock(KakaoOAuth2UserInfoClient.class) - ); + KakaoOAuth2Client kakaoOAuth2Client = new KakaoOAuth2Client(mock(), mock()); OAuth2Clients oAuth2Clients = OAuth2Clients.builder() .add(festagoOAuth2Client) diff --git a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java similarity index 81% rename from backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java rename to backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java index fd9b99077..05b1125b7 100644 --- a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java +++ b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java @@ -6,8 +6,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.festago.auth.dto.KakaoAccessTokenResponse; -import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenClient; +import com.festago.auth.dto.KakaoOAuth2TokenResponse; +import com.festago.auth.infrastructure.oauth2.KakaoOAuth2TokenClient; import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenErrorHandler.KakaoOAuth2ErrorResponse; import com.festago.common.exception.BadRequestException; import com.festago.common.exception.ErrorCode; @@ -24,13 +24,13 @@ @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") -@RestClientTest(KakaoOAuth2AccessTokenClient.class) -class KakaoOAuth2AccessTokenClientTest { +@RestClientTest(KakaoOAuth2TokenClient.class) +class KakaoOAuth2TokenClientTest { private static final String URL = "https://kauth.kakao.com/oauth/token"; @Autowired - KakaoOAuth2AccessTokenClient kakaoOAuth2AccessTokenClient; + KakaoOAuth2TokenClient kakaoOAuth2TokenClient; @Autowired MockRestServiceServer mockServer; @@ -48,7 +48,7 @@ class KakaoOAuth2AccessTokenClientTest { .body(objectMapper.writeValueAsString(expected))); // when & then - assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code")) + assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code")) .isInstanceOf(BadRequestException.class) .hasMessage(ErrorCode.OAUTH2_INVALID_CODE.getMessage()); } @@ -63,7 +63,7 @@ class KakaoOAuth2AccessTokenClientTest { .body(objectMapper.writeValueAsString(expected))); // when & then - assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code")) + assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code")) .isInstanceOf(InternalServerException.class) .hasMessage(ErrorCode.OAUTH2_INVALID_REQUEST.getMessage()); } @@ -76,7 +76,7 @@ class KakaoOAuth2AccessTokenClientTest { .contentType(MediaType.APPLICATION_JSON)); // when & then - assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code")) + assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code")) .isInstanceOf(InternalServerException.class) .hasMessage(ErrorCode.OAUTH2_INVALID_REQUEST.getMessage()); } @@ -89,7 +89,7 @@ class KakaoOAuth2AccessTokenClientTest { .contentType(MediaType.APPLICATION_JSON)); // when & then - assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code")) + assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code")) .isInstanceOf(InternalServerException.class) .hasMessage(ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE.getMessage()); } @@ -97,16 +97,15 @@ class KakaoOAuth2AccessTokenClientTest { @Test void 성공() throws JsonProcessingException { // given - KakaoAccessTokenResponse expected = new KakaoAccessTokenResponse("tokenType", "accessToken", - 100, "refreshToken", 50); + KakaoOAuth2TokenResponse expected = new KakaoOAuth2TokenResponse("accessToken", "idToken"); mockServer.expect(requestTo(URL)) .andRespond(MockRestResponseCreators.withSuccess() .contentType(MediaType.APPLICATION_JSON) .body(objectMapper.writeValueAsString(expected))); // when - String actual = kakaoOAuth2AccessTokenClient.getAccessToken("code"); + String actual = kakaoOAuth2TokenClient.getIdToken("code"); // then - assertThat(actual).isEqualTo(expected.accessToken()); + assertThat(actual).isEqualTo(expected.idToken()); } } diff --git a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java deleted file mode 100644 index 73b459a5e..000000000 --- a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.festago.auth.infrastructure; - -import static com.festago.common.exception.ErrorCode.OAUTH2_INVALID_TOKEN; -import static com.festago.common.exception.ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.SoftAssertions.assertSoftly; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.festago.auth.domain.SocialType; -import com.festago.auth.domain.UserInfo; -import com.festago.auth.dto.KakaoUserInfo; -import com.festago.auth.dto.KakaoUserInfo.KakaoAccount; -import com.festago.auth.dto.KakaoUserInfo.KakaoAccount.Profile; -import com.festago.auth.infrastructure.oauth2.KakaoOAuth2UserInfoClient; -import com.festago.common.exception.BadRequestException; -import com.festago.common.exception.InternalServerException; -import org.junit.jupiter.api.DisplayNameGeneration; -import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.test.web.client.MockRestServiceServer; -import org.springframework.test.web.client.response.MockRestResponseCreators; - -@DisplayNameGeneration(ReplaceUnderscores.class) -@SuppressWarnings("NonAsciiCharacters") -@RestClientTest(KakaoOAuth2UserInfoClient.class) -class KakaoOAuth2UserInfoClientTest { - - private static final String URL = "https://kapi.kakao.com/v2/user/me"; - - @Autowired - KakaoOAuth2UserInfoClient kakaoOAuth2UserInfoClient; - - @Autowired - MockRestServiceServer mockServer; - - @Autowired - ObjectMapper objectMapper; - - @Test - void 상태코드가_400이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withBadRequest() - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(BadRequestException.class) - .hasMessage(OAUTH2_INVALID_TOKEN.getMessage()); - } - - @Test - void 상태코드가_401이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withUnauthorizedRequest() - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(BadRequestException.class) - .hasMessage(OAUTH2_INVALID_TOKEN.getMessage()); - } - - @Test - void 상태코드가_5xx이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withStatus(HttpStatus.INTERNAL_SERVER_ERROR) - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(InternalServerException.class) - .hasMessage(OAUTH2_PROVIDER_NOT_RESPONSE.getMessage()); - } - - @Test - void 성공() throws JsonProcessingException { - // given - KakaoUserInfo expected = new KakaoUserInfo("id", new KakaoAccount(new Profile("nickname", "imageUrl"))); - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withSuccess() - .body(objectMapper.writeValueAsString(expected)) - .contentType(MediaType.APPLICATION_JSON)); - - // when - UserInfo actual = kakaoOAuth2UserInfoClient.getUserInfo("accessToken"); - - // then - assertSoftly(softly -> { - softly.assertThat(actual.socialType()).isEqualTo(SocialType.KAKAO); - softly.assertThat(actual.socialId()).isEqualTo(expected.id()); - softly.assertThat(actual.nickname()).isEqualTo(expected.kakaoAccount().profile().nickname()); - softly.assertThat(actual.profileImage()) - .isEqualTo(expected.kakaoAccount().profile().thumbnailImageUrl()); - }); - } -}