package com.plannaplan.controllers; import org.junit.Ignore; 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.test.web.servlet.result.MockMvcResultMatchers.status; import java.lang.reflect.Field; import java.sql.Timestamp; import com.plannaplan.entities.User; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class TokenControllerTest extends AbstractControllerTest { private final String TOKEN_ENDPOINT = "/token"; private final String REFRESH_TOKEN_ENDPOINT = "/token/refresh"; @Autowired private UserService service; @Test public void shouldFailWithNoParameter() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(TOKEN_ENDPOINT)).andExpect(status().isBadRequest()); } @Test public void shouldFailWithWrongTicket() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(TOKEN_ENDPOINT).param("ticket", "totaly-wrong-ticket")) .andExpect(status().is4xxClientError()); } @Test @Ignore public void shouldReturnToken() throws Exception { // have no idea how to make this test independent from user that run this String ticket = ""; MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(TOKEN_ENDPOINT).param("ticket", ticket)).andExpect(status().isOk()); } @Test public void shouldFailWithWrongRefreshToken() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(REFRESH_TOKEN_ENDPOINT).param("refreshToken", "totaly-wrong-refresh-token")) .andExpect(status().is4xxClientError()); } @Test public void shouldRetrunTokenBeforeExpirationOfOldOne() throws Exception { User user = new User("Eren", "Yager", "shouldRetrunTokenBeforeExpirationOfOldOne@TokenController.test", UserRoles.TEST_USER); user.setToken("Totalnie-bezpieczny-token"); this.service.save(user); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(REFRESH_TOKEN_ENDPOINT).param("refreshToken", user.getRefreshToken())) .andExpect(status().isOk()); } @Test public void shouldRetrunTokenAfterExpirationOfOldOne() throws Exception { final Field reader = User.class.getDeclaredField("tokenUsageDate"); reader.setAccessible(true); User user = new User("Mikasa", "Ackerman", "shouldRetrunTokenAfterExpirationOfOldOne@TokenController.test", UserRoles.TEST_USER); user.setToken("Totalnie-bezpieczny-token"); reader.set(user, new Timestamp(System.currentTimeMillis() - 86400000)); this.service.save(user); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(REFRESH_TOKEN_ENDPOINT).param("refreshToken", user.getRefreshToken())) .andExpect(status().isOk()); } }