1+ package me .oldboy .integration .controllers .api_user_details_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 .controllers .api .CardsController ;
9+ import me .oldboy .dto .card_dto .CardReadDto ;
10+ import me .oldboy .integration .annotation .IT ;
11+ import org .junit .jupiter .api .BeforeEach ;
12+ import org .junit .jupiter .api .Test ;
13+ import org .springframework .beans .factory .annotation .Autowired ;
14+ import org .springframework .security .test .context .support .WithUserDetails ;
15+ import org .springframework .test .web .servlet .MockMvc ;
16+ import org .springframework .test .web .servlet .MvcResult ;
17+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
18+ import org .springframework .web .context .WebApplicationContext ;
19+ 20+ import java .util .List ;
21+ 22+ import static me .oldboy .test_constant .TestConstantFields .EXIST_EMAIL ;
23+ import static org .assertj .core .api .Assertions .assertThat ;
24+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
25+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
26+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
27+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
28+ 29+ @ IT
30+ class CardsControllerIT extends TestContainerInit {
31+ 32+ @ Autowired
33+ private CardsController cardsController ;
34+ @ Autowired
35+ private WebApplicationContext webApplicationContext ;
36+ private MockMvc mockMvc ;
37+ 38+ @ BeforeEach
39+ void setUp (){
40+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
41+ .apply (springSecurity ())
42+ .build ();
43+ }
44+ 45+ @ Test
46+ @ SneakyThrows
47+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
48+ void getCardDetails_ShouldReturnDtoList_AndOkAdminAuth_Test () {
49+ MvcResult result = mockMvc .perform (get ("/api/myCards" ))
50+ .andExpect (status ().isOk ())
51+ .andReturn ();
52+ 53+ String strResult = result .getResponse ().getContentAsString ();
54+ List <CardReadDto > listFromResponse = new ObjectMapper ()
55+ .registerModule (new JavaTimeModule ())
56+ .readValue (strResult , new TypeReference <List <CardReadDto >>() {});
57+ 58+ assertThat (listFromResponse .size ()).isGreaterThan (1 );
59+ }
60+ 61+ @ Test
62+ @ SneakyThrows
63+ @ WithUserDetails (value = "user3@test.com" , userDetailsServiceBeanName = "clientDetailsService" )
64+ void getCardDetails_ShouldReturnForbidden_NotReadOrAdminAuth_Test () {
65+ mockMvc .perform (get ("/api/myCards" ))
66+ .andExpect (status ().isForbidden ())
67+ .andExpect (content ().string ("" ));
68+ }
69+ }
0 commit comments