Skip to content

Commit

Permalink
test: 사용자 인증 필터 테스트 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Srltas committed Nov 2, 2024
1 parent aacb26e commit 1b6a679
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static com.srltas.runtogether.adapter.in.web.common.AuthConstants.*;
import static com.srltas.runtogether.adapter.in.web.common.SessionAttribute.*;
import static jakarta.servlet.http.HttpServletResponse.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.io.IOException;
Expand All @@ -16,6 +18,7 @@

import com.srltas.runtogether.adapter.out.session.SessionStorage;
import com.srltas.runtogether.adapter.out.session.UserSessionDTO;
import com.srltas.runtogether.common.exception.UnauthorizedException;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -73,9 +76,13 @@ void testInvalidToken_UserAuthenticated() throws ServletException, IOException {
when(sessionStorage.getUserFromSessionId(INVALID_TOKEN)).thenReturn(null);

// when
authenticationFilter.doFilterInternal(request, response, filterChain);
UnauthorizedException exception = assertThrows(UnauthorizedException.class,
() -> {
authenticationFilter.doFilterInternal(request, response, filterChain);
});

verify(response).sendError(SC_UNAUTHORIZED, "인증되지 않은 사용자입니다.");
assertThat(exception.getErrorCode().getCode(), is(-4));
assertThat(exception.getMessage(), is("해당 API에 대한 요청 권한이 없습니다."));
verify(filterChain, never()).doFilter(request, response);
}

Expand All @@ -86,10 +93,13 @@ void testNoAuthorizationHeader() throws ServletException, IOException {
when(request.getHeader(AUTHORIZATION)).thenReturn(null);

// when
authenticationFilter.doFilterInternal(request, response, filterChain);
UnauthorizedException exception = assertThrows(UnauthorizedException.class, () -> {
authenticationFilter.doFilterInternal(request, response, filterChain);
});

// then
verify(response).sendError(SC_UNAUTHORIZED, "인증되지 않은 사용자입니다.");
assertThat(exception.getErrorCode().getCode(), is(-4));
assertThat(exception.getMessage(), is("해당 API에 대한 요청 권한이 없습니다."));
verify(filterChain, never()).doFilter(request, response);
}
}

0 comments on commit 1b6a679

Please sign in to comment.