From 34d648a1ef29d6e0008528edfae67d6008830794 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 14:36:15 +0900 Subject: [PATCH 01/16] =?UTF-8?q?style:=20OAuthInfoDtoFixture=20=EB=AA=85?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...OAuthInfoResponseFixture.java => OAuthInfoDtoFixture.java} | 4 ++-- .../server/oauth/client/OAuthMemberClientCompositeTest.java | 4 ++-- .../coffeemeet/server/oauth/service/OAuthServiceTest.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/test/java/coffeemeet/server/common/fixture/dto/{OAuthInfoResponseFixture.java => OAuthInfoDtoFixture.java} (67%) diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoResponseFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java similarity index 67% rename from src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoResponseFixture.java rename to src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java index a9e3f15f..c09d143a 100644 --- a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoResponseFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java @@ -3,9 +3,9 @@ import coffeemeet.server.oauth.dto.OAuthInfoDto; import org.instancio.Instancio; -public class OAuthInfoResponseFixture { +public class OAuthInfoDtoFixture { - public static OAuthInfoDto.Response oAuthInfoResponse() { + public static OAuthInfoDto.Response response() { return Instancio.of(OAuthInfoDto.Response.class) .create(); } diff --git a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java index 324dc45d..fcf9b179 100644 --- a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java +++ b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; -import coffeemeet.server.common.fixture.dto.OAuthInfoResponseFixture; +import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; import coffeemeet.server.oauth.dto.OAuthInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import java.util.Collections; @@ -34,7 +34,7 @@ void fetchTest() { // given String authCode = "authCode"; OAuthProvider oAuthProvider = OAuthProvider.KAKAO; - OAuthInfoDto.Response response = OAuthInfoResponseFixture.oAuthInfoResponse(); + OAuthInfoDto.Response response = OAuthInfoDtoFixture.response(); given(client.oAuthProvider()).willReturn(OAuthProvider.KAKAO); given(client.fetch(authCode)).willReturn(response); diff --git a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java index 96d368f5..ea79dd99 100644 --- a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java +++ b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; -import coffeemeet.server.common.fixture.dto.OAuthInfoResponseFixture; +import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite; import coffeemeet.server.oauth.client.OAuthMemberClientComposite; import coffeemeet.server.oauth.dto.OAuthInfoDto; @@ -48,7 +48,7 @@ void getOAuthInfoTest() { // given OAuthProvider oAuthProvider = OAuthProvider.KAKAO; String authCode = "authCode"; - OAuthInfoDto.Response oAuthInfoResponse = OAuthInfoResponseFixture.oAuthInfoResponse(); + OAuthInfoDto.Response oAuthInfoResponse = OAuthInfoDtoFixture.response(); given(oAuthMemberClientComposite.fetch(oAuthProvider, authCode)).willReturn(oAuthInfoResponse); From dc72a7c0972de4c8af57aec50d3cf70e562b2434 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 15:37:41 +0900 Subject: [PATCH 02/16] =?UTF-8?q?feat:=20RefreshTokenFixture=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/fixture/dto/RefreshTokenFixture.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/java/coffeemeet/server/common/fixture/dto/RefreshTokenFixture.java diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/RefreshTokenFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/RefreshTokenFixture.java new file mode 100644 index 00000000..9140ba1a --- /dev/null +++ b/src/test/java/coffeemeet/server/common/fixture/dto/RefreshTokenFixture.java @@ -0,0 +1,13 @@ +package coffeemeet.server.common.fixture.dto; + +import coffeemeet.server.auth.domain.RefreshToken; +import org.instancio.Instancio; + +public class RefreshTokenFixture { + + public static RefreshToken refreshToken() { + return Instancio.of(RefreshToken.class) + .create(); + } + +} From 09334bd998d960b1fc4707c58c62ae2cb9c208da Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 15:37:50 +0900 Subject: [PATCH 03/16] =?UTF-8?q?feat:=20AuthTokensFixture=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/fixture/dto/AuthTokensFixture.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java new file mode 100644 index 00000000..ce0be0e2 --- /dev/null +++ b/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java @@ -0,0 +1,13 @@ +package coffeemeet.server.common.fixture.dto; + +import coffeemeet.server.auth.domain.AuthTokens; +import org.instancio.Instancio; + +public class AuthTokensFixture { + + public static AuthTokens authTokens() { + return Instancio.of(AuthTokens.class) + .create(); + } + +} From bf6e8df4ce0bd39e9614677dbd34a54bc6e706de Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 15:38:27 +0900 Subject: [PATCH 04/16] =?UTF-8?q?refactor:=20@RequestMapping("/api/v1/auth?= =?UTF-8?q?")=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/coffeemeet/server/auth/controller/AuthController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/coffeemeet/server/auth/controller/AuthController.java b/src/main/java/coffeemeet/server/auth/controller/AuthController.java index 5ba14558..d2549563 100644 --- a/src/main/java/coffeemeet/server/auth/controller/AuthController.java +++ b/src/main/java/coffeemeet/server/auth/controller/AuthController.java @@ -12,7 +12,7 @@ @RestController @RequiredArgsConstructor -@RequestMapping("/auth") +@RequestMapping("/api/v1/auth") public class AuthController { private final AuthService authService; From 76dbd4cb096202b44597b1658c430aa46717c5bb Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 15:40:05 +0900 Subject: [PATCH 05/16] =?UTF-8?q?test:=20renewTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthControllerTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java diff --git a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java new file mode 100644 index 00000000..e72ff4a8 --- /dev/null +++ b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java @@ -0,0 +1,76 @@ +package coffeemeet.server.auth.controller; + +import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document; +import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.resourceDetails; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.BDDMockito.given; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import coffeemeet.server.auth.domain.AuthTokens; +import coffeemeet.server.auth.domain.RefreshToken; +import coffeemeet.server.auth.service.AuthService; +import coffeemeet.server.common.config.ControllerTestConfig; +import coffeemeet.server.common.fixture.dto.AuthTokensFixture; +import coffeemeet.server.common.fixture.dto.RefreshTokenFixture; +import com.epages.restdocs.apispec.Schema; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.restdocs.payload.JsonFieldType; + +@WebMvcTest(AuthController.class) +class AuthControllerTest extends ControllerTestConfig { + + @MockBean + private AuthService authService; + + @DisplayName("refresh token 을 통해 access token 을 갱신할 수 있다.") + @Test + void renewTest() throws Exception { + // given + AuthTokens authTokens = AuthTokensFixture.authTokens(); + RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); + given(authService.renew(anyLong(), any())).willReturn(authTokens); + given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); + + // when, then + mockMvc.perform(post("/api/v1/auth/renew-token") + .header("Authorization", TOKEN) + .contentType(MediaType.APPLICATION_JSON) + ) + .andDo(document("renew-token", + resourceDetails().tag("인증").description("토큰 갱신") + .responseSchema(Schema.schema("AuthTokens")), + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + responseFields( + fieldWithPath("accessToken").type(JsonFieldType.STRING).description("액세스 토큰"), + fieldWithPath("refreshToken").type(JsonFieldType.STRING).description("리프레시 토큰") + ) + ) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.accessToken").value(authTokens.accessToken())) + .andExpect(jsonPath("$.refreshToken").value(authTokens.refreshToken())); + } + + @Test + void logoutTest() { + } + + @Test + void deleteTest() { + } + +} From 798c760f83f112acbc646ae15fc256971e29ded5 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 16:16:01 +0900 Subject: [PATCH 06/16] =?UTF-8?q?test:=20logoutTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthControllerTest.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java index e72ff4a8..99e755bb 100644 --- a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java +++ b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java @@ -5,6 +5,9 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doNothing; +import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; +import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; @@ -41,6 +44,7 @@ void renewTest() throws Exception { // given AuthTokens authTokens = AuthTokensFixture.authTokens(); RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); + given(authService.renew(anyLong(), any())).willReturn(authTokens); given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); @@ -49,11 +53,14 @@ void renewTest() throws Exception { .header("Authorization", TOKEN) .contentType(MediaType.APPLICATION_JSON) ) - .andDo(document("renew-token", + .andDo(document("auth-renew", resourceDetails().tag("인증").description("토큰 갱신") .responseSchema(Schema.schema("AuthTokens")), preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), + requestHeaders( + headerWithName("Authorization").description("토큰") + ), responseFields( fieldWithPath("accessToken").type(JsonFieldType.STRING).description("액세스 토큰"), fieldWithPath("refreshToken").type(JsonFieldType.STRING).description("리프레시 토큰") @@ -65,8 +72,31 @@ void renewTest() throws Exception { .andExpect(jsonPath("$.refreshToken").value(authTokens.refreshToken())); } + @DisplayName("사용자는 로그아웃 할 수 있다.") @Test - void logoutTest() { + void logoutTest() throws Exception { + // given + AuthTokens authTokens = AuthTokensFixture.authTokens(); + RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); + + doNothing().when(authService).logout(anyLong()); + given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); + + // when, then + mockMvc.perform(post("/api/v1/auth/logout") + .header("Authorization", TOKEN) + .contentType(MediaType.APPLICATION_JSON) + ) + .andDo(document("auth-logout", + resourceDetails().tag("인증").description("로그아웃"), + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + requestHeaders( + headerWithName("Authorization").description("토큰") + ) + ) + ) + .andExpect(status().isOk()); } @Test From a79d935217bb8d1f7fe9ce864dc5c7824b0446c9 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 16:22:38 +0900 Subject: [PATCH 07/16] =?UTF-8?q?test:=20deleteTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthControllerTest.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java index 99e755bb..d9c03a48 100644 --- a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java +++ b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java @@ -76,7 +76,6 @@ void renewTest() throws Exception { @Test void logoutTest() throws Exception { // given - AuthTokens authTokens = AuthTokensFixture.authTokens(); RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); doNothing().when(authService).logout(anyLong()); @@ -99,8 +98,30 @@ void logoutTest() throws Exception { .andExpect(status().isOk()); } + @DisplayName("사용자는 회원탈퇴 할 수 있다.") @Test - void deleteTest() { + void deleteTest() throws Exception { + // given + RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); + + doNothing().when(authService).delete(anyLong()); + given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); + + // when, then + mockMvc.perform(post("/api/v1/auth/delete") + .header("Authorization", TOKEN) + .contentType(MediaType.APPLICATION_JSON) + ) + .andDo(document("auth-delete", + resourceDetails().tag("인증").description("회원탈퇴"), + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + requestHeaders( + headerWithName("Authorization").description("토큰") + ) + ) + ) + .andExpect(status().isOk()); } } From c1ad650574a37dc98c10489b95e71318a45599df Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 16:48:04 +0900 Subject: [PATCH 08/16] =?UTF-8?q?test:=20generateTest=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/domain/AuthTokensGeneratorTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java diff --git a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java new file mode 100644 index 00000000..94567f12 --- /dev/null +++ b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java @@ -0,0 +1,52 @@ +package coffeemeet.server.auth.domain; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import coffeemeet.server.auth.repository.RefreshTokenRepository; +import java.util.Date; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class AuthTokensGeneratorTest { + + private static final Long accessTokenExpireTime = 1800L; + private static final Long refreshTokenExpireTime = 3600L; + private static final String BEARER_TYPE = "Bearer "; + private static final String ACCESS_TOKEN = "accessToken"; + private static final String REFRESH_TOKEN = "refreshToken"; + + @InjectMocks + private AuthTokensGenerator authTokensGenerator; + + @Mock + private JwtTokenProvider jwtTokenProvider; + + @Mock + private RefreshTokenRepository refreshTokenRepository; + + @BeforeEach + public void init() { + authTokensGenerator = new AuthTokensGenerator(jwtTokenProvider, accessTokenExpireTime, + refreshTokenExpireTime, refreshTokenRepository); + } + + @Test + void generateTest() { + // given + when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, + REFRESH_TOKEN); + AuthTokens authTokens = authTokensGenerator.generate((long) Math.random()); + + assertThat(authTokens.accessToken()).isEqualTo(BEARER_TYPE + ACCESS_TOKEN); + assertThat(authTokens.refreshToken()).isEqualTo(BEARER_TYPE + REFRESH_TOKEN); + } + +} From 152fd565db9a4d3532c826e9e055bfc164dc0b71 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 16:51:00 +0900 Subject: [PATCH 09/16] =?UTF-8?q?test:=20refreshJwtTokenTest=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/domain/AuthTokensGeneratorTest.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java index 94567f12..68654d39 100644 --- a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java +++ b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java @@ -1,6 +1,7 @@ package coffeemeet.server.auth.domain; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -43,10 +44,32 @@ void generateTest() { // given when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, REFRESH_TOKEN); + + // when AuthTokens authTokens = authTokensGenerator.generate((long) Math.random()); - assertThat(authTokens.accessToken()).isEqualTo(BEARER_TYPE + ACCESS_TOKEN); - assertThat(authTokens.refreshToken()).isEqualTo(BEARER_TYPE + REFRESH_TOKEN); + // then + assertAll( + () -> assertThat(authTokens.accessToken()).isEqualTo(BEARER_TYPE + ACCESS_TOKEN), + () -> assertThat(authTokens.refreshToken()).isEqualTo(BEARER_TYPE + REFRESH_TOKEN) + ); + } + + @Test + void refreshJwtTokenTest() { + // given + when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, + REFRESH_TOKEN); + + // when + AuthTokens authTokens = authTokensGenerator.reissueAccessToken((long) Math.random(), + REFRESH_TOKEN); + + // then + assertAll( + () -> assertThat(authTokens.accessToken()).isEqualTo(BEARER_TYPE + ACCESS_TOKEN), + () -> assertThat(authTokens.refreshToken()).isEqualTo(BEARER_TYPE + REFRESH_TOKEN) + ); } } From 7b994d724dd28a2a7ee15424668c2880156ed6c4 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 17:28:10 +0900 Subject: [PATCH 10/16] =?UTF-8?q?test:=20AuthTokensGeneratorTest=20?= =?UTF-8?q?=EC=84=A4=EB=AA=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/auth/domain/AuthTokensGeneratorTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java index 68654d39..9f380338 100644 --- a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java +++ b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java @@ -9,6 +9,7 @@ import coffeemeet.server.auth.repository.RefreshTokenRepository; import java.util.Date; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -39,6 +40,7 @@ public void init() { refreshTokenExpireTime, refreshTokenRepository); } + @DisplayName("access token & refresh token 발급할 수 있다.") @Test void generateTest() { // given @@ -55,8 +57,9 @@ void generateTest() { ); } + @DisplayName("access token 을 갱신 할 수 있다.") @Test - void refreshJwtTokenTest() { + void reissueAccessTokenTest() { // given when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, REFRESH_TOKEN); From 7dc2e5600d5b649377ad45a3d902e0b8dbaa1be9 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 17:46:53 +0900 Subject: [PATCH 11/16] =?UTF-8?q?test:=20JwtTokenProviderTest=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/domain/JwtTokenProviderTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/coffeemeet/server/auth/domain/JwtTokenProviderTest.java diff --git a/src/test/java/coffeemeet/server/auth/domain/JwtTokenProviderTest.java b/src/test/java/coffeemeet/server/auth/domain/JwtTokenProviderTest.java new file mode 100644 index 00000000..2b22d811 --- /dev/null +++ b/src/test/java/coffeemeet/server/auth/domain/JwtTokenProviderTest.java @@ -0,0 +1,64 @@ +package coffeemeet.server.auth.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.security.Keys; +import java.util.Base64; +import java.util.Date; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class JwtTokenProviderTest { + + private static final String SUBJECT = "123"; + private static final int ACCESS_TOKEN_EXPIRE_TIME = 3600000; + + private final String secretKey = Base64.getEncoder() + .encodeToString(Keys.secretKeyFor(SignatureAlgorithm.HS512).getEncoded()); + + private JwtTokenProvider jwtTokenProvider = new JwtTokenProvider(secretKey); + + @DisplayName("jwt 를 생성할 수 있다.") + @Test + void generate() { + // given + Date date = new Date(System.currentTimeMillis() + ACCESS_TOKEN_EXPIRE_TIME); + + // when + String result = jwtTokenProvider.generate(SUBJECT, date); + + // then + assertThat(result).isNotNull(); + } + + @DisplayName("jwt 로부터 userId 를 가져올 수 있다.") + @Test + void extractUserId() { + // given + Date date = new Date(System.currentTimeMillis() + ACCESS_TOKEN_EXPIRE_TIME); + + // when + String token = jwtTokenProvider.generate(SUBJECT, date); + Long result = jwtTokenProvider.extractUserId(token); + + // then + assertThat(result).isEqualTo(Long.parseLong(SUBJECT)); + } + + @DisplayName("jwt(refresh token) 로부터 토큰이 만료되었는지 확인할 수 있다.") + @Test + void isExpiredRefreshToken() { + // given + Date date = new Date(System.currentTimeMillis() + ACCESS_TOKEN_EXPIRE_TIME); + + String token = jwtTokenProvider.generate(SUBJECT, date); + + // when, then + assertThat(jwtTokenProvider.isExpiredRefreshToken(token)).isFalse(); + } + +} From 07109a2319894bc1d119cc4835d0340142f1b3da Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 18:29:23 +0900 Subject: [PATCH 12/16] test: add authTokens fixture --- .../server/common/fixture/dto/AuthTokensFixture.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java index ce0be0e2..3d657555 100644 --- a/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/dto/AuthTokensFixture.java @@ -1,5 +1,7 @@ package coffeemeet.server.common.fixture.dto; +import static org.instancio.Select.field; + import coffeemeet.server.auth.domain.AuthTokens; import org.instancio.Instancio; @@ -10,4 +12,10 @@ public static AuthTokens authTokens() { .create(); } + public static AuthTokens authTokens(String refreshToken) { + return Instancio.of(AuthTokens.class) + .set(field(AuthTokens::refreshToken), refreshToken) + .create(); + } + } From 29119f97363143548e554092c7cb04d7c20e57bf Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 18:36:07 +0900 Subject: [PATCH 13/16] =?UTF-8?q?test:=20AuthServiceTest=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/auth/service/AuthServiceTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/java/coffeemeet/server/auth/service/AuthServiceTest.java diff --git a/src/test/java/coffeemeet/server/auth/service/AuthServiceTest.java b/src/test/java/coffeemeet/server/auth/service/AuthServiceTest.java new file mode 100644 index 00000000..614b0538 --- /dev/null +++ b/src/test/java/coffeemeet/server/auth/service/AuthServiceTest.java @@ -0,0 +1,90 @@ +package coffeemeet.server.auth.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.BDDMockito.any; +import static org.mockito.BDDMockito.anyLong; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willDoNothing; + +import coffeemeet.server.auth.domain.AuthTokens; +import coffeemeet.server.auth.domain.AuthTokensGenerator; +import coffeemeet.server.auth.domain.JwtTokenProvider; +import coffeemeet.server.auth.repository.RefreshTokenRepository; +import coffeemeet.server.common.fixture.dto.AuthTokensFixture; +import coffeemeet.server.user.service.UserService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class AuthServiceTest { + + public static final String REFRESH_TOKEN = "Bearer aaaaaaaa.bbbbbbb.ccccccc"; + @InjectMocks + private AuthService authService; + + @Mock + private UserService userService; + + @Mock + private AuthTokensGenerator authTokensGenerator; + + @Mock + private JwtTokenProvider jwtTokenProvider; + + @Mock + private RefreshTokenRepository refreshTokenRepository; + + @DisplayName("access token 을 갱신할 수 있다.") + @Test + void renew() { + // given + AuthTokens authTokens = AuthTokensFixture.authTokens(); + AuthTokens newAuthTokens = AuthTokensFixture.authTokens(REFRESH_TOKEN); + + given(jwtTokenProvider.isExpiredRefreshToken(REFRESH_TOKEN)).willReturn(false); + given(authTokensGenerator.reissueAccessToken(anyLong(), any(String.class))).willReturn( + newAuthTokens); + + // when + AuthTokens renewedAuthTokens = authService.renew((long) Math.random(), REFRESH_TOKEN); + + // then + assertAll( + () -> assertThat(renewedAuthTokens.accessToken()).isNotEqualTo(authTokens.accessToken()), + () -> assertThat(renewedAuthTokens.refreshToken()).isEqualTo(REFRESH_TOKEN) + ); + } + + @DisplayName("로그아웃 시킬 수 있다.") + @Test + void logout() { + // given + willDoNothing().given(refreshTokenRepository).deleteById(anyLong()); + + // when + authService.logout((long) Math.random()); + + // then + assertThat(true).isTrue(); + } + + @DisplayName("회원 탈퇴 시킬 수 있다.") + @Test + void delete() { + // given + willDoNothing().given(refreshTokenRepository).deleteById(anyLong()); + willDoNothing().given(userService).deleteUser(anyLong()); + + // when + authService.delete((long) Math.random()); + + // then + assertThat(true).isTrue(); + } + +} From b4d7e03b3c8de9a069d5cbdd264fa967e46164be Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 18:38:53 +0900 Subject: [PATCH 14/16] =?UTF-8?q?test:=20BDDMockito=20=EB=9D=BC=EC=9D=B4?= =?UTF-8?q?=EB=B8=8C=EB=9F=AC=EB=A6=AC=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/auth/controller/AuthControllerTest.java | 8 +++----- .../server/auth/domain/AuthTokensGeneratorTest.java | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java index d9c03a48..4387a1f6 100644 --- a/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java +++ b/src/test/java/coffeemeet/server/auth/controller/AuthControllerTest.java @@ -5,7 +5,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.doNothing; +import static org.mockito.BDDMockito.willDoNothing; import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; @@ -77,8 +77,7 @@ void renewTest() throws Exception { void logoutTest() throws Exception { // given RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); - - doNothing().when(authService).logout(anyLong()); + willDoNothing().given(authService).logout(anyLong()); given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); // when, then @@ -103,8 +102,7 @@ void logoutTest() throws Exception { void deleteTest() throws Exception { // given RefreshToken refreshToken = RefreshTokenFixture.refreshToken(); - - doNothing().when(authService).delete(anyLong()); + willDoNothing().given(authService).delete(anyLong()); given(refreshTokenRepository.findById(anyLong())).willReturn(Optional.ofNullable(refreshToken)); // when, then diff --git a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java index 9f380338..09de8058 100644 --- a/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java +++ b/src/test/java/coffeemeet/server/auth/domain/AuthTokensGeneratorTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; +import static org.mockito.BDDMockito.given; import coffeemeet.server.auth.repository.RefreshTokenRepository; import java.util.Date; @@ -44,7 +44,7 @@ public void init() { @Test void generateTest() { // given - when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, + given(jwtTokenProvider.generate(anyString(), any(Date.class))).willReturn(ACCESS_TOKEN, REFRESH_TOKEN); // when @@ -61,7 +61,7 @@ void generateTest() { @Test void reissueAccessTokenTest() { // given - when(jwtTokenProvider.generate(anyString(), any(Date.class))).thenReturn(ACCESS_TOKEN, + given(jwtTokenProvider.generate(anyString(), any(Date.class))).willReturn(ACCESS_TOKEN, REFRESH_TOKEN); // when From 816f2be962975302d684f5354f7c6c092736ea50 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 18:45:37 +0900 Subject: [PATCH 15/16] =?UTF-8?q?refactor:=20OAuthUserInfoDto=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coffeemeet/server/oauth/client/OAuthMemberClient.java | 4 ++-- .../server/oauth/client/OAuthMemberClientComposite.java | 4 ++-- .../dto/{OAuthInfoDto.java => OAuthUserInfoDto.java} | 8 ++++---- .../infrastructure/kakao/client/KakaoMemberClient.java | 4 ++-- .../infrastructure/kakao/dto/KakaoMemberResponse.java | 6 +++--- .../coffeemeet/server/oauth/service/OAuthService.java | 4 ++-- .../java/coffeemeet/server/user/service/UserService.java | 8 ++++---- .../server/common/fixture/dto/OAuthInfoDtoFixture.java | 6 +++--- .../oauth/client/OAuthMemberClientCompositeTest.java | 6 +++--- .../coffeemeet/server/oauth/service/OAuthServiceTest.java | 4 ++-- 10 files changed, 27 insertions(+), 27 deletions(-) rename src/main/java/coffeemeet/server/oauth/dto/{OAuthInfoDto.java => OAuthUserInfoDto.java} (76%) diff --git a/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClient.java b/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClient.java index a3f81b1f..7a13eeea 100644 --- a/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClient.java +++ b/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClient.java @@ -1,12 +1,12 @@ package coffeemeet.server.oauth.client; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; public interface OAuthMemberClient { OAuthProvider oAuthProvider(); - OAuthInfoDto.Response fetch(String authCode); + OAuthUserInfoDto.Response fetch(String authCode); } diff --git a/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClientComposite.java b/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClientComposite.java index 71796d74..b240569b 100644 --- a/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClientComposite.java +++ b/src/main/java/coffeemeet/server/oauth/client/OAuthMemberClientComposite.java @@ -1,6 +1,6 @@ package coffeemeet.server.oauth.client; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import java.util.Map; import java.util.Optional; @@ -21,7 +21,7 @@ public OAuthMemberClientComposite(Set clients) { ); } - public OAuthInfoDto.Response fetch(OAuthProvider oAuthProvider, String authCode) { + public OAuthUserInfoDto.Response fetch(OAuthProvider oAuthProvider, String authCode) { return getClient(oAuthProvider).fetch(authCode); } diff --git a/src/main/java/coffeemeet/server/oauth/dto/OAuthInfoDto.java b/src/main/java/coffeemeet/server/oauth/dto/OAuthUserInfoDto.java similarity index 76% rename from src/main/java/coffeemeet/server/oauth/dto/OAuthInfoDto.java rename to src/main/java/coffeemeet/server/oauth/dto/OAuthUserInfoDto.java index 9442fd87..0a1c1782 100644 --- a/src/main/java/coffeemeet/server/oauth/dto/OAuthInfoDto.java +++ b/src/main/java/coffeemeet/server/oauth/dto/OAuthUserInfoDto.java @@ -2,7 +2,7 @@ import coffeemeet.server.user.domain.OAuthProvider; -public sealed interface OAuthInfoDto permits OAuthInfoDto.Response { +public sealed interface OAuthUserInfoDto permits OAuthUserInfoDto.Response { record Response( String name, @@ -12,9 +12,9 @@ record Response( String email, OAuthProvider oAuthProvider, String oAuthProviderId - ) implements OAuthInfoDto { + ) implements OAuthUserInfoDto { - public static OAuthInfoDto.Response of( + public static OAuthUserInfoDto.Response of( String name, String profileImage, String birthYear, @@ -23,7 +23,7 @@ public static OAuthInfoDto.Response of( OAuthProvider oAuthProvider, String oAuthProviderId ) { - return new OAuthInfoDto.Response( + return new OAuthUserInfoDto.Response( name, profileImage, birthYear, diff --git a/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/client/KakaoMemberClient.java b/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/client/KakaoMemberClient.java index 1cc51007..d9343906 100644 --- a/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/client/KakaoMemberClient.java +++ b/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/client/KakaoMemberClient.java @@ -1,7 +1,7 @@ package coffeemeet.server.oauth.infrastructure.kakao.client; import coffeemeet.server.oauth.client.OAuthMemberClient; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoMemberResponse; import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoTokens; import coffeemeet.server.user.domain.OAuthProvider; @@ -20,7 +20,7 @@ public OAuthProvider oAuthProvider() { } @Override - public OAuthInfoDto.Response fetch(String authCode) { + public OAuthUserInfoDto.Response fetch(String authCode) { KakaoTokens tokenInfo = kakaoApiClient.fetchToken(authCode); KakaoMemberResponse response = kakaoApiClient.fetchMember(tokenInfo.accessToken()); diff --git a/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/dto/KakaoMemberResponse.java b/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/dto/KakaoMemberResponse.java index 0863930e..12acbdea 100644 --- a/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/dto/KakaoMemberResponse.java +++ b/src/main/java/coffeemeet/server/oauth/infrastructure/kakao/dto/KakaoMemberResponse.java @@ -1,6 +1,6 @@ package coffeemeet.server.oauth.infrastructure.kakao.dto; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; @@ -11,8 +11,8 @@ public record KakaoMemberResponse( KakaoAccount kakaoAccount ) { - public OAuthInfoDto.Response toOAuthInfoResponse() { - return OAuthInfoDto.Response.of( + public OAuthUserInfoDto.Response toOAuthInfoResponse() { + return OAuthUserInfoDto.Response.of( kakaoAccount.name, kakaoAccount.profile.profileImageUrl, kakaoAccount.birthyear, diff --git a/src/main/java/coffeemeet/server/oauth/service/OAuthService.java b/src/main/java/coffeemeet/server/oauth/service/OAuthService.java index 29aac986..07b7b1c4 100644 --- a/src/main/java/coffeemeet/server/oauth/service/OAuthService.java +++ b/src/main/java/coffeemeet/server/oauth/service/OAuthService.java @@ -2,7 +2,7 @@ import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite; import coffeemeet.server.oauth.client.OAuthMemberClientComposite; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -18,7 +18,7 @@ public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) { return authCodeRequestUrlProviderComposite.provide(oAuthProvider); } - public OAuthInfoDto.Response getOAuthInfo(OAuthProvider oAuthProvider, String authCode) { + public OAuthUserInfoDto.Response getOAuthInfo(OAuthProvider oAuthProvider, String authCode) { return oauthMemberClientComposite.fetch(oAuthProvider, authCode); } diff --git a/src/main/java/coffeemeet/server/user/service/UserService.java b/src/main/java/coffeemeet/server/user/service/UserService.java index ae0130be..9cf0c36d 100644 --- a/src/main/java/coffeemeet/server/user/service/UserService.java +++ b/src/main/java/coffeemeet/server/user/service/UserService.java @@ -11,7 +11,7 @@ import coffeemeet.server.interest.domain.Keyword; import coffeemeet.server.interest.repository.InterestRepository; import coffeemeet.server.interest.service.InterestService; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.oauth.service.OAuthService; import coffeemeet.server.user.domain.Birth; import coffeemeet.server.user.domain.Email; @@ -94,7 +94,7 @@ public void updateProfileInfo(Long userId, String nickname, String name, @Transactional public AuthTokens signup(SignupRequest request) { - OAuthInfoDto.Response response = oAuthService.getOAuthInfo(request.oAuthProvider(), + OAuthUserInfoDto.Response response = oAuthService.getOAuthInfo(request.oAuthProvider(), request.authCode()); checkDuplicatedUser(response); @@ -113,7 +113,7 @@ public AuthTokens signup(SignupRequest request) { } public AuthTokens login(OAuthProvider oAuthProvider, String authCode) { - OAuthInfoDto.Response response = oAuthService.getOAuthInfo(oAuthProvider, authCode); + OAuthUserInfoDto.Response response = oAuthService.getOAuthInfo(oAuthProvider, authCode); User foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId( response.oAuthProvider(), response.oAuthProviderId()).orElseThrow( () -> new IllegalArgumentException( @@ -134,7 +134,7 @@ public void checkDuplicatedNickname(String nickname) { } } - public void checkDuplicatedUser(OAuthInfoDto.Response response) { + public void checkDuplicatedUser(OAuthUserInfoDto.Response response) { if (userRepository.existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId( response.oAuthProvider(), response.oAuthProviderId())) { throw new IllegalArgumentException(ALREADY_REGISTERED_MESSAGE); diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java index c09d143a..f2177a86 100644 --- a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java @@ -1,12 +1,12 @@ package coffeemeet.server.common.fixture.dto; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import org.instancio.Instancio; public class OAuthInfoDtoFixture { - public static OAuthInfoDto.Response response() { - return Instancio.of(OAuthInfoDto.Response.class) + public static OAuthUserInfoDto.Response response() { + return Instancio.of(OAuthUserInfoDto.Response.class) .create(); } diff --git a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java index fcf9b179..7ec0762a 100644 --- a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java +++ b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java @@ -4,7 +4,7 @@ import static org.mockito.BDDMockito.given; import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import java.util.Collections; import java.util.HashSet; @@ -34,7 +34,7 @@ void fetchTest() { // given String authCode = "authCode"; OAuthProvider oAuthProvider = OAuthProvider.KAKAO; - OAuthInfoDto.Response response = OAuthInfoDtoFixture.response(); + OAuthUserInfoDto.Response response = OAuthInfoDtoFixture.response(); given(client.oAuthProvider()).willReturn(OAuthProvider.KAKAO); given(client.fetch(authCode)).willReturn(response); @@ -43,7 +43,7 @@ void fetchTest() { composite = new OAuthMemberClientComposite(clients); // when - OAuthInfoDto.Response expectedResponse = composite.fetch(oAuthProvider, authCode); + OAuthUserInfoDto.Response expectedResponse = composite.fetch(oAuthProvider, authCode); // then assertThat(response).isEqualTo(expectedResponse); diff --git a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java index ea79dd99..e40c8ec2 100644 --- a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java +++ b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java @@ -6,7 +6,7 @@ import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite; import coffeemeet.server.oauth.client.OAuthMemberClientComposite; -import coffeemeet.server.oauth.dto.OAuthInfoDto; +import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -48,7 +48,7 @@ void getOAuthInfoTest() { // given OAuthProvider oAuthProvider = OAuthProvider.KAKAO; String authCode = "authCode"; - OAuthInfoDto.Response oAuthInfoResponse = OAuthInfoDtoFixture.response(); + OAuthUserInfoDto.Response oAuthInfoResponse = OAuthInfoDtoFixture.response(); given(oAuthMemberClientComposite.fetch(oAuthProvider, authCode)).willReturn(oAuthInfoResponse); From a46ab5928e5636185bfa45dccc73a368a09cff25 Mon Sep 17 00:00:00 2001 From: yumyeonghan Date: Fri, 27 Oct 2023 18:47:01 +0900 Subject: [PATCH 16/16] =?UTF-8?q?refactor:=20OAuthUserInfoDtoFixture=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{OAuthInfoDtoFixture.java => OAuthUserInfoDtoFixture.java} | 2 +- .../server/oauth/client/OAuthMemberClientCompositeTest.java | 4 ++-- .../coffeemeet/server/oauth/service/OAuthServiceTest.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/test/java/coffeemeet/server/common/fixture/dto/{OAuthInfoDtoFixture.java => OAuthUserInfoDtoFixture.java} (87%) diff --git a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthUserInfoDtoFixture.java similarity index 87% rename from src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java rename to src/test/java/coffeemeet/server/common/fixture/dto/OAuthUserInfoDtoFixture.java index f2177a86..4ddb409f 100644 --- a/src/test/java/coffeemeet/server/common/fixture/dto/OAuthInfoDtoFixture.java +++ b/src/test/java/coffeemeet/server/common/fixture/dto/OAuthUserInfoDtoFixture.java @@ -3,7 +3,7 @@ import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import org.instancio.Instancio; -public class OAuthInfoDtoFixture { +public class OAuthUserInfoDtoFixture { public static OAuthUserInfoDto.Response response() { return Instancio.of(OAuthUserInfoDto.Response.class) diff --git a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java index 7ec0762a..96952a18 100644 --- a/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java +++ b/src/test/java/coffeemeet/server/oauth/client/OAuthMemberClientCompositeTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; -import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; +import coffeemeet.server.common.fixture.dto.OAuthUserInfoDtoFixture; import coffeemeet.server.oauth.dto.OAuthUserInfoDto; import coffeemeet.server.user.domain.OAuthProvider; import java.util.Collections; @@ -34,7 +34,7 @@ void fetchTest() { // given String authCode = "authCode"; OAuthProvider oAuthProvider = OAuthProvider.KAKAO; - OAuthUserInfoDto.Response response = OAuthInfoDtoFixture.response(); + OAuthUserInfoDto.Response response = OAuthUserInfoDtoFixture.response(); given(client.oAuthProvider()).willReturn(OAuthProvider.KAKAO); given(client.fetch(authCode)).willReturn(response); diff --git a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java index e40c8ec2..66176595 100644 --- a/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java +++ b/src/test/java/coffeemeet/server/oauth/service/OAuthServiceTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; -import coffeemeet.server.common.fixture.dto.OAuthInfoDtoFixture; +import coffeemeet.server.common.fixture.dto.OAuthUserInfoDtoFixture; import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite; import coffeemeet.server.oauth.client.OAuthMemberClientComposite; import coffeemeet.server.oauth.dto.OAuthUserInfoDto; @@ -48,7 +48,7 @@ void getOAuthInfoTest() { // given OAuthProvider oAuthProvider = OAuthProvider.KAKAO; String authCode = "authCode"; - OAuthUserInfoDto.Response oAuthInfoResponse = OAuthInfoDtoFixture.response(); + OAuthUserInfoDto.Response oAuthInfoResponse = OAuthUserInfoDtoFixture.response(); given(oAuthMemberClientComposite.fetch(oAuthProvider, authCode)).willReturn(oAuthInfoResponse);