Skip to content

Commit

Permalink
βœ… add test code for restdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiSeong committed Jul 27, 2024
1 parent 46af48d commit d8cb2fc
Showing 1 changed file with 71 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,41 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseToken;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import java.time.LocalDate;
import java.util.Map;
import net.pengcook.RestDocsSetting;
import net.pengcook.authentication.domain.JwtTokenManager;
import net.pengcook.authentication.domain.TokenPayload;
import net.pengcook.authentication.domain.TokenType;
import net.pengcook.authentication.dto.GoogleLoginRequest;
import net.pengcook.authentication.dto.GoogleLoginResponse;
import net.pengcook.authentication.dto.GoogleSignUpRequest;
import net.pengcook.authentication.dto.GoogleSignUpResponse;
import net.pengcook.authentication.dto.TokenRefreshRequest;
import net.pengcook.authentication.dto.TokenRefreshResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.jdbc.Sql;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Sql("/data/users.sql")
class LoginControllerTest {
class LoginControllerTest extends RestDocsSetting {

@LocalServerPort
int port;
@MockBean
private FirebaseAuth firebaseAuth;
@Autowired
private JwtTokenManager jwtTokenManager;

@BeforeEach
void setUp() {
RestAssured.port = port;
}

@Test
@DisplayName("이미 κ°€μž…λœ κ³„μ •μœΌλ‘œ λ‘œκ·ΈμΈν•˜λ©΄ 이미 κ°€μž…λ˜μ—ˆλ‹€κ³  μ•Œλ¦¬κ³  access tokenκ³Ό refresh token을 λ°˜ν™˜ν•œλ‹€.")
void loginWithGoogleWithEmailAlreadyRegistered() throws FirebaseAuthException {
Expand All @@ -56,7 +49,17 @@ void loginWithGoogleWithEmailAlreadyRegistered() throws FirebaseAuthException {
when(firebaseToken.getEmail()).thenReturn(email);
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

GoogleLoginResponse actual = RestAssured.given().log().all()
GoogleLoginResponse actual = RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
requestFields(
fieldWithPath("idToken").description("Google ID Token")
),
responseFields(
fieldWithPath("accessToken").description("JWT Access Token"),
fieldWithPath("refreshToken").description("JWT Refresh Token"),
fieldWithPath("registered").description("μ‚¬μš©μž 등둝 μ—¬λΆ€")
)
))
.contentType(ContentType.JSON)
.body(new GoogleLoginRequest(idToken))
.when().post("/api/oauth/google/login")
Expand All @@ -82,7 +85,17 @@ void loginWithGoogleWithEmailNotRegistered() throws FirebaseAuthException {
when(firebaseToken.getEmail()).thenReturn(email);
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

GoogleLoginResponse actual = RestAssured.given().log().all()
GoogleLoginResponse actual = RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
requestFields(
fieldWithPath("idToken").description("Google ID Token")
),
responseFields(
fieldWithPath("accessToken").description("JWT Access Token").optional(),
fieldWithPath("refreshToken").description("JWT Refresh Token").optional(),
fieldWithPath("registered").description("μ‚¬μš©μž 등둝 μ—¬λΆ€")
)
))
.contentType(ContentType.JSON)
.body(new GoogleLoginRequest(idToken))
.when().post("/api/oauth/google/login")
Expand Down Expand Up @@ -116,7 +129,27 @@ void signUpWithGoogle() throws FirebaseAuthException {
when(firebaseToken.getPicture()).thenReturn("new_face.jpg");
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

GoogleSignUpResponse actual = RestAssured.given().log().all()
GoogleSignUpResponse actual = RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
requestFields(
fieldWithPath("idToken").description("Google ID Token"),
fieldWithPath("username").description("μ‚¬μš©μž 아이디"),
fieldWithPath("nickname").description("μ‚¬μš©μž λ‹‰λ„€μž„"),
fieldWithPath("birthday").description("생년월일"),
fieldWithPath("country").description("κ΅­κ°€")
),
responseFields(
fieldWithPath("id").description("μ‚¬μš©μž ID"),
fieldWithPath("email").description("μ‚¬μš©μž 이메일"),
fieldWithPath("username").description("μ‚¬μš©μž 아이디"),
fieldWithPath("nickname").description("μ‚¬μš©μž λ‹‰λ„€μž„"),
fieldWithPath("image").description("μ‚¬μš©μž ν”„λ‘œν•„ 이미지"),
fieldWithPath("birthday").description("μ‚¬μš©μž 생년월일"),
fieldWithPath("country").description("μ‚¬μš©μž κ΅­κ°€"),
fieldWithPath("accessToken").description("JWT Access Token"),
fieldWithPath("refreshToken").description("JWT Refresh Token")
)
))
.contentType(ContentType.JSON)
.body(request)
.when().post("/api/oauth/google/sign-up")
Expand Down Expand Up @@ -146,7 +179,16 @@ void signUpWithGoogleWhenEmailAleadyRegistered() throws FirebaseAuthException {
when(firebaseToken.getPicture()).thenReturn("loki.jpg");
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

RestAssured.given().log().all()
RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
requestFields(
fieldWithPath("idToken").description("Google ID Token"),
fieldWithPath("username").description("μ‚¬μš©μž 아이디"),
fieldWithPath("nickname").description("μ‚¬μš©μž λ‹‰λ„€μž„"),
fieldWithPath("birthday").description("생년월일"),
fieldWithPath("country").description("κ΅­κ°€")
)
))
.contentType(ContentType.JSON)
.body(request)
.when().post("/api/oauth/google/sign-up")
Expand All @@ -159,9 +201,18 @@ void signUpWithGoogleWhenEmailAleadyRegistered() throws FirebaseAuthException {
void refresh() {
String refreshToken = jwtTokenManager.createToken(new TokenPayload(1L, "[email protected]", TokenType.REFRESH));

TokenRefreshResponse response = RestAssured.given().log().all()
TokenRefreshResponse response = RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
requestFields(
fieldWithPath("refreshToken").description("JWT Refresh Token")
),
responseFields(
fieldWithPath("accessToken").description("μƒˆλ‘œ λ°œκΈ‰λœ JWT Access Token"),
fieldWithPath("refreshToken").description("μƒˆλ‘œ λ°œκΈ‰λœ JWT Refresh Token")
)
))
.contentType(ContentType.JSON)
.body(Map.of("refreshToken", refreshToken))
.body(new TokenRefreshRequest(refreshToken))
.when().post("/api/token/refresh")
.then().log().all()
.statusCode(200)
Expand Down

0 comments on commit d8cb2fc

Please sign in to comment.