Skip to content

Commit 73891a4

Browse files
committed
feat: OAuth2 핸들러 테스트작성
1 parent e7f54c2 commit 73891a4

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.back.global.security;
2+
3+
import com.back.domain.user.service.UserAuthService;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.mockito.InjectMocks;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.jupiter.MockitoExtension;
13+
import org.springframework.security.core.Authentication;
14+
import org.springframework.test.util.ReflectionTestUtils;
15+
16+
import static org.mockito.Mockito.*;
17+
18+
@ExtendWith(MockitoExtension.class)
19+
class CustomOAuth2LoginSuccessHandlerTest {
20+
21+
@Mock
22+
private UserAuthService userAuthService;
23+
24+
@Mock
25+
private HttpServletRequest request;
26+
27+
@Mock
28+
private HttpServletResponse response;
29+
30+
@Mock
31+
private Authentication authentication;
32+
33+
@Mock
34+
private SecurityUser securityUser;
35+
36+
@InjectMocks
37+
private CustomOAuth2LoginSuccessHandler handler;
38+
39+
private final String frontendUrl = "http://localhost:3000";
40+
41+
@BeforeEach
42+
void setUp() {
43+
ReflectionTestUtils.setField(handler, "frontendUrl", frontendUrl);
44+
}
45+
46+
47+
@Test
48+
@DisplayName("첫 사용자는 first-user 페이지로 리다이렉트")
49+
void redirectToFirstUser() throws Exception {
50+
// given
51+
Long userId = 1L;
52+
String email = "[email protected]";
53+
String nickname = "testUser";
54+
55+
when(authentication.getPrincipal()).thenReturn(securityUser);
56+
when(securityUser.getId()).thenReturn(userId);
57+
when(securityUser.getEmail()).thenReturn(email);
58+
when(securityUser.getNickname()).thenReturn(nickname);
59+
when(securityUser.isFirstLogin()).thenReturn(true);
60+
61+
// when
62+
handler.onAuthenticationSuccess(request, response, authentication);
63+
64+
// then
65+
verify(userAuthService).issueTokens(response, userId, email, nickname);
66+
verify(userAuthService).setFirstLoginFalse(userId);
67+
verify(response).sendRedirect(frontendUrl + "/login/user/first-user");
68+
}
69+
70+
@Test
71+
@DisplayName("기존 사용자는 success 페이지로 리다이렉트")
72+
void redirectToSuccess() throws Exception {
73+
// given
74+
Long userId = 1L;
75+
String email = "[email protected]";
76+
String nickname = "testUser";
77+
78+
when(authentication.getPrincipal()).thenReturn(securityUser);
79+
when(securityUser.getId()).thenReturn(userId);
80+
when(securityUser.getEmail()).thenReturn(email);
81+
when(securityUser.getNickname()).thenReturn(nickname);
82+
when(securityUser.isFirstLogin()).thenReturn(false);
83+
84+
// when
85+
handler.onAuthenticationSuccess(request, response, authentication);
86+
87+
// then
88+
verify(userAuthService).issueTokens(response, userId, email, nickname);
89+
verify(userAuthService, never()).setFirstLoginFalse(userId);
90+
verify(response).sendRedirect(frontendUrl + "/login/user/success");
91+
}
92+
93+
@Test
94+
@DisplayName("토큰 발급이 정상적으로 호출되는지 확인")
95+
void tokenIssuance() throws Exception {
96+
// given
97+
Long userId = 2L;
98+
String email = "[email protected]";
99+
String nickname = "nickname";
100+
101+
when(authentication.getPrincipal()).thenReturn(securityUser);
102+
when(securityUser.getId()).thenReturn(userId);
103+
when(securityUser.getEmail()).thenReturn(email);
104+
when(securityUser.getNickname()).thenReturn(nickname);
105+
when(securityUser.isFirstLogin()).thenReturn(false);
106+
107+
// when
108+
handler.onAuthenticationSuccess(request, response, authentication);
109+
110+
// then
111+
verify(userAuthService).issueTokens(response, userId, email, nickname);
112+
}
113+
}

0 commit comments

Comments
 (0)