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