Skip to content

Commit

Permalink
refactor: API 테스트 통합테스트로 돌아가도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Nov 11, 2023
1 parent d38d2c8 commit b2439ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class TokenReissueController {

private final TokenReissueService tokenReissueService;

@PostMapping("/token/reissue")
public ResponseEntity<TokenResponse> reissueToken(
final @FetchAuthInfo AuthInfo authInfo
) {
@PostMapping("/tokens/reissue")
public ResponseEntity<TokenResponse> reissueToken(@FetchAuthInfo AuthInfo authInfo) {
TokenResponse response = tokenReissueService.reissueToken(authInfo.userId(), authInfo.token());
return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
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.user.domain.UserRepository;
import com.inq.wishhair.wesharewishhair.user.domain.entity.User;
import com.inq.wishhair.wesharewishhair.user.fixture.UserFixture;

@DisplayName("[AuthController 테스트] - API")
class AuthControllerTest extends ApiTestSupport {
Expand All @@ -28,6 +31,8 @@ class AuthControllerTest extends ApiTestSupport {
private MockMvc mockMvc;
@MockBean
private AuthService authService;
@Autowired
private UserRepository userRepository;

@Test
@DisplayName("[로그인 API 를 호출한다]")
Expand Down Expand Up @@ -60,11 +65,15 @@ void login() throws Exception {
@Test
@DisplayName("[로그아웃 API 를 호출한다]")
void logout() throws Exception {
//given
User user = UserFixture.getFixedManUser();
Long userId = userRepository.save(user).getId();

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(LOGOUT_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
.header(AUTHORIZATION, BEARER + getAccessToken(userId))
);

//then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
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.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.auth.domain.TokenRepository;
import com.inq.wishhair.wesharewishhair.auth.domain.entity.Token;
import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport;
import com.inq.wishhair.wesharewishhair.user.domain.UserRepository;
import com.inq.wishhair.wesharewishhair.user.domain.entity.User;
import com.inq.wishhair.wesharewishhair.user.fixture.UserFixture;

@DisplayName("[TokenReissueController 테스트] - API")
class TokenReissueControllerTest extends ApiTestSupport {

private static final String REISSUE_TOKEN_URL = "/api/token/reissue";
private static final String REISSUE_TOKEN_URL = "/api/tokens/reissue";

@Autowired
private MockMvc mockMvc;
@MockBean
private TokenReissueService tokenReissueService;
@Autowired
private UserRepository userRepository;
@Autowired
private TokenRepository tokenRepository;

@Test
@DisplayName("[토큰 재발급 API 를 호출한다]")
void reissueToken() throws Exception {
//given
TokenResponse tokenResponse = new TokenResponse("accessToken", "refreshToken");
given(tokenReissueService.reissueToken(1L, TOKEN))
.willReturn(tokenResponse);
User user = UserFixture.getFixedManUser();
Long userId = userRepository.save(user).getId();

String token = getAccessToken(userId);
tokenRepository.save(Token.issue(userId, token));

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(REISSUE_TOKEN_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
.header(AUTHORIZATION, BEARER + token)
);

//then
result.andExpectAll(
status().isOk(),
jsonPath("$.accessToken").value(tokenResponse.accessToken()),
jsonPath("$.refreshToken").value(tokenResponse.refreshToken())
jsonPath("$.accessToken").exists(),
jsonPath("$.refreshToken").exists()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;

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;

@SpringBootTest
Expand All @@ -21,19 +16,13 @@ public abstract class ApiTestSupport {

private final ObjectMapper objectMapper = new ObjectMapper();

@MockBean
protected AuthTokenManager authTokenManager;
@Autowired
private AuthTokenManager authTokenManager;

@BeforeEach
public void setAuthorization() {
given(authTokenManager.generate(any(Long.class))).willReturn(new AuthToken(TOKEN, TOKEN));
given(authTokenManager.getId(anyString())).willReturn(1L);
protected String getAccessToken(Long userId) {
return authTokenManager.generate(userId).accessToken();
}

protected void setAuthorization(Long userId) {
given(authTokenManager.generate(any(Long.class))).willReturn(new AuthToken(TOKEN, TOKEN));
given(authTokenManager.getId(anyString())).willReturn(userId);
}

public String toJson(Object object) throws JsonProcessingException {
return objectMapper.writeValueAsString(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class PointControllerTest extends ApiTestSupport {
void usePoint() throws Exception {
//given
User user = UserFixture.getFixedManUser();
userRepository.save(user);
Long userId = userRepository.save(user).getId();
pointLogRepository.save(PointLogFixture.getUsePointLog(user));

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.post(POINT_USE_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
.header(AUTHORIZATION, BEARER + getAccessToken(userId))
.contentType(MediaType.APPLICATION_JSON)
.content(toJson(PointLogFixture.getPointUseRequest()))
);
Expand All @@ -60,13 +60,11 @@ void findPointHistories() throws Exception {
Long userId = userRepository.save(user).getId();
pointLogRepository.save(PointLogFixture.getUsePointLog(user));

setAuthorization(userId);

//when
ResultActions result = mockMvc.perform(
MockMvcRequestBuilders
.get(POINT_QUERY_URL)
.header(AUTHORIZATION, ACCESS_TOKEN)
.header(AUTHORIZATION, BEARER + getAccessToken(userId))
);

//then
Expand Down

0 comments on commit b2439ff

Please sign in to comment.