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

[BE] docs: GitHub 연동, 자신이 만든 리뷰 그룹 목록 조회 API 문서 작성 #1014

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
7 changes: 7 additions & 0 deletions backend/src/docs/asciidoc/auth.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
==== 깃허브로 로그인/회원가입

operation::github-auth[snippets="curl-request,request-fields,http-response"]

==== 로그아웃

operation::logout[snippets="curl-request,request-cookies,http-response"]
4 changes: 4 additions & 0 deletions backend/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ include::review-gather.adoc[]
=== 답변 하이라이트

include::highlight-answers.adoc[]

== 인증

include::auth.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package reviewme.auth.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reviewme.auth.service.AuthService;
import reviewme.auth.service.dto.GithubCodeRequest;

@RestController
@RequiredArgsConstructor
public class AuthController {

private final AuthService authService;

@PostMapping("/v2/auth/github")
public ResponseEntity<Void> authWithGithub(
@Valid @RequestBody GithubCodeRequest request,
HttpServletRequest httpRequest
) {
return ResponseEntity.ok().build();
}
Comment on lines +19 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

산초가 올려준 디스커션의 인증 과정중 아래의 1~3번을 이 메서드에서 모두 하는것인지 궁금해요!(아직 구현이 안해서 헷갈려서 물어봅니당)

  1. auth code를 받음
  2. 그걸로 깃헙에 accessToken을 요청함
  3. 받은 accessToken으로 깃헙에 사용자의 정보를 요청해서 세션을 설정해 응답함

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞아요👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 이때 사용자 정보나 로그인 정보같은 것은 안내려줘도 괜찮나요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kimprodp
사용자 정보를 따로 넘겨주기보다, 클라이언트와는 JSESSION_ID로만 로그인한 사용자에 대한 통신을 하면 된다 생각해요!


@PostMapping("/v2/auth/logout")
public ResponseEntity<Void> logout(
HttpServletRequest httpRequest
) {
HttpSession session = httpRequest.getSession();
session.invalidate();
nayonsoso marked this conversation as resolved.
Show resolved Hide resolved

ResponseCookie cookie = ResponseCookie.from("JSESSIONID", "")
.path("/")
.maxAge(0)
.secure(true)
.httpOnly(true)
.build();

return ResponseEntity
.noContent()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.build();
}
nayonsoso marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package reviewme.auth.service;

import org.springframework.stereotype.Service;

@Service
public class AuthService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package reviewme.auth.service.dto;

import jakarta.validation.constraints.NotBlank;

public record GithubCodeRequest(
@NotBlank(message = "깃허브 임시 코드를 입력해주세요.")
String code) {
}
8 changes: 7 additions & 1 deletion backend/src/test/java/reviewme/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import reviewme.auth.controller.AuthController;
import reviewme.auth.service.AuthService;
import reviewme.highlight.controller.HighlightController;
import reviewme.highlight.service.HighlightService;
import reviewme.review.controller.ReviewController;
Expand All @@ -48,7 +50,8 @@
ReviewController.class,
TemplateController.class,
SectionController.class,
HighlightController.class
HighlightController.class,
AuthController.class
})
@ExtendWith(RestDocumentationExtension.class)
public abstract class ApiTest {
Expand Down Expand Up @@ -85,6 +88,9 @@ public abstract class ApiTest {
@MockBean
protected HighlightService highlightService;

@MockBean
protected AuthService authService;

@MockBean
private ReviewGroupSessionResolver reviewGroupSessionResolver;

Expand Down
61 changes: 61 additions & 0 deletions backend/src/test/java/reviewme/api/AuthApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package reviewme.api;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.restdocs.cookies.CookieDocumentation.cookieWithName;
import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;

import org.junit.jupiter.api.Test;
import org.springframework.restdocs.cookies.CookieDescriptor;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.payload.FieldDescriptor;

public class AuthApiTest extends ApiTest {

@Test
void 깃허브로_인증한다() {
String request = """
{
"code": "github_auth_code"
}
""";

FieldDescriptor[] requestFieldDescriptors = {
fieldWithPath("code").description("깃허브 임시 인증 코드"),
};

RestDocumentationResultHandler handler = document(
"github-auth",
requestFields(requestFieldDescriptors)
);

givenWithSpec().log().all()
.body(request)
.when().post("/v2/auth/github")
.then().log().all()
.apply(handler)
.statusCode(200);
}

@Test
void 로그아웃한다() {
CookieDescriptor[] cookieDescriptors = {
cookieWithName("JSESSIONID").description("세션 ID")
};

RestDocumentationResultHandler handler = document(
"logout",
requestCookies(cookieDescriptors)
);

givenWithSpec().log().all()
.cookie("JSESSIONID", "SESSION12345678")
.when().post("/v2/auth/logout")
.then().log().all()
.apply(handler)
.statusCode(204)
.header("Set-Cookie", containsString("JSESSIONID=; Path=/; Max-Age=0"));
}
}