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 AssignmentsControllerTest extends AbstractControllerTest { private static final String ASSIGFNMENTS_ENDPOINT = "/api/v1/assignments/user"; private static final String TEST_MAIL = "notexistingassignmentuser@mail.domain"; @Autowired private UserService service; @Test public void shouldReturn4xx() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldReturnOk() throws Exception { final User newuser = this.service.save(new User(null, null, TEST_MAIL, UserRoles.TEST_USER)); final String token = this.service.login(newuser).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } }