1
+ package com .moplus .moplus_server .domain .member .controller ;
2
+
3
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
4
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
5
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
6
+
7
+ import com .moplus .moplus_server .global .properties .jwt .JwtProperties ;
8
+ import io .jsonwebtoken .Jwts ;
9
+ import io .jsonwebtoken .security .Keys ;
10
+ import java .security .Key ;
11
+ import java .util .Date ;
12
+ import org .junit .jupiter .api .BeforeEach ;
13
+ import org .junit .jupiter .api .Nested ;
14
+ import org .junit .jupiter .api .Test ;
15
+ import org .springframework .beans .factory .annotation .Autowired ;
16
+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
17
+ import org .springframework .boot .test .context .SpringBootTest ;
18
+ import org .springframework .http .HttpHeaders ;
19
+ import org .springframework .test .context .ActiveProfiles ;
20
+ import org .springframework .test .context .jdbc .Sql ;
21
+ import org .springframework .test .web .servlet .MockMvc ;
22
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
23
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
24
+ import org .springframework .transaction .annotation .Transactional ;
25
+ import org .springframework .web .context .WebApplicationContext ;
26
+
27
+ @ Transactional
28
+ @ SpringBootTest
29
+ @ AutoConfigureMockMvc
30
+ @ ActiveProfiles ("h2test" )
31
+ @ Sql ({"/auth-test-data.sql" })
32
+ class MemberControllerTest {
33
+
34
+ @ Nested
35
+ class 어드민_로그인 {
36
+
37
+ @ Autowired
38
+ private JwtProperties jwtProperties ;
39
+
40
+ @ Autowired
41
+ private MockMvc mockMvc ;
42
+
43
+ @ Autowired
44
+ private WebApplicationContext context ;
45
+
46
+ private String validToken ;
47
+ private Key key ;
48
+
49
+ @ BeforeEach
50
+ public void setMockMvc () throws Exception {
51
+ this .mockMvc = MockMvcBuilders .webAppContextSetup (context )
52
+ .apply (springSecurity ()).build ();
53
+
54
+ key = Keys .hmacShaKeyFor (jwtProperties .accessTokenSecret ().getBytes ());
55
+ Date issuedAt = new Date (); // 3 hour ago
56
+ Date expiredAt = new Date (issuedAt .getTime () + jwtProperties .accessTokenExpirationMilliTime ());
57
+ validToken = Jwts .builder ()
58
+ .setIssuer (jwtProperties .issuer ())
59
+ .setSubject ("1" )
60
+ .claim ("role" , "ROLE_USER" )
61
+ .setIssuedAt (issuedAt )
62
+ .setExpiration (expiredAt )
63
+ .signWith (key )
64
+ .compact ();
65
+ }
66
+
67
+ @ Test
68
+ void 성공 () throws Exception {
69
+ mockMvc .perform (MockMvcRequestBuilders .get ("/api/v1/member/me" )
70
+ .contentType ("application/json" )
71
+ .header (HttpHeaders .AUTHORIZATION , "Bearer " + validToken ))
72
+ .andExpect (status ().isOk ()) // 200 응답 확인
73
+ .andExpect (jsonPath ("$.id" ).exists ()) // MemberGetResponse의 필드 확인
74
+ .andExpect (jsonPath ("$.name" ).exists ())
75
+ .andExpect (jsonPath ("$.email" ).exists ());
76
+ }
77
+ }
78
+ }
0 commit comments