From 533698920a0c754c7b028e904cefe9af25021bbf Mon Sep 17 00:00:00 2001 From: yoondgu Date: Tue, 10 Oct 2023 02:06:29 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Login=20RectDocs=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=ED=95=B4,=20=EC=83=81=ED=83=9C=EC=BD=94=EB=93=9C=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - MockMvcTest에서는 Cookie를 .header로 담을 수 없다 - Cookie는 US-ASCII chars만 허용한다 --- .../presentation/LoginControllerTest.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/backend/src/test/java/com/mapbefine/mapbefine/auth/presentation/LoginControllerTest.java b/backend/src/test/java/com/mapbefine/mapbefine/auth/presentation/LoginControllerTest.java index cf4febe0c..7da655ee4 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/auth/presentation/LoginControllerTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/auth/presentation/LoginControllerTest.java @@ -2,7 +2,6 @@ import static com.mapbefine.mapbefine.oauth.domain.OauthServerType.KAKAO; -import static org.apache.http.cookie.SM.COOKIE; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -12,12 +11,14 @@ import com.mapbefine.mapbefine.common.RestDocsIntegration; import com.mapbefine.mapbefine.member.dto.response.MemberDetailResponse; import com.mapbefine.mapbefine.oauth.application.OauthService; +import jakarta.servlet.http.Cookie; import java.time.LocalDateTime; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; class LoginControllerTest extends RestDocsIntegration { @@ -41,9 +42,9 @@ void redirection() throws Exception { ); // when then - mockMvc.perform( - MockMvcRequestBuilders.get("/oauth/kakao") - ).andDo(restDocs.document()); + mockMvc.perform(MockMvcRequestBuilders.get("/oauth/kakao")) + .andExpect(MockMvcResultMatchers.status().isFound()) + .andDo(restDocs.document()); } @Test @@ -69,42 +70,42 @@ void login() throws Exception { given(tokenService.issueTokens(memberDetailResponse.id())).willReturn(loginTokens); // when then - mockMvc.perform( - MockMvcRequestBuilders.get("/oauth/login/kakao") - .param("code", code) - ).andDo(restDocs.document()); + mockMvc.perform(MockMvcRequestBuilders.get("/oauth/login/kakao") + .param("code", code)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(restDocs.document()); } + @Test @DisplayName("만료된 Access Token과 유효한 RefreshToken인 경우, 새로운 토큰들을 발행한다.") void reissueTokens() throws Exception { - AccessToken expiredAccessToken = new AccessToken("만료된 액세스 토큰"); - String refreshToken = "리프레시 토큰"; + AccessToken expiredAccessToken = new AccessToken("expired-access-token"); + String refreshToken = "refresh-token"; - LoginTokens reissuedTokens = new LoginTokens("재발급된 액세스 토큰", "재발급된 리프레시 토큰"); + LoginTokens reissuedTokens = new LoginTokens("reissued-access-token", "reissued-refresh-token"); given(tokenService.reissueToken(any(), any())).willReturn(reissuedTokens); // then - mockMvc.perform( - MockMvcRequestBuilders.post("/refresh-token") - .header(COOKIE, refreshToken) + mockMvc.perform(MockMvcRequestBuilders.post("/refresh-token") + .cookie(new Cookie("refresh-token", refreshToken)) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(expiredAccessToken)) - ).andDo(restDocs.document()); + .content(objectMapper.writeValueAsString(expiredAccessToken))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(restDocs.document()); } @Test - @DisplayName("유효한 Access Token과 유효한 RefreshToken인 경우, RefreshTokenRepository에서 RefreshTokend을 삭제한다") + @DisplayName("유효한 Access Token과 유효한 RefreshToken으로 로그아웃을 요청한다.") void logout() throws Exception { - AccessToken accessToken = new AccessToken("access token"); + AccessToken accessToken = new AccessToken("access-token"); - mockMvc.perform( - MockMvcRequestBuilders.post("/logout") - .header(COOKIE, testAuthHeaderProvider.createRefreshToken()) + mockMvc.perform(MockMvcRequestBuilders.post("/logout") + .cookie(new Cookie("refresh-token", testAuthHeaderProvider.createRefreshToken())) .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(accessToken)) - ).andDo(restDocs.document()); - + .content(objectMapper.writeValueAsString(accessToken))) + .andExpect(MockMvcResultMatchers.status().isNoContent()) + .andDo(restDocs.document()); } }