Skip to content

Commit

Permalink
Merge pull request #19 from HongDam-org/feat/auth-path-api-docs
Browse files Browse the repository at this point in the history
[DOCS] auth and path api docs
  • Loading branch information
ohksj77 authored Sep 21, 2023
2 parents 412d813 + 22689a5 commit 27087f5
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend/src/docs/auth.adoc
Original file line number Diff line number Diff line change
@@ -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']
2 changes: 2 additions & 0 deletions backend/src/docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
HongDamJin

include::plan.adoc[]
include::auth.adoc[]
include::path.adoc[]
9 changes: 9 additions & 0 deletions backend/src/docs/path.adoc
Original file line number Diff line number Diff line change
@@ -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']
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}

0 comments on commit 27087f5

Please sign in to comment.