-
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.
GETP-141 test: 로그인 컨트롤러 단위 테스트 및 API 문서 작성 (#66)
* GETP-141 rename: fixture 클래스를 각 도메인의 fixture 패키지로 이동 * GETP-141 fix: bean validation 시 커스텀 메시지가 출력되지 않는 오류 수정 * GETP-141 test: 로그인 컨트롤러 단위 테스트 및 API 문서 작성
- Loading branch information
Showing
16 changed files
with
156 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
operation::/login/login[snippets="http-request,request-fields,http-response,response-fields-data"] | ||
operation::/login/login-error-code[snippets="error-code-fields"] |
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
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
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
17 changes: 8 additions & 9 deletions
17
src/main/java/es/princip/getp/global/exception/handler/BusinessLogicExceptionHandler.java
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 |
---|---|---|
@@ -1,22 +1,21 @@ | ||
package es.princip.getp.global.exception.handler; | ||
|
||
import es.princip.getp.global.exception.BusinessLogicException; | ||
import es.princip.getp.global.exception.ErrorCode; | ||
import org.springframework.http.HttpStatus; | ||
import es.princip.getp.global.util.ApiResponse; | ||
import es.princip.getp.global.util.ApiResponse.ApiErrorResult; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import es.princip.getp.global.exception.BusinessLogicException; | ||
import es.princip.getp.global.util.ApiResponse; | ||
import es.princip.getp.global.util.ApiResponse.ApiErrorResult; | ||
|
||
@RestControllerAdvice | ||
public class BusinessLogicExceptionHandler { | ||
|
||
@ExceptionHandler(BusinessLogicException.class) | ||
public ResponseEntity<ApiErrorResult> businessLogicException( | ||
final BusinessLogicException businessLogicException) { | ||
ErrorCode errorCode = businessLogicException.getErrorCode(); | ||
HttpStatus status = errorCode.status(); | ||
return ResponseEntity.status(status).body(ApiResponse.error(errorCode)); | ||
public ResponseEntity<ApiErrorResult> validationException( | ||
final BusinessLogicException exception) { | ||
ErrorCode errorCode = exception.getErrorCode(); | ||
return ResponseEntity.status(errorCode.status()) | ||
.body(ApiResponse.error(errorCode.status(), errorCode.description())); | ||
} | ||
} |
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
11 changes: 7 additions & 4 deletions
11
src/main/java/es/princip/getp/global/validator/annotation/Hyperlink.java
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
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
87 changes: 87 additions & 0 deletions
87
src/test/java/es/princip/getp/domain/auth/controller/AuthControllerTest.java
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package es.princip.getp.domain.auth.controller; | ||
|
||
import es.princip.getp.domain.auth.dto.request.LoginRequest; | ||
import es.princip.getp.domain.auth.dto.response.Token; | ||
import es.princip.getp.domain.auth.exception.LoginErrorCode; | ||
import es.princip.getp.domain.auth.fixture.LoginFixture; | ||
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.support.AbstractControllerTest; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
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 static es.princip.getp.global.support.ErrorCodeFields.errorCodeFields; | ||
import static es.princip.getp.global.support.FieldDescriptorHelper.getDescriptor; | ||
import static es.princip.getp.global.support.PayloadDocumentationHelper.responseFields; | ||
import static org.mockito.BDDMockito.given; | ||
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; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest({AuthController.class, ErrorCodeController.class}) | ||
class AuthControllerTest extends AbstractControllerTest { | ||
|
||
@MockBean | ||
private AuthService authService; | ||
|
||
@DisplayName("사용자는") | ||
@Nested | ||
class Login { | ||
|
||
@Test | ||
void loginErrorCode() throws Exception { | ||
mockMvc.perform(get("/error-code/login")) | ||
.andDo(restDocs.document(errorCodeFields(LoginErrorCode.values()))); | ||
} | ||
|
||
@DisplayName("로그인을 할 수 있다.") | ||
@Test | ||
void login() throws Exception { | ||
given(authService.login(LoginFixture.createLoginRequest())) | ||
.willReturn(Token.builder() | ||
.grantType("Bearer") | ||
.accessToken("access-token") | ||
.refreshToken("refresh-token") | ||
.build()); | ||
|
||
mockMvc.perform(post("/auth/login") | ||
.content(objectMapper.writeValueAsString(LoginFixture.createLoginRequest())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isCreated()) | ||
.andDo( | ||
restDocs.document( | ||
requestFields( | ||
getDescriptor("email", "이메일", LoginRequest.class), | ||
getDescriptor("password", "비밀번호", LoginRequest.class) | ||
), | ||
responseFields( | ||
getDescriptor("grantType", "토큰 타입", Token.class) | ||
.attributes(key("format").value("Bearer")), | ||
getDescriptor("accessToken", "Access Token", Token.class), | ||
getDescriptor("refreshToken", "Refresh Token", Token.class) | ||
) | ||
) | ||
) | ||
.andDo(print()); | ||
} | ||
|
||
@DisplayName("올바르지 않은 이메일 또는 비밀번호인 경우 로그인할 수 없다.") | ||
@Test | ||
void login_WhenEmailAndPasswordIsIncorrect_ShouldFail() throws Exception { | ||
given(authService.login(LoginFixture.createLoginRequest())) | ||
.willThrow(new BusinessLogicException(LoginErrorCode.INCORRECT_EMAIL_OR_PASSWORD)); | ||
|
||
mockMvc.perform(post("/auth/login") | ||
.content(objectMapper.writeValueAsString(LoginFixture.createLoginRequest())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(errorCode(LoginErrorCode.INCORRECT_EMAIL_OR_PASSWORD)) | ||
.andDo(print()); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...etp/fixture/EmailVerificationFixture.java → ...uth/fixture/EmailVerificationFixture.java
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
12 changes: 12 additions & 0 deletions
12
src/test/java/es/princip/getp/domain/auth/fixture/LoginFixture.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package es.princip.getp.domain.auth.fixture; | ||
|
||
import es.princip.getp.domain.auth.dto.request.LoginRequest; | ||
|
||
public class LoginFixture { | ||
private static final String EMAIL = "[email protected]"; | ||
private static final String PASSWORD = "1q2w3e4r!"; | ||
|
||
public static LoginRequest createLoginRequest() { | ||
return new LoginRequest(EMAIL, PASSWORD); | ||
} | ||
} |
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
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
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
2 changes: 1 addition & 1 deletion
2
...rviceTerm/support/ServiceTermFixture.java → ...rviceTerm/fixture/ServiceTermFixture.java
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
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