|
| 1 | +package com.back.global.security; |
| 2 | + |
| 3 | +import com.back.global.jwt.JwtUtil; |
| 4 | +import com.back.global.rq.Rq; |
| 5 | +import jakarta.servlet.FilterChain; |
| 6 | +import jakarta.servlet.http.HttpServletRequest; |
| 7 | +import jakarta.servlet.http.HttpServletResponse; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | +import org.springframework.security.core.Authentication; |
| 16 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 17 | +import org.springframework.test.util.ReflectionTestUtils; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +class CustomAuthenticationFilterTest { |
| 24 | + |
| 25 | + @Mock |
| 26 | + private JwtUtil jwtUtil; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private Rq rq; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private HttpServletRequest request; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private HttpServletResponse response; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private FilterChain filterChain; |
| 39 | + |
| 40 | + @InjectMocks |
| 41 | + private CustomAuthenticationFilter filter; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + void setUp() { |
| 45 | + ReflectionTestUtils.setField(filter, "accessTokenExpiration", 900); |
| 46 | + SecurityContextHolder.clearContext(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("토큰 없으면 통과") |
| 51 | + void noToken_Pass() throws Exception { |
| 52 | + when(rq.getCookieValue("accessToken", "")).thenReturn(""); |
| 53 | + |
| 54 | + filter.doFilterInternal(request, response, filterChain); |
| 55 | + |
| 56 | + verify(filterChain).doFilter(request, response); |
| 57 | + assertNull(SecurityContextHolder.getContext().getAuthentication()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @DisplayName("유효한 토큰으로 인증 성공") |
| 62 | + void validToken_Success() throws Exception { |
| 63 | + String token = "valid.token"; |
| 64 | + when(rq.getCookieValue("accessToken", "")).thenReturn(token); |
| 65 | + when(jwtUtil.validateAccessToken(token)).thenReturn(true); |
| 66 | + when(jwtUtil.getUserIdFromToken(token)).thenReturn(1L); |
| 67 | + when( jwtUtil. getEmailFromToken( token)). thenReturn( "[email protected]"); |
| 68 | + when(jwtUtil.getNicknameFromToken(token)).thenReturn("user"); |
| 69 | + |
| 70 | + filter.doFilterInternal(request, response, filterChain); |
| 71 | + |
| 72 | + verify(filterChain).doFilter(request, response); |
| 73 | + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); |
| 74 | + assertNotNull(auth); |
| 75 | + assertInstanceOf(SecurityUser.class, auth.getPrincipal()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("무효한 토큰은 통과") |
| 80 | + void invalidToken_Pass() throws Exception { |
| 81 | + String token = "invalid.token"; |
| 82 | + when(rq.getCookieValue("accessToken", "")).thenReturn(token); |
| 83 | + when(jwtUtil.validateAccessToken(token)).thenReturn(false); |
| 84 | + |
| 85 | + filter.doFilterInternal(request, response, filterChain); |
| 86 | + |
| 87 | + verify(filterChain).doFilter(request, response); |
| 88 | + assertNull(SecurityContextHolder.getContext().getAuthentication()); |
| 89 | + } |
| 90 | +} |
0 commit comments