Skip to content

Commit b269c61

Browse files
committed
Test: JWT 인증 인프라 통합 테스트
1 parent 7aebe80 commit b269c61

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.back;
2+
3+
import com.back.global.security.JwtTokenProvider;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
@SpringBootTest
15+
@AutoConfigureMockMvc
16+
class JwtSecurityIntegrationTest {
17+
18+
@Autowired
19+
private MockMvc mockMvc;
20+
21+
@Autowired
22+
private JwtTokenProvider jwtTokenProvider;
23+
24+
// @Test
25+
// @DisplayName("public 엔드포인트 접근 시 200 OK 반환")
26+
// void givenNoToken_whenAccessPublic_thenReturn200() throws Exception {
27+
// mockMvc.perform(get("/api/test/public"))
28+
// .andExpect(status().isOk());
29+
// }
30+
31+
@Test
32+
@DisplayName("일반 유저가 /me 접근 시 200 OK 반환")
33+
void givenUserToken_whenAccessMe_thenReturn200() throws Exception {
34+
// ROLE_USER 토큰 발급
35+
String userToken = jwtTokenProvider.createAccessToken(3L, "user2", "USER");
36+
37+
mockMvc.perform(get("/api/test/me")
38+
.header("Authorization", "Bearer " + userToken))
39+
.andExpect(status().isOk());
40+
}
41+
42+
@Test
43+
@DisplayName("토큰 없이 /me 접근 시 401 Unauthorized 반환")
44+
void givenNoToken_whenAccessMe_thenReturn401() throws Exception {
45+
mockMvc.perform(get("/api/test/me"))
46+
.andExpect(status().isUnauthorized());
47+
}
48+
49+
@Test
50+
@DisplayName("관리자가 /admin 접근 시 200 OK 반환")
51+
void givenAdminToken_whenAccessAdmin_thenReturn200() throws Exception {
52+
// ROLE_ADMIN 토큰 발급
53+
String adminToken = jwtTokenProvider.createAccessToken(2L, "admin1", "ADMIN");
54+
55+
mockMvc.perform(get("/api/test/admin")
56+
.header("Authorization", "Bearer " + adminToken))
57+
.andExpect(status().isOk());
58+
}
59+
60+
@Test
61+
@DisplayName("일반 유저가 /admin 접근 시 403 Forbidden 반환")
62+
void givenUserToken_whenAccessAdmin_thenReturn403() throws Exception {
63+
// ROLE_USER 토큰 발급
64+
String userToken = jwtTokenProvider.createAccessToken(1L, "user1", "USER");
65+
66+
mockMvc.perform(get("/api/test/admin")
67+
.header("Authorization", "Bearer " + userToken))
68+
.andExpect(status().isForbidden());
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.back.api;
2+
3+
import com.back.global.security.CustomUserDetails;
4+
import org.springframework.security.access.prepost.PreAuthorize;
5+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/api/test")
12+
public class TestController {
13+
14+
// @GetMapping("/public")
15+
// public String publicApi() {
16+
// return "누구나 접근 가능";
17+
// }
18+
19+
@GetMapping("/me")
20+
public String me(@AuthenticationPrincipal CustomUserDetails user) {
21+
return "내 정보: " + user.getUsername() + " (id=" + user.getUserId() + ")";
22+
}
23+
24+
@GetMapping("/admin")
25+
@PreAuthorize("hasRole('ADMIN')")
26+
public String adminOnly() {
27+
return "관리자 전용 API";
28+
}
29+
}

0 commit comments

Comments
 (0)