backend/restservice/src/test/java/com/plannaplan/controllers/TokenControllerTest.java

90 lines
3.7 KiB
Java
Raw Normal View History

2020-09-25 16:17:52 +02:00
package com.plannaplan.controllers;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
2020-12-23 12:12:50 +01:00
import org.springframework.beans.factory.annotation.Autowired;
2020-09-25 16:17:52 +02:00
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;
2020-12-23 12:12:50 +01:00
import java.lang.reflect.Field;
import java.sql.Timestamp;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
2020-09-25 16:17:52 +02:00
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
2020-10-30 16:53:41 +01:00
public class TokenControllerTest extends AbstractControllerTest {
2020-09-25 16:17:52 +02:00
private final String TOKEN_ENDPOINT = "/token";
2020-12-23 12:12:50 +01:00
private final String REFRESH_TOKEN_ENDPOINT = "/token/refresh";
@Autowired
private UserService service;
2020-09-25 16:17:52 +02:00
@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());
}
2020-12-23 12:12:50 +01:00
@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());
}
2020-09-25 16:17:52 +02:00
}