2424import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
2525
2626import java .util .List ;
27+ import java .util .Map ;
2728
2829import static org .assertj .core .api .Assertions .assertThat ;
2930import static org .assertj .core .api .AssertionsForClassTypes .assertThatThrownBy ;
@@ -48,6 +49,9 @@ class MemberControllerTest {
4849 @ Mock
4950 private HttpServletResponse response ;
5051
52+ @ Mock
53+ private com .ai .lawyer .global .oauth .OAuth2LogoutService oauth2LogoutService ;
54+
5155 @ InjectMocks
5256 private MemberController memberController ;
5357
@@ -217,120 +221,124 @@ void login_Fail_PasswordMismatch() throws Exception {
217221 @ DisplayName ("로그아웃 성공 - Authentication에서 loginId 추출하여 Redis 삭제" )
218222 void logout_Success () {
219223 // given
224+ given (
oauth2LogoutService .
logoutFromOAuth2Provider (
eq (
"[email protected] " ))).
willReturn (
false );
225+ given (
oauth2LogoutService .
getOAuth2LogoutUrl (
eq (
"[email protected] " ))).
willReturn (
null );
220226 doNothing ().
when (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
221227
222228 // when
223- ResponseEntity <Void > result = memberController .logout (authentication , response );
229+ ResponseEntity <LogoutResponse > result = memberController .logout (authentication , response );
224230
225231 // then
226232 assertThat (result .getStatusCode ()).isEqualTo (HttpStatus .OK );
233+ verify (
oauth2LogoutService ).
logoutFromOAuth2Provider (
eq (
"[email protected] " ));
234+ verify (
oauth2LogoutService ).
getOAuth2LogoutUrl (
eq (
"[email protected] " ));
227235 verify (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
228236 }
229237
230238 @ Test
231239 @ DisplayName ("로그아웃 성공 - 인증되지 않은 상태에서도 쿠키 클리어" )
232240 void logout_Success_Unauthenticated () {
233241 // given
242+ given (oauth2LogoutService .getOAuth2LogoutUrl (null )).willReturn (null );
234243 doNothing ().when (memberService ).logout (eq ("" ), eq (response ));
235244
236245 // when
237- ResponseEntity <Void > result = memberController .logout (null , response );
246+ ResponseEntity <LogoutResponse > result = memberController .logout (null , response );
238247
239248 // then
240249 assertThat (result .getStatusCode ()).isEqualTo (HttpStatus .OK );
250+ verify (oauth2LogoutService ).getOAuth2LogoutUrl (null );
241251 verify (memberService ).logout (eq ("" ), eq (response ));
242252 }
243253
244254 @ Test
245- @ DisplayName ("토큰 재발급 성공 - Authentication 기반 " )
246- void refreshToken_Success () {
255+ @ DisplayName ("토큰 재발급 성공 - 쿠키에서 리프레시 토큰 추출 " )
256+ void refreshToken_Success () throws Exception {
247257 // given
248- Long memberId = 1L ;
249- Authentication testAuth = new UsernamePasswordAuthenticationToken (
250- memberId ,
251- null ,
252- List .of (new SimpleGrantedAuthority ("ROLE_USER" ))
253- );
254- given (memberService .getMemberById (memberId )).willReturn (memberResponse );
258+ String refreshTokenValue = "validRefreshToken" ;
259+ jakarta .servlet .http .Cookie refreshCookie = new jakarta .servlet .http .Cookie ("refreshToken" , refreshTokenValue );
260+
261+ given (memberService .refreshToken (eq (refreshTokenValue ), any (HttpServletResponse .class ))).willReturn (memberResponse );
255262
256263 // when
257- ResponseEntity <MemberResponse > result = memberController .refreshToken (testAuth );
264+ mockMvc .perform (post ("/api/auth/refresh" )
265+ .cookie (refreshCookie )
266+ .with (csrf ()))
267+ .andDo (print ())
268+ .andExpect (status ().isOk ())
269+ .andExpect (jsonPath ("$.memberId" ).value (1L ))
270+ .
andExpect (
jsonPath (
"$.loginId" ).
value (
"[email protected] " ));
258271
259272 // then
260- assertThat (result .getStatusCode ()).isEqualTo (HttpStatus .OK );
261- assertThat (result .getBody ()).isEqualTo (memberResponse );
262- verify (memberService ).getMemberById (memberId );
273+ verify (memberService ).refreshToken (eq (refreshTokenValue ), any (HttpServletResponse .class ));
263274 }
264275
265276 @ Test
266- @ DisplayName ("토큰 재발급 실패 - 인증 정보 없음" )
267- void refreshToken_Fail_NoAuthentication () {
268- // given - authentication이 null인 경우
277+ @ DisplayName ("토큰 재발급 실패 - 리프레시 토큰 없음" )
278+ void refreshToken_Fail_NoRefreshToken () throws Exception {
279+ // given - 쿠키 없이 요청
269280
270281 // when & then
271- assertThatThrownBy (() -> memberController .refreshToken (null ))
272- .isInstanceOf (MemberAuthenticationException .class )
273- .hasMessage ("인증이 필요합니다." );
274- }
275-
276- @ Test
277- @ DisplayName ("토큰 재발급 실패 - Principal 없음" )
278- void refreshToken_Fail_NoPrincipal () {
279- // given
280- Authentication testAuth = new UsernamePasswordAuthenticationToken (
281- null ,
282- null ,
283- List .of (new SimpleGrantedAuthority ("ROLE_USER" ))
284- );
282+ mockMvc .perform (post ("/api/auth/refresh" )
283+ .with (csrf ()))
284+ .andDo (print ())
285+ .andExpect (status ().isUnauthorized ());
285286
286- // when & then
287- assertThatThrownBy (() -> memberController .refreshToken (testAuth ))
288- .isInstanceOf (MemberAuthenticationException .class )
289- .hasMessage ("인증이 필요합니다." );
287+ verify (memberService , never ()).refreshToken (anyString (), any ());
290288 }
291289
292290 @ Test
293291 @ DisplayName ("회원탈퇴 성공" )
294292 void withdraw_Success () {
295- // given - 현재 Controller는 직접 memberId를 사용
296- doNothing (). when ( memberService ). withdraw ( 1L );
293+ // given
294+ given ( oauth2LogoutService . unlinkFromOAuth2Provider ( eq ( "[email protected] " ))). willReturn ( false );
297295 doNothing ().
when (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
296+ doNothing ().
when (
memberService ).
deleteMember (
eq (
"[email protected] " ));
298297
299298 // when
300- ResponseEntity <Void > result = memberController .withdraw (authentication , response );
299+ ResponseEntity <Map < String , Object > > result = memberController .withdraw (authentication , response );
301300
302301 // then
303302 assertThat (result .getStatusCode ()).isEqualTo (HttpStatus .OK );
304- verify (memberService ). withdraw ( 1L );
303+ verify (
oauth2LogoutService ). unlinkFromOAuth2Provider ( eq ( "[email protected] " ) );
305304 verify (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
305+ verify (
memberService ).
deleteMember (
eq (
"[email protected] " ));
306306 }
307307
308308 @ Test
309309 @ DisplayName ("회원탈퇴 실패 - 인증되지 않은 사용자" )
310310 void withdraw_Fail_Unauthenticated () {
311- // when & then
312- assertThatThrownBy (() -> memberController .withdraw (null , response ))
313- .isInstanceOf (MemberAuthenticationException .class )
314- .hasMessage ("인증이 필요합니다." );
311+ // when
312+ ResponseEntity <Map <String , Object >> result = memberController .withdraw (null , response );
313+
314+ // then
315+ assertThat (result .getStatusCode ()).isEqualTo (HttpStatus .UNAUTHORIZED );
316+ assertThat (result .getBody ()).isNotNull ();
317+ assertThat (result .getBody ().get ("success" )).isEqualTo (false );
318+ assertThat (result .getBody ().get ("message" )).isEqualTo ("인증이 필요합니다." );
315319
316- verify (memberService , never ()).withdraw (anyLong ());
320+ verify (oauth2LogoutService , never ()).unlinkFromOAuth2Provider (anyString ());
321+ verify (memberService , never ()).deleteMember (anyString ());
317322 verify (memberService , never ()).logout (anyString (), any ());
318323 }
319324
320325 @ Test
321326 @ DisplayName ("회원탈퇴 실패 - 존재하지 않는 회원" )
322327 void withdraw_Fail_MemberNotFound () {
323328 // given
329+ given (
oauth2LogoutService .
unlinkFromOAuth2Provider (
eq (
"[email protected] " ))).
willReturn (
false );
330+ doNothing ().
when (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
324331 doThrow (new IllegalArgumentException ("존재하지 않는 회원입니다." ))
325- .when (memberService ).withdraw ( 1L );
332+ .
when (
memberService ).
deleteMember ( eq ( "[email protected] " ) );
326333
327334 // when & then
328335 assertThatThrownBy (() -> memberController .withdraw (authentication , response ))
329336 .isInstanceOf (IllegalArgumentException .class )
330337 .hasMessage ("존재하지 않는 회원입니다." );
331338
332- verify (memberService ).withdraw (1L );
333- verify (memberService , never ()).logout (anyString (), any ());
339+ verify (
oauth2LogoutService ).
unlinkFromOAuth2Provider (
eq (
"[email protected] " ));
340+ verify (
memberService ).
logout (
eq (
"[email protected] " ),
eq (
response ));
341+ verify (
memberService ).
deleteMember (
eq (
"[email protected] " ));
334342 }
335343
336344 @ Test
@@ -699,4 +707,5 @@ void verifyEmail_Success_LoggedInUser() throws Exception {
699707
700708 verify (
memberService ).
verifyAuthCode (
"[email protected] " ,
"123456" );
701709 }
710+
702711}
0 commit comments