-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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) | ||
|