Added tests

This commit is contained in:
Filip Izydorczyk 2020-12-23 12:12:50 +01:00
parent 61c5a43192
commit 6eac8e6266
2 changed files with 55 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package com.plannaplan.controllers;
import java.util.Optional;
import java.util.UUID;
import com.plannaplan.entities.User;
@ -67,9 +68,13 @@ public class TokenController {
@GetMapping("/token/refresh")
@ApiOperation(value = "Endpoint to access new token based on refresh token. It's needed when request with provided token fail with code 403")
public ResponseEntity<TokenResponse> getRefreshToken(
@RequestParam("refreshToken") @ApiParam(value = "Refresh token obtained in /token request") final String refreshToken) {
User user = this.userService.getUserByRefreshToken(refreshToken)
.orElseThrow(() -> new NullPointerException("User not found"));
@RequestParam("refreshToken") @ApiParam(value = "Refresh token obtained in /token request") final String refreshToken)
throws NullPointerException {
Optional<User> userResult = this.userService.getUserByRefreshToken(refreshToken);
if (userResult.isEmpty()) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
User user = userResult.get();
user.setToken(UUID.randomUUID().toString());
user = this.userService.save(user);
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);

View File

@ -3,6 +3,7 @@ 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;
@ -12,11 +13,22 @@ 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 {
@ -39,4 +51,39 @@ public class TokenControllerTest extends AbstractControllerTest {
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());
}
}