diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthConfig.java b/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthConfig.java index 618364021..f1a669da8 100644 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthConfig.java +++ b/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthConfig.java @@ -3,15 +3,20 @@ import com.woowacourse.moamoa.auth.controller.AuthenticatedMemberResolver; import com.woowacourse.moamoa.auth.controller.AuthenticatedRefreshArgumentResolver; import com.woowacourse.moamoa.auth.controller.AuthenticationArgumentResolver; -import com.woowacourse.moamoa.auth.controller.AuthenticationInterceptor; +import com.woowacourse.moamoa.auth.controller.interceptor.AuthenticationInterceptor; +import com.woowacourse.moamoa.auth.controller.interceptor.PathMatcherContainer; +import com.woowacourse.moamoa.auth.controller.interceptor.PathMatcherInterceptor; +import com.woowacourse.moamoa.auth.infrastructure.TokenProvider; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; import org.springframework.web.client.RestTemplate; import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -20,10 +25,12 @@ public class AuthConfig implements WebMvcConfigurer { private final AuthenticatedRefreshArgumentResolver authenticatedRefreshArgumentResolver; - private final AuthenticationInterceptor authenticationInterceptor; private final AuthenticationArgumentResolver authenticationArgumentResolver; private final AuthenticatedMemberResolver authenticatedMemberResolver; + private final PathMatcherContainer pathMatcherContainer; + private final TokenProvider jwtTokenProvider; + @Override public void addArgumentResolvers(final List resolvers) { resolvers.add(authenticationArgumentResolver); @@ -33,10 +40,24 @@ public void addArgumentResolvers(final List resol @Override public void addInterceptors(final InterceptorRegistry registry) { - registry.addInterceptor(authenticationInterceptor) + registry.addInterceptor(loginInterceptor()) .addPathPatterns("/**"); } + private HandlerInterceptor loginInterceptor() { + return new PathMatcherInterceptor(new AuthenticationInterceptor(jwtTokenProvider), pathMatcherContainer) + .excludePathPattern("/**", HttpMethod.OPTIONS) + .includePathPattern("/api/studies/**", HttpMethod.POST) + .includePathPattern("/api/studies/**", HttpMethod.PUT) + .includePathPattern("/api/study/\\d+", HttpMethod.PUT) + .includePathPattern("/api/studies/**", HttpMethod.DELETE) + .includePathPattern("/api/members/me/**", HttpMethod.GET) + .includePathPattern("/api/auth/refresh", HttpMethod.GET) + .includePathPattern("/api/my/studies", HttpMethod.GET) + .includePathPattern("/api/studies/\\w+/community/articles/**", HttpMethod.GET) + .includePathPattern("/api/studies/\\d+/reference-room/links", HttpMethod.GET); + } + @Bean public RestTemplate restTemplate() { return new RestTemplate(); diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthRequestMatchConfig.java b/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthRequestMatchConfig.java deleted file mode 100644 index 7cd7c9f54..000000000 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/config/AuthRequestMatchConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.woowacourse.moamoa.auth.config; - -import static org.springframework.http.HttpMethod.*; - -import com.woowacourse.moamoa.auth.controller.matcher.AuthenticationRequestMatcher; -import com.woowacourse.moamoa.auth.controller.matcher.AuthenticationRequestMatcherBuilder; -import lombok.AllArgsConstructor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpMethod; - -@Configuration -@AllArgsConstructor -public class AuthRequestMatchConfig { - - @Bean - public AuthenticationRequestMatcher authenticationRequestMatcher() { - return new AuthenticationRequestMatcherBuilder() - .addUpAuthenticationPath(POST, - "/api/studies/\\d+/reviews", - "/api/studies/\\d+/reviews/\\d+", - "/api/studies/\\w+/\\w+/articles", - "/api/studies") - .addUpAuthenticationPath(GET, - "/api/my/studies", - "/api/auth/refresh", - "/api/members/me", - "/api/members/me/role", - "/api/studies/\\w+/community/articles/\\w+", - "/api/studies/\\w+/community/articles", - "/api/studies/\\d+/reference-room/links" - ) - .addUpAuthenticationPath(HttpMethod.PUT, - "/api/studies/\\d+/reviews/\\d+", - "/api/studies/\\d+/reference-room/links/\\d+", - "/api/study/\\d+" - ) - .addUpAuthenticationPath(HttpMethod.DELETE, - "/api/studies/\\d+/reviews/\\d+", - "/api/studies/\\w+/\\w+/articles/\\w+") - .build(); - } -} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptor.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptor.java similarity index 56% rename from backend/src/main/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptor.java rename to backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptor.java index 1f4a0847c..087db716b 100644 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptor.java +++ b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptor.java @@ -1,7 +1,6 @@ -package com.woowacourse.moamoa.auth.controller; +package com.woowacourse.moamoa.auth.controller.interceptor; import com.woowacourse.moamoa.auth.config.AuthenticationExtractor; -import com.woowacourse.moamoa.auth.controller.matcher.AuthenticationRequestMatcher; import com.woowacourse.moamoa.auth.infrastructure.TokenProvider; import com.woowacourse.moamoa.common.exception.UnauthorizedException; @@ -9,7 +8,6 @@ import javax.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; -import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; @@ -18,30 +16,17 @@ public class AuthenticationInterceptor implements HandlerInterceptor { private final TokenProvider tokenProvider; - private final AuthenticationRequestMatcher authenticationRequestMatcher; @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { - - if (isPreflight(request)) { - return true; - } - - if (authenticationRequestMatcher.isRequiredAuth(request)) { - final String token = AuthenticationExtractor.extract(request); - validateToken(token, request.getRequestURI()); - - request.setAttribute("payload", token); - } + final String token = AuthenticationExtractor.extract(request); + validateToken(token, request.getRequestURI()); + request.setAttribute("payload", token); return true; } - private boolean isPreflight(HttpServletRequest request) { - return HttpMethod.OPTIONS.matches(request.getMethod()); - } - - private void validateToken(String token, String requestURI) { + private void validateToken(final String token, final String requestURI) { if (requestURI.equals("/api/auth/refresh") && token != null) { return; } diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainer.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainer.java new file mode 100644 index 000000000..583a308c8 --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainer.java @@ -0,0 +1,40 @@ +package com.woowacourse.moamoa.auth.controller.interceptor; + +import java.util.ArrayList; +import java.util.List; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; +import org.springframework.util.AntPathMatcher; +import org.springframework.util.PathMatcher; + +@Component +public class PathMatcherContainer { + + private final PathMatcher pathMatcher; + private final List includePathPatterns; + private final List excludePathPatterns; + + public PathMatcherContainer() { + this.pathMatcher = new AntPathMatcher(); + this.includePathPatterns = new ArrayList<>(); + this.excludePathPatterns = new ArrayList<>(); + } + + public boolean isNotIncludePath(final String targetPath, final String pathMethod) { + final boolean excludePattern = excludePathPatterns.stream() + .anyMatch(requestPath -> requestPath.match(pathMatcher, targetPath, pathMethod)); + + final boolean isNotIncludePattern = includePathPatterns.stream() + .noneMatch(requestPath -> requestPath.match(pathMatcher, targetPath, pathMethod)); + + return excludePattern || isNotIncludePattern; + } + + public void includePathPattern(final String path, final HttpMethod method) { + this.includePathPatterns.add(new PathRequestMatcher(path, method)); + } + + public void excludePathPattern(final String path, final HttpMethod method) { + this.excludePathPatterns.add(new PathRequestMatcher(path, method)); + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherInterceptor.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherInterceptor.java new file mode 100644 index 000000000..9e3c38ed3 --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherInterceptor.java @@ -0,0 +1,38 @@ +package com.woowacourse.moamoa.auth.controller.interceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import lombok.AllArgsConstructor; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Component +@AllArgsConstructor +public class PathMatcherInterceptor implements HandlerInterceptor { + + private final HandlerInterceptor handlerInterceptor; + private final PathMatcherContainer pathMatcherContainer; + + @Override + public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) + throws Exception { + + if (pathMatcherContainer.isNotIncludePath(request.getRequestURI(), request.getMethod())) { + return true; + } + + return this.handlerInterceptor.preHandle(request, response, handler); + } + + public PathMatcherInterceptor includePathPattern(final String pathPattern, final HttpMethod method) { + this.pathMatcherContainer.includePathPattern(pathPattern, method); + return this; + } + + public PathMatcherInterceptor excludePathPattern(final String pathPattern, final HttpMethod method) { + this.pathMatcherContainer.excludePathPattern(pathPattern, method); + return this; + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcher.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcher.java new file mode 100644 index 000000000..863dc781b --- /dev/null +++ b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcher.java @@ -0,0 +1,18 @@ +package com.woowacourse.moamoa.auth.controller.interceptor; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpMethod; +import org.springframework.util.PathMatcher; + +@AllArgsConstructor +@Getter +public class PathRequestMatcher { + + private final String path; + private final HttpMethod method; + + public boolean match(final PathMatcher pathMatcher, final String targetPath, final String pathMethod) { + return pathMatcher.match(path, targetPath) && this.method.matches(pathMethod); + } +} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequest.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequest.java deleted file mode 100644 index 232f99176..000000000 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.woowacourse.moamoa.auth.controller.matcher; - -import java.util.regex.Pattern; -import javax.servlet.http.HttpServletRequest; -import lombok.AllArgsConstructor; -import org.springframework.http.HttpMethod; - -@AllArgsConstructor -public class AuthenticationRequest { - - private final HttpMethod method; - private final String path; - - public boolean isMatch(HttpServletRequest request) { - final Pattern pattern = Pattern.compile(path); - - return method.equals(HttpMethod.resolve(request.getMethod().toUpperCase())) - && pattern.matcher(request.getRequestURI()).matches(); - } -} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcher.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcher.java deleted file mode 100644 index 4deeb2586..000000000 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcher.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.woowacourse.moamoa.auth.controller.matcher; - -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import javax.servlet.http.HttpServletRequest; -import org.springframework.http.HttpMethod; - -public class AuthenticationRequestMatcher { - - final List authenticationRequests; - - public AuthenticationRequestMatcher(final Map> authenticationPaths) { - this.authenticationRequests = authenticationPaths.keySet() - .stream() - .flatMap(method -> authenticationPaths.get(method) - .stream() - .map(path -> new AuthenticationRequest(method, path)) - ) - .collect(Collectors.toList()); - } - - public boolean isRequiredAuth(HttpServletRequest request) { - return authenticationRequests.stream() - .anyMatch(authenticationRequest -> authenticationRequest.isMatch(request)); - } -} diff --git a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherBuilder.java b/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherBuilder.java deleted file mode 100644 index 8b55f616d..000000000 --- a/backend/src/main/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherBuilder.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.woowacourse.moamoa.auth.controller.matcher; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import org.springframework.http.HttpMethod; - -public class AuthenticationRequestMatcherBuilder { - - private static final List METHODS = List.of( - HttpMethod.GET, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.PATCH - ); - - private final Map> authenticationPaths; - - public AuthenticationRequestMatcherBuilder() { - this.authenticationPaths = METHODS.stream() - .collect(Collectors.toMap(method -> method, method -> new ArrayList<>())); - } - - public AuthenticationRequestMatcherBuilder addUpAuthenticationPath(HttpMethod method, String... urls) { - final List paths = authenticationPaths.get(method); - paths.addAll(List.of(urls)); - return this; - } - - public AuthenticationRequestMatcher build() { - return new AuthenticationRequestMatcher(authenticationPaths); - } -} diff --git a/backend/src/test/java/com/woowacourse/moamoa/WebMVCTest.java b/backend/src/test/java/com/woowacourse/moamoa/WebMVCTest.java index 0aab0a5f3..4290a3a1e 100644 --- a/backend/src/test/java/com/woowacourse/moamoa/WebMVCTest.java +++ b/backend/src/test/java/com/woowacourse/moamoa/WebMVCTest.java @@ -4,8 +4,8 @@ import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; -import com.woowacourse.moamoa.auth.config.AuthRequestMatchConfig; -import com.woowacourse.moamoa.auth.controller.AuthenticationInterceptor; +import com.woowacourse.moamoa.auth.controller.interceptor.PathMatcherContainer; +import com.woowacourse.moamoa.auth.controller.interceptor.PathMatcherInterceptor; import com.woowacourse.moamoa.auth.infrastructure.JwtTokenProvider; import com.woowacourse.moamoa.auth.infrastructure.TokenProvider; import com.woowacourse.moamoa.common.MockedServiceObjectsBeanRegister; @@ -28,7 +28,7 @@ import org.springframework.web.context.request.NativeWebRequest; @WebMvcTest(includeFilters = @Filter(type = FilterType.ANNOTATION, classes = RestController.class)) -@Import({JwtTokenProvider.class, AuthRequestMatchConfig.class, MockedServiceObjectsBeanRegister.class}) +@Import({JwtTokenProvider.class, PathMatcherContainer.class, MockedServiceObjectsBeanRegister.class}) public abstract class WebMVCTest { @Autowired @@ -38,10 +38,7 @@ public abstract class WebMVCTest { protected TokenProvider tokenProvider; @Autowired - protected AuthRequestMatchConfig authRequestMatchConfig; - - @Autowired - protected AuthenticationInterceptor authenticationInterceptor; + protected PathMatcherInterceptor pathMatcherInterceptor; @Autowired protected ObjectMapper objectMapper; diff --git a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptorTest.java b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptorTest.java similarity index 81% rename from backend/src/test/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptorTest.java rename to backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptorTest.java index bb42c718c..311b2acb2 100644 --- a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/AuthenticationInterceptorTest.java +++ b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/AuthenticationInterceptorTest.java @@ -1,4 +1,4 @@ -package com.woowacourse.moamoa.auth.controller; +package com.woowacourse.moamoa.auth.controller.interceptor; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -8,6 +8,7 @@ import com.woowacourse.moamoa.common.exception.UnauthorizedException; import java.util.Collections; import java.util.List; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.http.HttpHeaders; @@ -17,16 +18,16 @@ class AuthenticationInterceptorTest extends WebMVCTest { @DisplayName("Preflight 요청인지 확인한다.") @Test - void isPreflightRequest() { + void isPreflightRequest() throws Exception { given(httpServletRequest.getMethod()) .willReturn(HttpMethod.OPTIONS.toString()); - assertThat(authenticationInterceptor.preHandle(httpServletRequest, null, null)).isTrue(); + assertThat(pathMatcherInterceptor.preHandle(httpServletRequest, null, null)).isTrue(); } @DisplayName("유효한 토큰을 검증한다.") @Test - void validateValidToken() { + void validateValidToken() throws Exception { final String token = tokenProvider.createToken(1L).getAccessToken(); String bearerToken = "Bearer " + token; @@ -39,7 +40,7 @@ void validateValidToken() { given(httpServletRequest.getAttribute("payload")).willReturn("1"); - assertThat(authenticationInterceptor.preHandle(httpServletRequest, null, null)).isTrue(); + assertThat(pathMatcherInterceptor.preHandle(httpServletRequest, null, null)).isTrue(); assertThat(httpServletRequest.getAttribute("payload")).isEqualTo(tokenProvider.getPayload(token)); } @@ -55,7 +56,7 @@ void validateInvalidToken() { given(httpServletRequest.getHeaders(HttpHeaders.AUTHORIZATION)) .willReturn(Collections.enumeration(List.of(token))); - assertThatThrownBy(() -> authenticationInterceptor.preHandle(httpServletRequest, null, null)) + assertThatThrownBy(() -> pathMatcherInterceptor.preHandle(httpServletRequest, null, null)) .isInstanceOf(UnauthorizedException.class) .hasMessageContaining("유효하지 않은 토큰입니다."); } diff --git a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainerTest.java b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainerTest.java new file mode 100644 index 000000000..c2582efda --- /dev/null +++ b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathMatcherContainerTest.java @@ -0,0 +1,30 @@ +package com.woowacourse.moamoa.auth.controller.interceptor; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.woowacourse.moamoa.WebMVCTest; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.springframework.beans.factory.annotation.Autowired; + +class PathMatcherContainerTest extends WebMVCTest { + + @Autowired + private PathMatcherContainer pathMatcherContainer; + + @DisplayName("인증이 필요한 요청이 아닌지 확인한다.") + @ParameterizedTest + @CsvSource(value = { + "/api/studies,OPTION,true", + "/api/studies,POST,false", + "/api/studies,GET,true", + "/api/members/me/role,GET,false", + "/api/studies/1/reviews,POST,false", + "/api/studies/1/reviews/1,DELETE,false", + "/api/study/\\d+,PUT,false" + }) + void checkIsValidateRequest(String path, String method, boolean expected) { + assertThat(pathMatcherContainer.isNotIncludePath(path, method)).isEqualTo(expected); + } +} diff --git a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcherTest.java b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcherTest.java new file mode 100644 index 000000000..a715e9ac9 --- /dev/null +++ b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/interceptor/PathRequestMatcherTest.java @@ -0,0 +1,26 @@ +package com.woowacourse.moamoa.auth.controller.interceptor; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.springframework.http.HttpMethod; +import org.springframework.util.AntPathMatcher; + +class PathRequestMatcherTest { + + @DisplayName("요청이 잘 match되는지 검증한다.") + @ParameterizedTest + @CsvSource(value = { + "/api/studies,POST,/api/studies,POST,true", + "/api/studies,GET,/api/studies,POST,false", + "/api/studies/**,POST,/api/studies/1,POST,true", + "/api/studies/**,PUT,/api/studies/1/reviews/1,PUT,true" + }) + void isMatch(String path, HttpMethod method, String targetPath, String targetMethod, boolean expected) { + final PathRequestMatcher pathRequestMatcher = new PathRequestMatcher(path, method); + + assertThat(pathRequestMatcher.match(new AntPathMatcher(), targetPath, targetMethod)).isEqualTo(expected); + } +} diff --git a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherTest.java b/backend/src/test/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherTest.java deleted file mode 100644 index 991bab0c6..000000000 --- a/backend/src/test/java/com/woowacourse/moamoa/auth/controller/matcher/AuthenticationRequestMatcherTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.woowacourse.moamoa.auth.controller.matcher; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.BDDMockito.given; - -import com.woowacourse.moamoa.WebMVCTest; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; -import org.springframework.beans.factory.annotation.Autowired; - -class AuthenticationRequestMatcherTest extends WebMVCTest { - - @Autowired - private AuthenticationRequestMatcher authenticationRequestMatcher; - - @DisplayName("사용자 인증이 필요한 요청인 경우") - @ParameterizedTest - @CsvSource(value = { - "POST,/api/studies", "POST,/api/studies/1/reviews", "GET,/api/my/studies" - }) - void matchAuthRequestIsTrue(String method, String path) { - given(httpServletRequest.getMethod()) - .willReturn(method); - given(httpServletRequest.getRequestURI()) - .willReturn(path); - - assertThat(authenticationRequestMatcher.isRequiredAuth(httpServletRequest)).isTrue(); - } - - @DisplayName("사용자 인증이 필요한 요청이 아닌 경우") - @ParameterizedTest - @CsvSource(value = { - "GET,/api/studies", "GET,/api/studies/1/reviews", "GET,/api/studies/search", "GET,/api/studies/1" - }) - void matchAuthRequestIsFalse(String method, String path) { - given(httpServletRequest.getMethod()) - .willReturn(method); - given(httpServletRequest.getRequestURI()) - .willReturn(path); - - assertThat(authenticationRequestMatcher.isRequiredAuth(httpServletRequest)).isFalse(); - } -}