Skip to content

Commit

Permalink
hotfix: checkstyle test 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Shin-Jae-Yoon committed Nov 23, 2023
1 parent ae497cf commit 9668f00
Showing 1 changed file with 71 additions and 44 deletions.
115 changes: 71 additions & 44 deletions src/test/java/com/moabam/global/filter/AuthorizationFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
package com.moabam.global.filter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.moabam.api.application.auth.AuthorizationService;
import com.moabam.api.application.auth.JwtAuthenticationService;
import com.moabam.api.application.auth.JwtProviderService;
import com.moabam.global.auth.filter.AuthorizationFilter;
import com.moabam.global.auth.model.AuthMember;
import com.moabam.global.auth.model.AuthorizationThreadLocal;
import com.moabam.global.auth.model.PublicClaim;
import com.moabam.support.fixture.JwtProviderFixture;
import com.moabam.support.fixture.PublicClaimFixture;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;

@ExtendWith(MockitoExtension.class)
class AuthorizationFilterTest {
//
// @InjectMocks
// AuthorizationFilter authorizationFilter;
//

@InjectMocks
AuthorizationFilter authorizationFilter;

// @Mock
// HandlerExceptionResolver handlerExceptionResolver;
//
// @Mock
// JwtAuthenticationService jwtAuthenticationService;
//
// @Mock
// AuthorizationService authorizationService;
//

@Mock
JwtAuthenticationService jwtAuthenticationService;

@Mock
AuthorizationService authorizationService;

// @DisplayName("토큰 타입이 Bearer가 아니면 예외 발생")
// @ParameterizedTest
// @ValueSource(strings = {
Expand Down Expand Up @@ -109,37 +136,37 @@ class AuthorizationFilterTest {
// eq(httpServletRequest), eq(httpServletResponse),
// eq(null), any(UnauthorizedException.class));
// }
//
// @DisplayName("새로운 도큰 발급 성공")
// @Test
// void issue_new_token_success() throws ServletException, IOException {
// // given
// JwtProviderService jwtProviderService = JwtProviderFixture.jwtProviderService();
// PublicClaim publicClaim = PublicClaimFixture.publicClaim();
//
// MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
// MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
// MockFilterChain mockFilterChain = new MockFilterChain();
//
// // when
// String accessToken = jwtProviderService.provideAccessToken(publicClaim);
// String refreshToken = jwtProviderService.provideRefreshToken();
// httpServletRequest.setCookies(
// new Cookie("token_type", "Bearer"),
// new Cookie("access_token", accessToken),
// new Cookie("refresh_token", refreshToken));
//
// when(jwtAuthenticationService.parseClaim(accessToken)).thenReturn(publicClaim);
// when(jwtAuthenticationService.isTokenExpire(accessToken)).thenReturn(true);
// when(jwtAuthenticationService.isTokenExpire(refreshToken)).thenReturn(false);
//
// authorizationFilter.doFilter(httpServletRequest, httpServletResponse, mockFilterChain);
//
// // then
// verify(authorizationService, times(1))
// .issueServiceToken(httpServletResponse, publicClaim);
//
// AuthMember authMember = AuthorizationThreadLocal.getAuthMember();
// assertThat(authMember.id()).isEqualTo(1L);
// }

@DisplayName("새로운 토큰 발급 성공")
@Test
void issue_new_token_success() throws ServletException, IOException {
// given
JwtProviderService jwtProviderService = JwtProviderFixture.jwtProviderService();
PublicClaim publicClaim = PublicClaimFixture.publicClaim();

MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
MockFilterChain mockFilterChain = new MockFilterChain();

// when
String accessToken = jwtProviderService.provideAccessToken(publicClaim);
String refreshToken = jwtProviderService.provideRefreshToken();
httpServletRequest.setCookies(
new Cookie("token_type", "Bearer"),
new Cookie("access_token", accessToken),
new Cookie("refresh_token", refreshToken));

when(jwtAuthenticationService.parseClaim(accessToken)).thenReturn(publicClaim);
when(jwtAuthenticationService.isTokenExpire(accessToken)).thenReturn(true);
when(jwtAuthenticationService.isTokenExpire(refreshToken)).thenReturn(false);

authorizationFilter.doFilter(httpServletRequest, httpServletResponse, mockFilterChain);

// then
verify(authorizationService, times(1))
.issueServiceToken(httpServletResponse, publicClaim);

AuthMember authMember = AuthorizationThreadLocal.getAuthMember();
assertThat(authMember.id()).isEqualTo(1L);
}
}

0 comments on commit 9668f00

Please sign in to comment.