From b36cf3fd90312bd3f16818c3c674b04b5ce5860f Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 17:05:20 +0900 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20=EC=BB=A4=EB=B2=84=EB=A6=AC?= =?UTF-8?q?=EC=A7=80=20=EC=A0=9C=EC=99=B8=ED=95=A0=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=ED=8F=AC=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 217dd9ed..04e146fd 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -85,7 +85,9 @@ subprojects { '**/ImageType*', // domain-cvi-oauth-service '**/KakaoProfile*', - '**/NaverProfile*' + '**/NaverProfile*', + '**/UserInformation*', + '**/AuthRequest*' ] + Qdomains)})) } finalizedBy 'jacocoTestCoverageVerification' @@ -107,7 +109,7 @@ subprojects { // domain-cvi "**/Sort.java, **/Filter.java, **/ImageType.java, " + // domain-cvi-oauth-service - "**/KakaoProfile.java, **/NaverProfile.java" + "**/KakaoProfile.java, **/NaverProfile.java, **/UserInformation.java, **/AuthRequest.java" } } } From 7c08a3042a2ec9ea12b2a6a3a1f9992563021dcc Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 17:05:34 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=88=84=EB=9D=BD=EB=B6=80=EB=B6=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cvi/parser/AuthorizationManagerTest.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java index 8abb45f6..1780f882 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java @@ -2,7 +2,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import com.cvi.dto.profile.KakaoProfile; @@ -30,10 +32,10 @@ class AuthorizationManagerTest { private Map authorizationMap = new HashMap<>(); private AuthorizationManager authorizationManager = new AuthorizationManager(authorizationMap); - private NaverAuthorization naverAuthorization = spy(new NaverAuthorization()); - private KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization()); + private final NaverAuthorization naverAuthorization = spy(new NaverAuthorization()); + private final KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization()); - private ObjectMapper objectMapper = new ObjectMapper(); + private final ObjectMapper objectMapper = new ObjectMapper(); private UserInformation naverUserInfo; private UserInformation kakaoUserInfo; @@ -71,9 +73,9 @@ void requestKakaoUserInfo() { assertThat(expected).isEqualTo(kakaoUserInfo); } - @DisplayName("Authorization 매니저 유저 정보 요청 - 실패 - Provider가 유효하지 않은 경우 - Naver") + @DisplayName("Authorization 매니저 유저 정보 요청 - 실패 - Provider가 Null인 경우") @Test - void requestUserInfoFailureWhenInvalidSocialProviderNaver() { + void requestUserInfoFailureWhenNullSocialProvider() { //given //when //then @@ -81,4 +83,18 @@ void requestUserInfoFailureWhenInvalidSocialProviderNaver() { .isExactlyInstanceOf(InvalidOperationException.class) .hasMessage("해당 OAuth 제공자가 존재하지 않습니다 입력값: null"); } + + @DisplayName("Authorization 매니저 유저 정보 요청 - 실패 - Provider가 유효하지 않은 경우") + @Test + void requestUserInfoFailureWhenInvalidSocialProvider() { + //given + //when + authorizationMap = spy(HashMap.class); + authorizationManager = new AuthorizationManager(authorizationMap); + willReturn(false).given(authorizationMap).containsKey(any()); + //then + assertThatThrownBy(() -> authorizationManager.requestUserInfo(SocialProvider.KAKAO, SOCIAL_CODE, STATE, REQUEST_ORIGIN)) + .isExactlyInstanceOf(InvalidOperationException.class) + .hasMessage("해당 OAuth 제공자가 존재하지 않습니다 입력값: kakaoAuthorization"); + } } From 7c0bc5b1603a7b7dbea3271efaf33a9d0630af3c Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 17:19:19 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20=EC=9E=90=EC=BD=94=EC=BD=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=9C=EC=99=B8=20=EC=96=B4?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BB=A4=EB=B2=84=EB=A6=AC?= =?UTF-8?q?=EC=A7=80=20100?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cvi/ExcludeFromJacocoGeneratedReport.java | 11 +++++++++++ .../main/java/com/cvi/parser/KakaoAuthorization.java | 2 ++ .../main/java/com/cvi/parser/NaverAuthorization.java | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java diff --git a/backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java b/backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java new file mode 100644 index 00000000..a11218c9 --- /dev/null +++ b/backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java @@ -0,0 +1,11 @@ +package com.cvi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface ExcludeFromJacocoGeneratedReport { +} \ No newline at end of file diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java index e238a2b5..ab71ea11 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java @@ -1,5 +1,6 @@ package com.cvi.parser; +import com.cvi.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.KakaoOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.KakaoProfile; @@ -91,6 +92,7 @@ public HttpEntity> createProfileRequest(OAuthToken } @Override + @ExcludeFromJacocoGeneratedReport public ResponseEntity sendRequest(HttpEntity> request, String url) { return restTemplate.exchange( url, diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java index bd30fb70..4da06d93 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java @@ -1,5 +1,6 @@ package com.cvi.parser; +import com.cvi.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.NaverOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.NaverProfile; @@ -95,6 +96,7 @@ public HttpEntity> createProfileRequest(OAuthToken } @Override + @ExcludeFromJacocoGeneratedReport public ResponseEntity sendRequest(HttpEntity> request, String url) { return restTemplate.exchange( url, From f9fb2ecb63d6c33add08c40f57b261a0b328ad99 Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 17:39:42 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BB=A4=EB=B2=84=EB=A6=AC=EC=A7=80=20=EA=B8=B0=EC=A4=80=20?= =?UTF-8?q?=EC=83=81=ED=96=A5,=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cvi/{ => jacoco}/ExcludeFromJacocoGeneratedReport.java | 4 ++-- backend/domain-cvi-oauth-service/build.gradle | 2 +- .../src/main/java/com/cvi/parser/KakaoAuthorization.java | 2 +- .../src/main/java/com/cvi/parser/NaverAuthorization.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename backend/common-cvi/src/main/java/com/cvi/{ => jacoco}/ExcludeFromJacocoGeneratedReport.java (91%) diff --git a/backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java b/backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java similarity index 91% rename from backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java rename to backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java index a11218c9..41a8433e 100644 --- a/backend/common-cvi/src/main/java/com/cvi/ExcludeFromJacocoGeneratedReport.java +++ b/backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java @@ -1,4 +1,4 @@ -package com.cvi; +package com.cvi.jacoco; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -8,4 +8,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ExcludeFromJacocoGeneratedReport { -} \ No newline at end of file +} diff --git a/backend/domain-cvi-oauth-service/build.gradle b/backend/domain-cvi-oauth-service/build.gradle index 957a874a..2489ad81 100644 --- a/backend/domain-cvi-oauth-service/build.gradle +++ b/backend/domain-cvi-oauth-service/build.gradle @@ -16,7 +16,7 @@ jacocoTestCoverageVerification { // 코드 커버리지 측정항목 시행 limit { counter = 'METHOD' value = 'COVEREDRATIO' - minimum = 0.85 + minimum = 1.00 } } } diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java index ab71ea11..d7c12193 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java @@ -1,6 +1,6 @@ package com.cvi.parser; -import com.cvi.ExcludeFromJacocoGeneratedReport; +import com.cvi.jacoco.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.KakaoOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.KakaoProfile; diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java index 4da06d93..3b1c468e 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java @@ -1,6 +1,6 @@ package com.cvi.parser; -import com.cvi.ExcludeFromJacocoGeneratedReport; +import com.cvi.jacoco.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.NaverOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.NaverProfile; From 9d3d0a243d8334a625c970b053c013f3e1c75f5c Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 19:46:47 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20Oauth=20=EB=AA=A8=EB=93=88=20?= =?UTF-8?q?=EC=BB=A4=EB=B2=84=EB=A6=AC=EC=A7=80=20100?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 2 +- .../ExcludeFromJacocoGeneratedReport.java | 11 ----- .../main/java/com/cvi/config/OAuthConfig.java | 19 ++++++++ .../com/cvi/parser/KakaoAuthorization.java | 16 +++---- .../com/cvi/parser/NaverAuthorization.java | 18 ++++---- .../cvi/parser/AuthorizationManagerTest.java | 25 ++++------ .../cvi/parser/KakaoAuthorizationTest.java | 46 ++++++++++++------- .../cvi/parser/NaverAuthorizationTest.java | 28 +++++++---- 8 files changed, 94 insertions(+), 71 deletions(-) delete mode 100644 backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java create mode 100644 backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java diff --git a/backend/build.gradle b/backend/build.gradle index 04e146fd..1f553700 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -109,7 +109,7 @@ subprojects { // domain-cvi "**/Sort.java, **/Filter.java, **/ImageType.java, " + // domain-cvi-oauth-service - "**/KakaoProfile.java, **/NaverProfile.java, **/UserInformation.java, **/AuthRequest.java" + "**/KakaoProfile.java, **/NaverProfile.java, **/UserInformation.java, **/AuthRequest.java, **/OAuthConfig.java" } } } diff --git a/backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java b/backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java deleted file mode 100644 index 41a8433e..00000000 --- a/backend/common-cvi/src/main/java/com/cvi/jacoco/ExcludeFromJacocoGeneratedReport.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.cvi.jacoco; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface ExcludeFromJacocoGeneratedReport { -} diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java new file mode 100644 index 00000000..156d6cc9 --- /dev/null +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java @@ -0,0 +1,19 @@ +package com.cvi.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class OAuthConfig { + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } +} diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java index d7c12193..ecee0df4 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/KakaoAuthorization.java @@ -1,6 +1,5 @@ package com.cvi.parser; -import com.cvi.jacoco.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.KakaoOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.KakaoProfile; @@ -9,6 +8,7 @@ import com.cvi.exception.MappingFailureException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -21,14 +21,15 @@ @Slf4j @Component +@RequiredArgsConstructor public class KakaoAuthorization implements Authorization { private static final String PROFILE_REQUEST_URL = "https://kapi.kakao.com/v2/user/me"; private static final String TOKEN_REQUEST_URL = "https://kauth.kakao.com/oauth/token"; private static final String CALLBACK_URL_SUFFIX = "/auth/kakao/callback"; - private final RestTemplate restTemplate = new RestTemplate(); - private final ObjectMapper objectMapper = new ObjectMapper(); + private final RestTemplate restTemplate; + private final ObjectMapper objectMapper; @Override public UserInformation requestProfile(String code, String state, String requestOrigin) { @@ -92,13 +93,12 @@ public HttpEntity> createProfileRequest(OAuthToken } @Override - @ExcludeFromJacocoGeneratedReport public ResponseEntity sendRequest(HttpEntity> request, String url) { return restTemplate.exchange( - url, - HttpMethod.POST, - request, - String.class + url, + HttpMethod.POST, + request, + String.class ); } } diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java index 3b1c468e..0bf5c409 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/parser/NaverAuthorization.java @@ -1,6 +1,5 @@ package com.cvi.parser; -import com.cvi.jacoco.ExcludeFromJacocoGeneratedReport; import com.cvi.dto.oauthtoken.NaverOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.NaverProfile; @@ -9,6 +8,7 @@ import com.cvi.exception.MappingFailureException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; @@ -22,13 +22,14 @@ @Component @Slf4j -public class NaverAuthorization implements Authorization { +@RequiredArgsConstructor +public class NaverAuthorization implements Authorization { private static final String PROFILE_REQUEST_URL = "https://openapi.naver.com/v1/nid/me"; private static final String TOKEN_REQUEST_URL = "https://nid.naver.com/oauth2.0/token"; - private final RestTemplate restTemplate = new RestTemplate(); - private final ObjectMapper objectMapper = new ObjectMapper(); + private final RestTemplate restTemplate; + private final ObjectMapper objectMapper; @Value("${security.auth.naver.client-secret}") private String clientSecret; @@ -96,13 +97,12 @@ public HttpEntity> createProfileRequest(OAuthToken } @Override - @ExcludeFromJacocoGeneratedReport public ResponseEntity sendRequest(HttpEntity> request, String url) { return restTemplate.exchange( - url, - HttpMethod.POST, - request, - String.class + url, + HttpMethod.POST, + request, + String.class ); } } diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java index 1780f882..67a57600 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java @@ -4,7 +4,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.willReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import com.cvi.dto.profile.KakaoProfile; @@ -19,6 +18,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.web.client.RestTemplate; @DisplayName("Authorization 매니저 도메인 테스트") class AuthorizationManagerTest { @@ -31,11 +31,13 @@ class AuthorizationManagerTest { private Map authorizationMap = new HashMap<>(); private AuthorizationManager authorizationManager = new AuthorizationManager(authorizationMap); + private final ObjectMapper objectMapper = new ObjectMapper(); + private final RestTemplate restTemplate = new RestTemplate(); + + private final NaverAuthorization naverAuthorization = spy(new NaverAuthorization(restTemplate, objectMapper)); + private final KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization(restTemplate, objectMapper)); - private final NaverAuthorization naverAuthorization = spy(new NaverAuthorization()); - private final KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization()); - private final ObjectMapper objectMapper = new ObjectMapper(); private UserInformation naverUserInfo; private UserInformation kakaoUserInfo; @@ -62,17 +64,6 @@ void requestNaverUserInfo() { assertThat(expected).isEqualTo(naverUserInfo); } - @DisplayName("Kakao Authorization 매니저 유저 정보 요청 - 성공") - @Test - void requestKakaoUserInfo() { - //given - willReturn(kakaoUserInfo).given(kakaoAuthorization).requestProfile(SOCIAL_CODE, null, REQUEST_ORIGIN); - //when - UserInformation expected = authorizationManager.requestUserInfo(SocialProvider.KAKAO, SOCIAL_CODE, null, REQUEST_ORIGIN); - //then - assertThat(expected).isEqualTo(kakaoUserInfo); - } - @DisplayName("Authorization 매니저 유저 정보 요청 - 실패 - Provider가 Null인 경우") @Test void requestUserInfoFailureWhenNullSocialProvider() { @@ -80,8 +71,8 @@ void requestUserInfoFailureWhenNullSocialProvider() { //when //then assertThatThrownBy(() -> authorizationManager.requestUserInfo(null, SOCIAL_CODE, STATE, REQUEST_ORIGIN)) - .isExactlyInstanceOf(InvalidOperationException.class) - .hasMessage("해당 OAuth 제공자가 존재하지 않습니다 입력값: null"); + .isExactlyInstanceOf(InvalidOperationException.class) + .hasMessage("해당 OAuth 제공자가 존재하지 않습니다 입력값: null"); } @DisplayName("Authorization 매니저 유저 정보 요청 - 실패 - Provider가 유효하지 않은 경우") diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java index 6c35a2aa..325d23c6 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java @@ -1,26 +1,28 @@ package com.cvi.parser; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.BDDMockito.willReturn; -import static org.mockito.Mockito.spy; - import com.cvi.dto.oauthtoken.KakaoOAuthToken; import com.cvi.dto.oauthtoken.OAuthToken; import com.cvi.dto.profile.SocialProfile; import com.cvi.dto.profile.UserInformation; import com.cvi.exception.MappingFailureException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.willReturn; +import static org.mockito.Mockito.spy; @DisplayName("Kakao Authorization 도메인 테스트") class KakaoAuthorizationTest { - private static final String TOKEN_RESPONSE = "{\"token_type\":\"bearer\",\"access_token\":\"{ACCESS_TOKEN received from Social Provider}\",\"expires_in\":\"43199\",\"refresh_token\":\"{REFRESH_TOKEN received from Social Provider}\",\"refresh_token_expires_in\":\"25184000\",\"scope\":\"account_email profile\"}"; private static final String PROFILE_RESPONSE = "{\"id\":1816688137,\"connected_at\":\"2021-07-22T05:43:16Z\",\"properties\":{\"nickname\":\"김영빈\"},\"kakao_account\":{\"profile_nickname_needs_agreement\":false,\"profile_image_needs_agreement\":false,\"profile\":{\"nickname\":\"김영빈\",\"thumbnail_image_url\":\"http://k.kakaocdn.net/dn/dpk9l1/btqmGhA2lKL/Oz0wDuJn1YV2DIn92f6DVK/img_110x110.jpg\",\"profile_image_url\":\"http://k.kakaocdn.net/dn/dpk9l1/btqmGhA2lKL/Oz0wDuJn1YV2DIn92f6DVK/img_640x640.jpg\",\"is_default_image\":true}}}"; private static final String TOKEN_REQUEST_URL = "https://kauth.kakao.com/oauth/token"; @@ -35,11 +37,12 @@ class KakaoAuthorizationTest { private static final String SCOPE = "account_email profile"; private static final String REQUEST_ORIGIN = "http://localhost:9000"; - private KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization()); + private final RestTemplate restTemplate = spy(RestTemplate.class); + private final ObjectMapper objectMapper = new ObjectMapper(); + private final KakaoAuthorization kakaoAuthorization = new KakaoAuthorization(restTemplate, objectMapper); private HttpEntity> kakaoTokenRequest; private ResponseEntity tokenResponse; private ResponseEntity profileResponse; - private HttpEntity> kakaoProfileRequest; @BeforeEach void beforeEach() { @@ -47,10 +50,10 @@ void beforeEach() { tokenResponse = ResponseEntity.ok(TOKEN_RESPONSE); profileResponse = ResponseEntity.ok(PROFILE_RESPONSE); - kakaoProfileRequest = kakaoAuthorization.createProfileRequest(kakaoAuthorization.mapToOAuthToken(tokenResponse)); + HttpEntity> kakaoProfileRequest = kakaoAuthorization.createProfileRequest(kakaoAuthorization.mapToOAuthToken(tokenResponse)); - willReturn(tokenResponse).given(kakaoAuthorization).sendRequest(kakaoTokenRequest, TOKEN_REQUEST_URL); - willReturn(profileResponse).given(kakaoAuthorization).sendRequest(kakaoProfileRequest, PROFILE_REQUEST_URL); + willReturn(tokenResponse).given(restTemplate).exchange(TOKEN_REQUEST_URL, HttpMethod.POST, kakaoTokenRequest, String.class); + willReturn(profileResponse).given(restTemplate).exchange(PROFILE_REQUEST_URL, HttpMethod.POST, kakaoProfileRequest, String.class); } @DisplayName("카카오 프로필 요청 테스트 - 성공") @@ -84,11 +87,11 @@ void requestToken() { void requestTokenFailure() { //given HttpEntity> invalidToken = kakaoAuthorization.createTokenRequest("INVALID_TOKEN", null, REQUEST_ORIGIN); - willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(kakaoAuthorization).sendRequest(invalidToken, TOKEN_REQUEST_URL); + willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(restTemplate).exchange(TOKEN_REQUEST_URL, HttpMethod.POST, invalidToken, String.class); //when //then assertThatThrownBy(() -> kakaoAuthorization.requestToken("INVALID_TOKEN", null, REQUEST_ORIGIN)) - .isExactlyInstanceOf(MappingFailureException.class); + .isExactlyInstanceOf(MappingFailureException.class); } @DisplayName("토큰 매핑 테스트 - 성공") @@ -113,7 +116,7 @@ void mapToOAuthTokenFailureWhenNotValidTokenResponse() { //when //then assertThatThrownBy(() -> kakaoAuthorization.mapToOAuthToken(ResponseEntity.ok("NOT_VALID_TOKEN"))) - .isExactlyInstanceOf(MappingFailureException.class); + .isExactlyInstanceOf(MappingFailureException.class); } @DisplayName("프로필 요청 테스트 - 성공") @@ -133,11 +136,11 @@ void parseProfileFailure() { //given OAuthToken oAuthToken = kakaoAuthorization.mapToOAuthToken(tokenResponse); HttpEntity> invalidProfileRequest = kakaoAuthorization.createProfileRequest(oAuthToken); - willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(kakaoAuthorization).sendRequest(invalidProfileRequest, PROFILE_REQUEST_URL); + willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(restTemplate).exchange(PROFILE_REQUEST_URL, HttpMethod.POST, invalidProfileRequest, String.class); //when //then assertThatThrownBy(() -> kakaoAuthorization.parseProfile(oAuthToken)) - .isExactlyInstanceOf(MappingFailureException.class); + .isExactlyInstanceOf(MappingFailureException.class); } @DisplayName("프로필 매핑 테스트 - 성공") @@ -158,6 +161,15 @@ void mapToProfileFailureWhenInvalidProfileResponse() { //when //then assertThatThrownBy(() -> kakaoAuthorization.mapToProfile(ResponseEntity.ok("NOT_VALID_PROFILE"))) - .isExactlyInstanceOf(MappingFailureException.class); + .isExactlyInstanceOf(MappingFailureException.class); + } + + @DisplayName("외부요청 테스트 - 성공") + @Test + void sendRequest() { + //given + //when + //then + assertThat(kakaoAuthorization.sendRequest(kakaoTokenRequest, TOKEN_REQUEST_URL)).isEqualTo(tokenResponse); } } diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java index e9aceb90..f3950ba0 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java @@ -10,13 +10,16 @@ import com.cvi.dto.profile.SocialProfile; import com.cvi.dto.profile.UserInformation; import com.cvi.exception.MappingFailureException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; @DisplayName("Naver Authorization 도메인 테스트") class NaverAuthorizationTest { @@ -35,11 +38,12 @@ class NaverAuthorizationTest { private static final String EXPIRE_TIME = "3600"; private static final String REQUEST_ORIGIN = "http://localhost:9000"; - private NaverAuthorization naverAuthorization = spy(new NaverAuthorization()); + private final RestTemplate restTemplate = spy(RestTemplate.class); + private final ObjectMapper objectMapper = new ObjectMapper(); + private final NaverAuthorization naverAuthorization = new NaverAuthorization(restTemplate, objectMapper); private HttpEntity> naverTokenRequest; private ResponseEntity tokenResponse; private ResponseEntity profileResponse; - private HttpEntity> naverProfileRequest; @BeforeEach void beforeEach() { @@ -47,10 +51,9 @@ void beforeEach() { tokenResponse = ResponseEntity.ok(TOKEN_RESPONSE); profileResponse = ResponseEntity.ok(PROFILE_RESPONSE); - naverProfileRequest = naverAuthorization.createProfileRequest(naverAuthorization.mapToOAuthToken(tokenResponse)); - - willReturn(tokenResponse).given(naverAuthorization).sendRequest(naverTokenRequest, TOKEN_REQUEST_URL); - willReturn(profileResponse).given(naverAuthorization).sendRequest(naverProfileRequest, PROFILE_REQUEST_URL); + HttpEntity> naverProfileRequest = naverAuthorization.createProfileRequest(naverAuthorization.mapToOAuthToken(tokenResponse)); + willReturn(tokenResponse).given(restTemplate).exchange(TOKEN_REQUEST_URL, HttpMethod.POST, naverTokenRequest, String.class); + willReturn(profileResponse).given(restTemplate).exchange(PROFILE_REQUEST_URL, HttpMethod.POST, naverProfileRequest, String.class); } @DisplayName("네이버 프로필 요청 테스트 - 성공") @@ -82,7 +85,7 @@ void requestToken() { void requestTokenFailure() { //given HttpEntity> invalidToken = naverAuthorization.createTokenRequest("INVALID_TOKEN", STATE, REQUEST_ORIGIN); - willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(naverAuthorization).sendRequest(invalidToken, TOKEN_REQUEST_URL); + willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(restTemplate).exchange(TOKEN_REQUEST_URL, HttpMethod.POST, invalidToken, String.class); //when //then assertThatThrownBy(() -> naverAuthorization.requestToken("INVALID_TOKEN", STATE, REQUEST_ORIGIN)) @@ -129,7 +132,7 @@ void parseProfileFailure() { //given OAuthToken oAuthToken = naverAuthorization.mapToOAuthToken(tokenResponse); HttpEntity> invalidProfileRequest = naverAuthorization.createProfileRequest(oAuthToken); - willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(naverAuthorization).sendRequest(invalidProfileRequest, PROFILE_REQUEST_URL); + willReturn(new ResponseEntity<>("{\"ERROR\":\"ERROR\"}", HttpStatus.BAD_REQUEST)).given(restTemplate).exchange(PROFILE_REQUEST_URL, HttpMethod.POST, invalidProfileRequest, String.class); //when //then assertThatThrownBy(() -> naverAuthorization.parseProfile(oAuthToken)) @@ -156,4 +159,13 @@ void mapToProfileFailureWhenInvalidProfileResponse() { assertThatThrownBy(() -> naverAuthorization.mapToProfile(ResponseEntity.ok("NOT_VALID_PROFILE"))) .isExactlyInstanceOf(MappingFailureException.class); } + + @DisplayName("외부요청 테스트 - 성공") + @Test + void sendRequest() { + //given + //when + //then + assertThat(naverAuthorization.sendRequest(naverTokenRequest, TOKEN_REQUEST_URL)).isEqualTo(tokenResponse); + } } From bbd0a8abaa0aa99ce069a425356e37f97e1cac9d Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 21:17:13 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20ObjectMapper=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 3 ++- .../src/main/java/com/cvi/config/OAuthConfig.java | 7 +------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 1f553700..53ecde02 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -87,7 +87,8 @@ subprojects { '**/KakaoProfile*', '**/NaverProfile*', '**/UserInformation*', - '**/AuthRequest*' + '**/AuthRequest*', + '**/OAuthConfig*' ] + Qdomains)})) } finalizedBy 'jacocoTestCoverageVerification' diff --git a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java index 156d6cc9..ae11a66a 100644 --- a/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java +++ b/backend/domain-cvi-oauth-service/src/main/java/com/cvi/config/OAuthConfig.java @@ -1,19 +1,14 @@ package com.cvi.config; -import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class OAuthConfig { + @Bean public RestTemplate restTemplate() { return new RestTemplate(); } - - @Bean - public ObjectMapper objectMapper() { - return new ObjectMapper(); - } } From 051e635a16d60023c2c394bc4375e963b4428db7 Mon Sep 17 00:00:00 2001 From: younghoonkwon Date: Wed, 27 Oct 2021 22:14:42 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EA=B8=80=EB=A1=9C=EB=B2=8C?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EB=93=A4=20=EC=9D=B8=EC=8A=A4=ED=84=B4?= =?UTF-8?q?=EC=8A=A4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cvi/parser/AuthorizationManagerTest.java | 8 ++------ .../test/java/com/cvi/parser/KakaoAuthorizationTest.java | 2 +- .../test/java/com/cvi/parser/NaverAuthorizationTest.java | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java index 67a57600..c79f277d 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/AuthorizationManagerTest.java @@ -34,12 +34,11 @@ class AuthorizationManagerTest { private final ObjectMapper objectMapper = new ObjectMapper(); private final RestTemplate restTemplate = new RestTemplate(); - private final NaverAuthorization naverAuthorization = spy(new NaverAuthorization(restTemplate, objectMapper)); - private final KakaoAuthorization kakaoAuthorization = spy(new KakaoAuthorization(restTemplate, objectMapper)); + private final Authorization naverAuthorization = spy(new NaverAuthorization(restTemplate, objectMapper)); + private final Authorization kakaoAuthorization = spy(new KakaoAuthorization(restTemplate, objectMapper)); private UserInformation naverUserInfo; - private UserInformation kakaoUserInfo; @BeforeEach void setUp() throws JsonProcessingException { @@ -48,9 +47,6 @@ void setUp() throws JsonProcessingException { NaverProfile naverProfile = objectMapper.readValue(NAVER_PROFILE_RESPONSE, NaverProfile.class); naverUserInfo = UserInformation.of(naverProfile); - - KakaoProfile kakaoProfile = objectMapper.readValue(KAKAO_PROFILE_RESPONSE, KakaoProfile.class); - kakaoUserInfo = UserInformation.of(kakaoProfile); } @DisplayName("Naver Authorization 매니저 유저 정보 요청 - 성공") diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java index 325d23c6..c345d202 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/KakaoAuthorizationTest.java @@ -39,7 +39,7 @@ class KakaoAuthorizationTest { private final RestTemplate restTemplate = spy(RestTemplate.class); private final ObjectMapper objectMapper = new ObjectMapper(); - private final KakaoAuthorization kakaoAuthorization = new KakaoAuthorization(restTemplate, objectMapper); + private final Authorization kakaoAuthorization = new KakaoAuthorization(restTemplate, objectMapper); private HttpEntity> kakaoTokenRequest; private ResponseEntity tokenResponse; private ResponseEntity profileResponse; diff --git a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java index f3950ba0..0920f8ca 100644 --- a/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java +++ b/backend/domain-cvi-oauth-service/src/test/java/com/cvi/parser/NaverAuthorizationTest.java @@ -40,7 +40,7 @@ class NaverAuthorizationTest { private final RestTemplate restTemplate = spy(RestTemplate.class); private final ObjectMapper objectMapper = new ObjectMapper(); - private final NaverAuthorization naverAuthorization = new NaverAuthorization(restTemplate, objectMapper); + private final Authorization naverAuthorization = new NaverAuthorization(restTemplate, objectMapper); private HttpEntity> naverTokenRequest; private ResponseEntity tokenResponse; private ResponseEntity profileResponse;