Skip to content

Commit 174ac9f

Browse files
committed
test: 전체 테스트 Token 설정
1 parent 4a8589b commit 174ac9f

8 files changed

+904
-757
lines changed

src/test/java/banking/account/api/AccountControllerIntegrationTest.java renamed to src/test/java/banking/account/api/AccountControllerTest.java

+10-22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import banking.account.enums.Currency;
99
import banking.common.exception.CustomException;
1010
import banking.common.exception.ErrorCode;
11+
import banking.common.jwt.TestJwtUtil;
1112
import banking.fixture.dto.UserCreationRequestDtoFixture;
1213
import banking.user.dto.request.UserCreationRequest;
1314
import banking.user.dto.response.UserResponse;
15+
import banking.user.entity.Role;
1416
import banking.user.repository.UserRepository;
1517
import banking.user.service.UserService;
1618
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -22,7 +24,6 @@
2224
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2325
import org.springframework.boot.test.context.SpringBootTest;
2426
import org.springframework.http.MediaType;
25-
import org.springframework.mock.web.MockHttpSession;
2627
import org.springframework.test.context.ActiveProfiles;
2728
import org.springframework.test.web.servlet.MockMvc;
2829

@@ -38,7 +39,7 @@
3839
@SpringBootTest
3940
@AutoConfigureMockMvc
4041
@ActiveProfiles("test")
41-
class AccountControllerIntegrationTest {
42+
class AccountControllerTest {
4243

4344
@Autowired
4445
MockMvc mockMvc;
@@ -71,9 +72,6 @@ void create_account_succeed_test () throws Exception {
7172
UserCreationRequest userCreationRequest = UserCreationRequestDtoFixture.USER_CREATION_REQUEST_DTO_FIXTURE_1.createUserCreationRequestDto();
7273
UserResponse userResponse = userService.register(userCreationRequest);
7374

74-
MockHttpSession session = new MockHttpSession();
75-
session.setAttribute("user", userResponse.userId());
76-
7775
AccountCreationRequest request = AccountCreationRequest.builder()
7876
.password("62324")
7977
.currency(Currency.KRW)
@@ -83,7 +81,7 @@ void create_account_succeed_test () throws Exception {
8381
// when & then
8482
mockMvc.perform(
8583
post("/accounts")
86-
.session(session)
84+
.header("Authorization", "Bearer " + TestJwtUtil.generateTestAccessToken(userResponse.userId(), Role.USER))
8785
.contentType(MediaType.APPLICATION_JSON)
8886
.content(objectMapper.writeValueAsString(request))
8987
)
@@ -94,8 +92,6 @@ void create_account_succeed_test () throws Exception {
9492
.andExpect(jsonPath("$.balance").value(comparesEqualTo(BigDecimal.ZERO.intValue())))
9593
.andExpect(jsonPath("$.accountName").value(request.accountName()))
9694
.andDo(print());
97-
98-
session.invalidate();
9995
}
10096

10197
@Test
@@ -105,9 +101,6 @@ void find_single_account_test () throws Exception {
105101
UserCreationRequest userCreationRequest = UserCreationRequestDtoFixture.USER_CREATION_REQUEST_DTO_FIXTURE_1.createUserCreationRequestDto();
106102
UserResponse userResponse = userService.register(userCreationRequest);
107103

108-
MockHttpSession session = new MockHttpSession();
109-
session.setAttribute("user", userResponse.userId());
110-
111104
AccountCreationRequest request = AccountCreationRequest.builder()
112105
.password("62324")
113106
.currency(Currency.KRW)
@@ -120,15 +113,13 @@ void find_single_account_test () throws Exception {
120113

121114
// when & then
122115
mockMvc.perform(get("/accounts/{id}", account.getId())
123-
.session(session)
116+
.header("Authorization", "Bearer " + TestJwtUtil.generateTestAccessToken(userResponse.userId(), Role.USER))
124117
.contentType(MediaType.APPLICATION_JSON))
125118
.andExpect(status().isOk())
126119
.andExpect(jsonPath("$.accountId").value(account.getId()))
127120
.andExpect(jsonPath("$.accountNumber").value(account.getAccountNumber()))
128121
.andExpect(jsonPath("$.currency").value(request.currency().toString()))
129122
.andDo(print());
130-
131-
session.invalidate();
132123
}
133124

134125
@Test
@@ -138,9 +129,6 @@ void find_all_account_test () throws Exception {
138129
UserCreationRequest userCreationRequest = UserCreationRequestDtoFixture.USER_CREATION_REQUEST_DTO_FIXTURE_1.createUserCreationRequestDto();
139130
UserResponse userResponse = userService.register(userCreationRequest);
140131

141-
MockHttpSession session = new MockHttpSession();
142-
session.setAttribute("user", userResponse.userId());
143-
144132
AccountCreationRequest requestKrwAccount = AccountCreationRequest.builder()
145133
.password("62324")
146134
.currency(Currency.KRW)
@@ -157,14 +145,14 @@ void find_all_account_test () throws Exception {
157145
accountService.createAccount(userResponse.userId(), requestUsdAccount);
158146

159147
// when & then
160-
mockMvc.perform(get("/accounts")
161-
.session(session)
162-
.contentType(MediaType.APPLICATION_JSON))
148+
mockMvc.perform(
149+
get("/accounts")
150+
.header("Authorization", "Bearer " + TestJwtUtil.generateTestAccessToken(userResponse.userId(), Role.USER))
151+
.contentType(MediaType.APPLICATION_JSON)
152+
)
163153
.andExpect(status().isOk())
164154
.andExpect(jsonPath("$.length()").value(2))
165155
.andExpect(jsonPath("$[*].currency").value(Matchers.containsInAnyOrder("KRW", "USD")))
166156
.andDo(print());
167-
168-
session.invalidate();
169157
}
170158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package banking.auth.api;
2+
3+
import banking.account.repository.AccountRepository;
4+
import banking.auth.dto.request.LoginRequest;
5+
import banking.user.dto.request.UserCreationRequest;
6+
import banking.user.repository.UserRepository;
7+
import banking.user.service.UserService;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.http.MediaType;
16+
import org.springframework.test.context.ActiveProfiles;
17+
import org.springframework.test.web.servlet.MockMvc;
18+
19+
import static org.hamcrest.Matchers.notNullValue;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
27+
28+
@SpringBootTest
29+
@AutoConfigureMockMvc
30+
@ActiveProfiles("test")
31+
class AuthControllerTest {
32+
33+
@Autowired
34+
UserService userService;
35+
36+
@Autowired
37+
UserRepository userRepository;
38+
39+
@Autowired
40+
AccountRepository accountRepository;
41+
42+
@Autowired
43+
MockMvc mockMvc;
44+
45+
@Autowired
46+
ObjectMapper objectMapper;
47+
48+
@AfterEach
49+
void afterEach() {
50+
accountRepository.deleteAll();
51+
userRepository.deleteAll();
52+
}
53+
54+
@Test
55+
@DisplayName("[로그인 성공 테스트] 로그인 성공 시 Access & Refresh Token 생성")
56+
public void login_succeed_test () throws Exception {
57+
// given
58+
UserCreationRequest userCreationRequest = UserCreationRequest.builder()
59+
.name("test-name")
60+
.loginId("test-login-id-890")
61+
.password("test-password")
62+
.build();
63+
64+
userService.register(userCreationRequest);
65+
66+
LoginRequest loginRequest = LoginRequest.builder()
67+
.loginId(userCreationRequest.loginId())
68+
.password(userCreationRequest.password())
69+
.build();
70+
71+
// when & then
72+
mockMvc.perform(
73+
post("/auth/login")
74+
.content(objectMapper.writeValueAsString(loginRequest))
75+
.contentType(MediaType.APPLICATION_JSON)
76+
)
77+
.andExpect(status().isOk())
78+
.andExpect(jsonPath("$").value("로그인 성공"))
79+
.andExpect(header().string("Authorization", notNullValue()))
80+
.andExpect(cookie().exists("refresh_token"))
81+
.andDo(print());
82+
}
83+
84+
@Test
85+
@DisplayName("[로그인 실패 테스트] 아이디 일치하지 않으면 LOGIN_FAIL 에러 코드 예외 발생")
86+
public void login_incorrect_login_id_fail_test () throws Exception {
87+
// given
88+
UserCreationRequest userCreationRequest = UserCreationRequest.builder()
89+
.name("test-name")
90+
.loginId("test-login-id-789")
91+
.password("test-password")
92+
.build();
93+
94+
userService.register(userCreationRequest);
95+
96+
LoginRequest loginRequest = LoginRequest.builder()
97+
.loginId(userCreationRequest.loginId() + "1234")
98+
.password(userCreationRequest.password())
99+
.build();
100+
101+
// when & then
102+
mockMvc.perform(
103+
post("/auth/login")
104+
.content(objectMapper.writeValueAsString(loginRequest))
105+
.contentType(MediaType.APPLICATION_JSON)
106+
)
107+
.andExpect(status().isNotFound())
108+
.andExpect(jsonPath("$").value("존재하지 않는 유저입니다."))
109+
.andDo(print());
110+
}
111+
112+
@Test
113+
@DisplayName("[로그인 실패 테스트] 비밀번호 일치하지 않으면 LOGIN_FAIL 에러 코드 예외 발생")
114+
public void login_incorrect_password_fail_test () throws Exception {
115+
// given
116+
UserCreationRequest userCreationRequest = UserCreationRequest.builder()
117+
.name("test-name")
118+
.loginId("test-login-id-257")
119+
.password("test-password")
120+
.build();
121+
122+
userService.register(userCreationRequest);
123+
124+
LoginRequest loginRequest = LoginRequest.builder()
125+
.loginId(userCreationRequest.loginId())
126+
.password(userCreationRequest.password() + "1234")
127+
.build();
128+
129+
// when & then
130+
mockMvc.perform(
131+
post("/auth/login")
132+
.content(objectMapper.writeValueAsString(loginRequest))
133+
.contentType(MediaType.APPLICATION_JSON)
134+
)
135+
.andExpect(status().isUnauthorized())
136+
.andExpect(jsonPath("$").value("비밀번호가 일치하지 않습니다."))
137+
.andDo(print());
138+
}
139+
140+
@Test
141+
@DisplayName("[로그아웃 성공 테스트] 로그아웃 시 refresh 토큰 만료")
142+
public void logout_succeed_test () throws Exception {
143+
// given
144+
UserCreationRequest userCreationRequest = UserCreationRequest.builder()
145+
.name("test-name")
146+
.loginId("test-login-id-2319")
147+
.password("test-password")
148+
.build();
149+
150+
userService.register(userCreationRequest);
151+
152+
LoginRequest loginRequest = LoginRequest.builder()
153+
.loginId(userCreationRequest.loginId())
154+
.password(userCreationRequest.password())
155+
.build();
156+
157+
// when & then
158+
String accessToken = mockMvc.perform(
159+
post("/auth/login")
160+
.content(objectMapper.writeValueAsString(loginRequest))
161+
.contentType(MediaType.APPLICATION_JSON)
162+
)
163+
.andExpect(status().isOk())
164+
.andExpect(jsonPath("$").value("로그인 성공"))
165+
.andExpect(header().string("Authorization", notNullValue()))
166+
.andExpect(cookie().exists("refresh_token"))
167+
.andDo(print())
168+
.andReturn()
169+
.getResponse()
170+
.getHeader("Authorization");
171+
172+
mockMvc.perform(
173+
get("/auth/logout")
174+
.contentType(MediaType.APPLICATION_JSON)
175+
.header("Authorization", accessToken)
176+
)
177+
.andExpect(status().isOk())
178+
.andExpect(jsonPath("$").value("로그아웃 성공"))
179+
.andExpect(header().doesNotExist("Authorization"))
180+
.andDo(print());
181+
}
182+
}

0 commit comments

Comments
 (0)