package com.plannaplan.controllers; import com.plannaplan.entities.User; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class UsersControllerTest extends AbstractControllerTest { private static final String SEARCH_ENDPOINT = "/api/v1/users/student/search"; private static final String ALL_USERS_ENDPOINT = "/api/v1/users/students"; @Autowired private UserService service; /* SEARCH_ENDPOINT */ @Test public void shouldRestrun200OK() throws Exception { final String email = "notexistingassignmentuser@shouldRestrun200OK.test"; final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(SEARCH_ENDPOINT).param("query", "").header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } @Test public void shouldRestrunForbiden() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(SEARCH_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldFailedDueToMissingParam() throws Exception { final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test"; final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(SEARCH_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } /* ALL_USERS_ENDPOINT */ @Test public void shouldRestrunAllStudents200OK() throws Exception { final String email = "notexistingassignmentuser@shouldRestrunAllStudents200OK.test"; final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test public void shouldRestrunAllStudentsForbiden() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(ALL_USERS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldDenyAllStudentsTryByStudent() throws Exception { final String email = "notexistingassignmentuser@shouldDenyAllStudentsTryByStudent.test"; final User user = this.service.save(new User(null, null, email, UserRoles.STUDENT)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } }