1+ package me .oldboy .integration .controllers .api_jwt_scenario ;
2+ 3+ import com .fasterxml .jackson .core .type .TypeReference ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
6+ import lombok .SneakyThrows ;
7+ import me .oldboy .config .test_data_source .TestContainerInit ;
8+ import me .oldboy .dto .transaction_dto .TransactionReadDto ;
9+ import me .oldboy .integration .annotation .IT ;
10+ import org .junit .jupiter .api .BeforeEach ;
11+ import org .junit .jupiter .api .Test ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
13+ import org .springframework .http .MediaType ;
14+ import org .springframework .test .web .servlet .MockMvc ;
15+ import org .springframework .test .web .servlet .MvcResult ;
16+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
17+ import org .springframework .web .context .WebApplicationContext ;
18+ 19+ import java .util .List ;
20+ 21+ import static me .oldboy .test_constant .TestConstantFields .EXIST_EMAIL ;
22+ import static me .oldboy .test_constant .TestConstantFields .EXIST_EMAIL_WITH_READ_AUTH ;
23+ import static org .assertj .core .api .Assertions .assertThat ;
24+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .jwt ;
25+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
26+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
27+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
28+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
29+ 30+ @ IT
31+ class JwtBalanceControllerIT extends TestContainerInit {
32+ 33+ @ Autowired
34+ private WebApplicationContext webApplicationContext ;
35+ private MockMvc mockMvc ;
36+ 37+ @ BeforeEach
38+ void setUp (){
39+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
40+ .apply (springSecurity ())
41+ .build ();
42+ }
43+ 44+ /* Фактически для тестирования двух следующих тестов достаточно в токен передать claim - "sub", тут нет проверки ROLE и т.п. */
45+ @ Test
46+ @ SneakyThrows
47+ void getBalanceDetails_ShouldReturnOk_AndDtoList_Test () {
48+ MvcResult result = mockMvc .perform (get ("/api/myBalance" )
49+ .accept (MediaType .APPLICATION_JSON )
50+ .with (jwt ().jwt (builder -> builder .claim ("sub" , EXIST_EMAIL )))) // У клиента с EXIST_EMAIL есть записи в БД
51+ .andExpect (status ().isOk ())
52+ .andReturn ();
53+ 54+ String strResult = result .getResponse ().getContentAsString ();
55+ List <TransactionReadDto > listFromResponse = new ObjectMapper ()
56+ .registerModule (new JavaTimeModule ())
57+ .readValue (strResult , new TypeReference <List <TransactionReadDto >>() {});
58+ 59+ assertThat (listFromResponse .size ()).isGreaterThan (1 );
60+ }
61+ 62+ @ Test
63+ @ SneakyThrows
64+ void getBalanceDetails_ShouldReturn_2xx_NoContent_ClientHasNoTransaction_Test () {
65+ MvcResult result = mockMvc .perform (get ("/api/myBalance" )
66+ .accept (MediaType .APPLICATION_JSON )
67+ .with (jwt ().jwt (builder -> builder .claim ("sub" , EXIST_EMAIL_WITH_READ_AUTH )))) // У клиента с EXIST_EMAIL_WITH_READ_AUTH нет записей в БД
68+ .andExpect (status ().is2xxSuccessful ())
69+ .andReturn ();
70+ 71+ String strResult = result .getResponse ().getContentAsString ();
72+ 73+ assertThat (strResult .isEmpty ()).isTrue ();
74+ }
75+ 76+ @ Test
77+ @ SneakyThrows
78+ void getBalanceDetails_ShouldReturnNotAuth_4xx_Test () {
79+ mockMvc .perform (get ("/api/myBalance" ))
80+ .andExpect (status ().is4xxClientError ())
81+ .andExpect (content ().string ("" ));
82+ }
83+ }
0 commit comments