diff --git a/backend/src/docs/auth.adoc b/backend/src/docs/auth.adoc new file mode 100644 index 00000000..147525fc --- /dev/null +++ b/backend/src/docs/auth.adoc @@ -0,0 +1,15 @@ +:doctype: book +:icons: font +:source-highlighter: highlightjs +:toc: left +:toclevels: 4 + +== Auth +=== 리프레시 토큰 갱신 +operation::post refresh token[snippets='http-request,http-response'] + +=== 첫 로그인 +operation::post save member[snippets='http-request,http-response'] + +=== 저장된 멤버 로그인 +operation::post login[snippets='http-request,http-response'] diff --git a/backend/src/docs/index.adoc b/backend/src/docs/index.adoc index 265625de..83b54f23 100644 --- a/backend/src/docs/index.adoc +++ b/backend/src/docs/index.adoc @@ -9,3 +9,5 @@ HongDamJin include::plan.adoc[] +include::auth.adoc[] +include::path.adoc[] diff --git a/backend/src/docs/path.adoc b/backend/src/docs/path.adoc new file mode 100644 index 00000000..dd321d28 --- /dev/null +++ b/backend/src/docs/path.adoc @@ -0,0 +1,9 @@ +:doctype: book +:icons: font +:source-highlighter: highlightjs +:toc: left +:toclevels: 4 + +== Path +=== 경로 검색 +operation::post search path[snippets='http-request,http-response'] diff --git a/backend/src/test/java/com/twtw/backend/domain/member/controller/AuthControllerTest.java b/backend/src/test/java/com/twtw/backend/domain/member/controller/AuthControllerTest.java new file mode 100644 index 00000000..0b12fe08 --- /dev/null +++ b/backend/src/test/java/com/twtw/backend/domain/member/controller/AuthControllerTest.java @@ -0,0 +1,120 @@ +package com.twtw.backend.domain.member.controller; + +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest; +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.twtw.backend.domain.member.dto.request.MemberSaveRequest; +import com.twtw.backend.domain.member.dto.request.OAuthRequest; +import com.twtw.backend.domain.member.dto.request.TokenRequest; +import com.twtw.backend.domain.member.dto.response.TokenDto; +import com.twtw.backend.domain.member.entity.AuthType; +import com.twtw.backend.domain.member.entity.Role; +import com.twtw.backend.domain.member.service.AuthService; +import com.twtw.backend.support.docs.RestDocsTest; + +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.test.web.servlet.ResultActions; + +@DisplayName("AuthController의") +@WebMvcTest(AuthController.class) +class AuthControllerTest extends RestDocsTest { + @MockBean private AuthService authService; + + @Test + @DisplayName("JWT 리프레시 API가 수행되는가") + void authorize() throws Exception { + // given + final TokenDto expected = new TokenDto("access.token.value", "refresh.token.value"); + given(authService.refreshToken(any(), any())).willReturn(expected); + + // when + final ResultActions perform = + mockMvc.perform( + post("/auth/refresh") + .content( + toRequestBody( + new TokenRequest( + "access.token.value", + "refresh.token.value"))) + .contentType(MediaType.APPLICATION_JSON)); + + // then + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.accessToken").isString()) + .andExpect(jsonPath("$.refreshToken").isString()); + + // docs + perform.andDo(print()) + .andDo(document("post refresh token", getDocumentRequest(), getDocumentResponse())); + } + + @Test + @DisplayName("첫 로그인 API가 수행되는가") + void saveMember() throws Exception { + // given + final TokenDto expected = new TokenDto("access.token.value", "refresh.token.value"); + given(authService.saveMember(any())).willReturn(expected); + + // when + final ResultActions perform = + mockMvc.perform( + post("/auth/save") + .content( + toRequestBody( + new MemberSaveRequest( + "정해진", + "http://some-url-to-profile-image", + "01000000000", + Role.ROLE_USER, + new OAuthRequest( + "client-id", AuthType.APPLE)))) + .contentType(MediaType.APPLICATION_JSON)); + + // then + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.accessToken").isString()) + .andExpect(jsonPath("$.refreshToken").isString()); + + // docs + perform.andDo(print()) + .andDo(document("post save member", getDocumentRequest(), getDocumentResponse())); + } + + @Test + @DisplayName("멤버가 저장된 상태에서의 로그인 API가 수행되는가") + void afterSocialLogin() throws Exception { + // given + final TokenDto expected = new TokenDto("access.token.value", "refresh.token.value"); + given(authService.getTokenByOAuth(any())).willReturn(expected); + + // when + final ResultActions perform = + mockMvc.perform( + post("/auth/login") + .content( + toRequestBody( + new OAuthRequest("client-id", AuthType.KAKAO))) + .contentType(MediaType.APPLICATION_JSON)); + + // then + perform.andExpect(status().isOk()) + .andExpect(jsonPath("$.accessToken").isString()) + .andExpect(jsonPath("$.refreshToken").isString()); + + // docs + perform.andDo(print()) + .andDo(document("post login", getDocumentRequest(), getDocumentResponse())); + } +} diff --git a/backend/src/test/java/com/twtw/backend/domain/path/controller/PathControllerTest.java b/backend/src/test/java/com/twtw/backend/domain/path/controller/PathControllerTest.java new file mode 100644 index 00000000..17445c7b --- /dev/null +++ b/backend/src/test/java/com/twtw/backend/domain/path/controller/PathControllerTest.java @@ -0,0 +1,75 @@ +package com.twtw.backend.domain.path.controller; + +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest; +import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.twtw.backend.domain.path.dto.client.*; +import com.twtw.backend.domain.path.service.PathService; +import com.twtw.backend.support.docs.RestDocsTest; + +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.test.web.servlet.ResultActions; + +import java.util.List; +import java.util.Map; + +@DisplayName("PathController의") +@WebMvcTest(PathController.class) +class PathControllerTest extends RestDocsTest { + @MockBean private PathService pathService; + + @Test + @DisplayName("경로 검색 API가 수행되는가") + void searchPath() throws Exception { + // given + final SearchPathResponse expected = + new SearchPathResponse( + 0, + "", + "", + Map.of( + "", + new RouteUnitEnt[] { + new RouteUnitEnt( + new Summary(), + List.of( + new Double[] {0.0, 0.0}, + new Double[] {0.0, 0.0})) + })); + + given(pathService.searchPath(any())).willReturn(expected); + + // when + final ResultActions perform = + mockMvc.perform( + post("/paths/search") + .content( + toRequestBody( + new SearchPathRequest( + "", + "", + "", + SearchPathOption.TRAFAST, + SearchPathFuel.DIESEL, + 0))) + .contentType(MediaType.APPLICATION_JSON)); + + // then + perform.andExpect(status().isOk()); + + // docs + perform.andDo(print()) + .andDo(document("post search path", getDocumentRequest(), getDocumentResponse())); + } +}