1
+ package me .oldboy .integration .controllers .api_jwt_scenario ;
2
+
3
+ import lombok .SneakyThrows ;
4
+ import me .oldboy .config .test_data_source .TestContainerInit ;
5
+ import me .oldboy .integration .annotation .IT ;
6
+ import org .junit .jupiter .api .BeforeEach ;
7
+ import org .junit .jupiter .api .Test ;
8
+ import org .springframework .beans .factory .annotation .Autowired ;
9
+ import org .springframework .http .MediaType ;
10
+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
11
+ import org .springframework .test .web .servlet .MockMvc ;
12
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
13
+ import org .springframework .web .context .WebApplicationContext ;
14
+
15
+ import static me .oldboy .test_constant .TestConstantFields .EXIST_EMAIL_WITH_READ_AUTH ;
16
+ import static org .hamcrest .Matchers .containsString ;
17
+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .jwt ;
18
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
19
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
20
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
21
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
22
+
23
+ @ IT
24
+ class JwtAccountControllerIT extends TestContainerInit {
25
+
26
+ @ Autowired
27
+ private WebApplicationContext webApplicationContext ;
28
+ private MockMvc mockMvc ;
29
+
30
+ @ BeforeEach
31
+ void setUp (){
32
+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
33
+ .apply (springSecurity ())
34
+ .build ();
35
+ }
36
+
37
+ /* В данном тесте для его прохождения нам нужен только claim - "sub", остальные "антураж" (можно удалить) */
38
+ @ Test
39
+ @ SneakyThrows
40
+ void getAccountDetails_ShouldReturnOk_AndAccountRecord_CorrectJwt_Test () {
41
+ mockMvc .perform (get ("/api/myAccount" )
42
+ .accept (MediaType .APPLICATION_JSON )
43
+ .with (jwt ().jwt (builder -> builder
44
+ .claim ("sub" , EXIST_EMAIL_WITH_READ_AUTH ) // Существенный для теста claim
45
+ .claim ("preferred_username" , EXIST_EMAIL_WITH_READ_AUTH ) // НЕ существенный для теста claim
46
+ .claim ("scope" , "openid profile" )) // НЕ существенный для теста claim
47
+ .authorities (new SimpleGrantedAuthority ("ROLE_READ" )))) // НЕ существенный для теста claim
48
+ .andExpect (status ().isOk ())
49
+ .andExpect (content ().contentType (MediaType .APPLICATION_JSON ))
50
+ .andExpect (content ().string (containsString ("accountNumber" )))
51
+ .andExpect (content ().string (containsString ("accountType" )))
52
+ .andExpect (content ().string (containsString ("branchAddress" )))
53
+ .andExpect (content ().string (containsString ("createDt" )));
54
+ }
55
+
56
+ @ Test
57
+ @ SneakyThrows
58
+ void getAccountDetails_ShouldReturn_401_WithOutJwt_Test () {
59
+ mockMvc .perform (get ("/api/myAccount" )
60
+ .accept (MediaType .APPLICATION_JSON ))
61
+ .andExpect (status ().is4xxClientError ())
62
+ .andExpect (content ().string ("" ));
63
+ }
64
+ }
0 commit comments