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