|
| 1 | +package com.senifit.was.security; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.mock.web.MockHttpSession; |
| 9 | +import org.springframework.test.web.servlet.MockMvc; |
| 10 | +import org.springframework.test.web.servlet.MvcResult; |
| 11 | + |
| 12 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; |
| 13 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 14 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 15 | + |
| 16 | +/** |
| 17 | + * 세션 기반 인증 테스트 |
| 18 | + * |
| 19 | + * 이 테스트는 백엔드의 세션 관리가 정상 동작하는지 검증합니다. |
| 20 | + * - 로그인 후 세션 쿠키가 유지되는지 |
| 21 | + * - 세션을 통한 인증된 요청이 정상 처리되는지 |
| 22 | + * - 세션 없이 요청하면 401/403이 반환되는지 |
| 23 | + */ |
| 24 | +@SpringBootTest |
| 25 | +@AutoConfigureMockMvc |
| 26 | +class SessionAuthenticationTest { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("인증 없이 보호된 API 호출 시 403 반환") |
| 33 | + void unauthenticatedRequest_shouldReturn403() throws Exception { |
| 34 | + mockMvc.perform(get("/center")) |
| 35 | + .andExpect(status().isForbidden()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("로그인 후 세션으로 인증된 API 호출 성공") |
| 40 | + void authenticatedRequest_withSession_shouldSucceed() throws Exception { |
| 41 | + // 1. 로그인 수행 (테스트용 계정 필요) |
| 42 | + // 실제 테스트 시에는 테스트용 계정 정보로 변경 필요 |
| 43 | + MvcResult loginResult = mockMvc.perform( |
| 44 | + formLogin("/auth/signin") |
| 45 | + .user("id", "test_account") // 실제 테스트 계정으로 변경 |
| 46 | + .password("password", "test_password") // 실제 비밀번호로 변경 |
| 47 | + ).andReturn(); |
| 48 | + |
| 49 | + // 2. 로그인 응답에서 세션 추출 |
| 50 | + MockHttpSession session = (MockHttpSession) loginResult.getRequest().getSession(); |
| 51 | + |
| 52 | + // 3. 세션을 포함하여 보호된 API 호출 |
| 53 | + if (loginResult.getResponse().getStatus() == 200) { |
| 54 | + mockMvc.perform(get("/center").session(session)) |
| 55 | + .andExpect(status().isOk()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("세션 없이 요청 → 새 세션으로 요청: 403 반환") |
| 61 | + void requestWithoutSessionCookie_shouldReturn403() throws Exception { |
| 62 | + // 첫 번째 요청 (세션 없음) |
| 63 | + MvcResult result1 = mockMvc.perform(get("/center")) |
| 64 | + .andExpect(status().isForbidden()) |
| 65 | + .andReturn(); |
| 66 | + |
| 67 | + // 두 번째 요청 (다른 세션) |
| 68 | + mockMvc.perform(get("/center")) |
| 69 | + .andExpect(status().isForbidden()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @DisplayName("로그인 엔드포인트는 인증 없이 접근 가능") |
| 74 | + void loginEndpoint_shouldBeAccessible() throws Exception { |
| 75 | + mockMvc.perform(get("/health")) |
| 76 | + .andExpect(status().isOk()); |
| 77 | + } |
| 78 | +} |
0 commit comments