package com.plannaplan.controllers; 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.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import com.plannaplan.entities.User; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class DeveloperControllerTest extends AbstractControllerTest { private static final String ACCEPT_ENDPOINT = "/api/v1/developer/algoythm/accept"; @Autowired private UserService userService; @Test public void shouldFailWithWrongAcces() throws Exception { final String mail = "shouldFailWithWrongAcces@ConfigController.test"; final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER)); final String token = this.userService.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldOkPerformingAcceptAlgotyrhm() throws Exception { final String mail = "shouldOkPerformingAcceptAlgotyrhm@ConfigController.test"; final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER)); final String token = this.userService.login(usr).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk()); } @Test public void shouldFailWithNoToken() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ACCEPT_ENDPOINT)).andExpect(status().is4xxClientError()); } }