Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GETP-142 test: Access Token과 Refresh Token 재발급 컨트롤러 단위 테스트 및 API 문서 작성 #69

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/docs/asciidoc/auth/reissue-access-token.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
operation::/reissue-access-token/reissue-access-token[snippets="http-request,request-headers,http-response,response-fields-data"]
1 change: 1 addition & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include::{docdir}/auth/login.adoc[]
=== 회원 가입 시 이메일 인증 코드 전송

=== Access Token 및 Refresh Token 재발급
include::{docdir}/auth/reissue-access-token.adoc[]

== 사용자

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import es.princip.getp.domain.auth.service.AuthService;
import es.princip.getp.global.util.ApiResponse;
import es.princip.getp.global.util.ApiResponse.ApiSuccessResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -44,6 +45,7 @@ public ResponseEntity<ApiSuccessResult<Token>> login(@RequestBody @Valid LoginRe
* @return 재발급 된 Access Token과 Refresh Token
*/
@PostMapping("/reissue")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<ApiSuccessResult<Token>> reissueAccessToken(HttpServletRequest servletRequest) {
Token token = authService.reissueAccessToken(servletRequest);
String authorization = token.grantType() + " " + token.accessToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import es.princip.getp.domain.auth.service.AuthService;
import es.princip.getp.global.controller.ErrorCodeController;
import es.princip.getp.global.exception.BusinessLogicException;
import es.princip.getp.global.mock.WithCustomMockUser;
import es.princip.getp.global.support.AbstractControllerTest;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -17,8 +19,11 @@

import static es.princip.getp.global.support.ErrorCodeFields.errorCodeFields;
import static es.princip.getp.global.support.FieldDescriptorHelper.getDescriptor;
import static es.princip.getp.global.support.HeaderDescriptorHelper.authorizationHeaderDescriptor;
import static es.princip.getp.global.support.PayloadDocumentationHelper.responseFields;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -84,4 +89,38 @@ void login_WhenEmailAndPasswordIsIncorrect_ShouldFail() throws Exception {
.andDo(print());
}
}

@DisplayName("사용자는")
@Nested
class ReissueAccessToken {

@DisplayName("로그인 유지를 할 수 있다.")
@Test
@WithCustomMockUser
void reissueAccessToken() throws Exception {
given(authService.reissueAccessToken(any(HttpServletRequest.class)))
.willReturn(Token.builder()
.grantType("Bearer")
.accessToken("access-token")
.refreshToken("refresh-token")
.build());

mockMvc.perform(post("/auth/reissue")
.header("Authorization", "Bearer ${ACCESS_TOKEN}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andDo(
restDocs.document(
requestHeaders(authorizationHeaderDescriptor()),
responseFields(
getDescriptor("grantType", "토큰 타입", Token.class)
.attributes(key("format").value("Bearer")),
getDescriptor("accessToken", "Access Token", Token.class),
getDescriptor("refreshToken", "Refresh Token", Token.class)
)
)
)
.andDo(print());
}
}
}