-
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
1 parent
d64059a
commit aa808a5
Showing
7 changed files
with
283 additions
and
0 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
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
76 changes: 76 additions & 0 deletions
76
src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/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,76 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.presentation; | ||
|
||
import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.application.AuthService; | ||
import com.inq.wishhair.wesharewishhair.auth.application.dto.response.LoginResponse; | ||
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.LoginRequest; | ||
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; | ||
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig; | ||
|
||
@WebMvcTest(value = {AuthController.class, SecurityConfig.class}) | ||
@DisplayName("[AuthController 테스트] - API") | ||
class AuthControllerTest extends ApiTestSupport { | ||
|
||
private static final String LOGIN_URL = "/api/auth/login"; | ||
private static final String LOGOUT_URL = "/api/auth/logout"; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private AuthService authService; | ||
|
||
@Test | ||
@DisplayName("[로그인 API 를 호출한다]") | ||
void login() throws Exception { | ||
//given | ||
String email = "email"; | ||
String pw = "pw"; | ||
LoginRequest loginRequest = new LoginRequest(email, pw); | ||
|
||
LoginResponse loginResponse = new LoginResponse("token", "token"); | ||
given(authService.login(email, pw)) | ||
.willReturn(loginResponse); | ||
|
||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(LOGIN_URL) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(toJson(loginRequest)) | ||
|
||
); | ||
|
||
//then | ||
result.andExpectAll( | ||
jsonPath("$.accessToken").value(loginResponse.accessToken()), | ||
jsonPath("$.refreshToken").value(loginResponse.refreshToken()) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("[로그아웃 API 를 호출한다]") | ||
void logout() throws Exception { | ||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(LOGOUT_URL) | ||
.header(AUTHORIZATION, ACCESS_TOKEN) | ||
); | ||
|
||
//then | ||
result.andExpect(status().isOk()); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test/java/com/inq/wishhair/wesharewishhair/auth/presentation/MailAuthControllerTest.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,91 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.presentation; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.application.MailAuthService; | ||
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.AuthKeyRequest; | ||
import com.inq.wishhair.wesharewishhair.auth.presentation.dto.request.MailRequest; | ||
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; | ||
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig; | ||
import com.inq.wishhair.wesharewishhair.user.application.utils.UserValidator; | ||
|
||
@WebMvcTest(value = {MailAuthController.class, SecurityConfig.class}) | ||
@DisplayName("[MailAuthController 테스트] - API") | ||
class MailAuthControllerTest extends ApiTestSupport { | ||
|
||
private static final String CHECK_DUPLICATED_EMAIL_URL = "/api/email/check"; | ||
private static final String SEND_AUTHORIZATION_MAIL_URL = "/api/email/send"; | ||
private static final String AUTHORIZE_KEY_URL = "/api/email/validate"; | ||
private static final String EMAIL = "[email protected]"; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private UserValidator userValidator; | ||
@MockBean | ||
private MailAuthService mailAuthService; | ||
|
||
@Test | ||
@DisplayName("[이메일 중복검사 API 를 호출한다]") | ||
void checkDuplicateEmail() throws Exception { | ||
//given | ||
MailRequest mailRequest = new MailRequest(EMAIL); | ||
|
||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(CHECK_DUPLICATED_EMAIL_URL) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(toJson(mailRequest)) | ||
); | ||
|
||
//then | ||
result.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@DisplayName("[인증 메일 발송 API 를 호출한다]") | ||
void sendAuthorizationMail() throws Exception { | ||
//given | ||
MailRequest mailRequest = new MailRequest(EMAIL); | ||
|
||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(SEND_AUTHORIZATION_MAIL_URL) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(toJson(mailRequest)) | ||
); | ||
|
||
//then | ||
result.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
@DisplayName("[인증코드 확인 API 를 호출한다]") | ||
void authorizeKey() throws Exception { | ||
//given | ||
AuthKeyRequest authKeyRequest = new AuthKeyRequest(EMAIL, "authcode"); | ||
|
||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(AUTHORIZE_KEY_URL) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(toJson(authKeyRequest)) | ||
); | ||
|
||
//then | ||
result.andExpect(status().isOk()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...t/java/com/inq/wishhair/wesharewishhair/auth/presentation/TokenReissueControllerTest.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,54 @@ | ||
package com.inq.wishhair.wesharewishhair.auth.presentation; | ||
|
||
import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
import com.inq.wishhair.wesharewishhair.auth.application.TokenReissueService; | ||
import com.inq.wishhair.wesharewishhair.auth.application.dto.response.TokenResponse; | ||
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; | ||
import com.inq.wishhair.wesharewishhair.global.config.SecurityConfig; | ||
|
||
@WebMvcTest(value = {TokenReissueController.class, SecurityConfig.class}) | ||
@DisplayName("[TokenReissueController 테스트] - API") | ||
class TokenReissueControllerTest extends ApiTestSupport { | ||
|
||
private static final String REISSUE_TOKEN_URL = "/api/token/reissue"; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private TokenReissueService tokenReissueService; | ||
|
||
@Test | ||
@DisplayName("[토큰 재발급 API 를 호출한다]") | ||
void reissueToken() throws Exception { | ||
//given | ||
TokenResponse tokenResponse = new TokenResponse("accessToken", "refreshToken"); | ||
given(tokenReissueService.reissueToken(1L, TOKEN)) | ||
.willReturn(tokenResponse); | ||
|
||
//when | ||
ResultActions result = mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post(REISSUE_TOKEN_URL) | ||
.header(AUTHORIZATION, ACCESS_TOKEN) | ||
); | ||
|
||
//then | ||
result.andExpectAll( | ||
status().isOk(), | ||
jsonPath("$.accessToken").value(tokenResponse.accessToken()), | ||
jsonPath("$.refreshToken").value(tokenResponse.refreshToken()) | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/com/inq/wishhair/wesharewishhair/common/fixture/AuthFixture.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,13 @@ | ||
package com.inq.wishhair.wesharewishhair.common.fixture; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class AuthFixture { | ||
|
||
public static final String TOKEN = "token"; | ||
public static final String BEARER = "Bearer "; | ||
public static final String AUTHORIZATION = "Authorization"; | ||
public static final String ACCESS_TOKEN = BEARER + TOKEN; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.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,30 @@ | ||
package com.inq.wishhair.wesharewishhair.common.support; | ||
|
||
import static com.inq.wishhair.wesharewishhair.common.fixture.AuthFixture.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.inq.wishhair.wesharewishhair.auth.domain.AuthToken; | ||
import com.inq.wishhair.wesharewishhair.auth.domain.AuthTokenManager; | ||
|
||
public abstract class ApiTestSupport { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@MockBean | ||
protected AuthTokenManager authTokenManager; | ||
|
||
@BeforeEach | ||
public void setAuthorization() { | ||
given(authTokenManager.generate(any(Long.class))).willReturn(new AuthToken(TOKEN, TOKEN)); | ||
given(authTokenManager.getId(anyString())).willReturn(1L); | ||
} | ||
|
||
public String toJson(Object object) throws JsonProcessingException { | ||
return objectMapper.writeValueAsString(object); | ||
} | ||
} |