Skip to content

Commit

Permalink
[JT-81] test: AuthenticationFilter 통합 테스트 작성
Browse files Browse the repository at this point in the history
- SecurityConfig exceptionHandling 추가
  • Loading branch information
ymkim97 committed Sep 22, 2023
1 parent b1c9037 commit ed60730
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package shop.jtoon.security.filter;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("prod")
class AuthenticationFilterIntegrationTest {

@Autowired
MockMvc mockMvc;

@Test
@DisplayName("미인증 사용자 접근 실패")
void unauthenticated_user_fail() throws Exception {
mockMvc.perform(post("/members/test"))
.andExpect(status().isUnauthorized());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

Expand Down Expand Up @@ -59,6 +61,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
authenticationService,
jwtService),
UsernamePasswordAuthenticationFilter.class)
.exceptionHandling(exceptionHandling -> exceptionHandling
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)))
.oauth2Login(login -> login
.userInfoEndpoint(endpoint -> endpoint.userService(customOAuth2UserService))
.successHandler(oAuth2SuccessHandler)
Expand Down

0 comments on commit ed60730

Please sign in to comment.