Fix AuthControllerTests.
This commit is contained in:
parent
2642f6100e
commit
cf8ebe984b
@ -1,22 +1,23 @@
|
|||||||
package app.mealsmadeeasy.api.auth;
|
package app.mealsmadeeasy.api.auth;
|
||||||
|
|
||||||
|
import app.mealsmadeeasy.api.IntegrationTestsExtension;
|
||||||
import app.mealsmadeeasy.api.user.User;
|
import app.mealsmadeeasy.api.user.User;
|
||||||
import app.mealsmadeeasy.api.user.UserCreateException;
|
import app.mealsmadeeasy.api.user.UserCreateException;
|
||||||
import app.mealsmadeeasy.api.user.UserService;
|
import app.mealsmadeeasy.api.user.UserService;
|
||||||
import jakarta.servlet.http.Cookie;
|
import jakarta.servlet.http.Cookie;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
import tools.jackson.databind.ObjectMapper;
|
import tools.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
@ -27,8 +28,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
|
@ExtendWith(IntegrationTestsExtension.class)
|
||||||
public class AuthControllerTests {
|
public class AuthControllerTests {
|
||||||
|
|
||||||
|
private static final String TEST_PASSWORD = "test";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObjectMapper objectMapper;
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
@ -38,43 +42,38 @@ public class AuthControllerTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
private User createTestUser() {
|
private User seedUser() {
|
||||||
|
final String uuid = UUID.randomUUID().toString();
|
||||||
try {
|
try {
|
||||||
return this.userService.createUser("test", "test@test.com", "test");
|
return this.userService.createUser(uuid, uuid + "@test.com", TEST_PASSWORD);
|
||||||
} catch (UserCreateException e) {
|
} catch (UserCreateException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MockHttpServletRequestBuilder getLoginRequest() {
|
private MockHttpServletRequestBuilder getLoginRequest(String username, String password) {
|
||||||
final Map<String, ?> body = Map.of(
|
final Map<String, Object> body = Map.of(
|
||||||
"username", "test",
|
"username", username,
|
||||||
"password", "test"
|
"password", password
|
||||||
);
|
);
|
||||||
return post("/auth/login")
|
return post("/auth/login")
|
||||||
.content(this.objectMapper.writeValueAsString(body))
|
.content(this.objectMapper.writeValueAsString(body))
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.with(user("test").password("test"));
|
.with(user(username).password(password));
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setup() {
|
|
||||||
final User testUser = this.createTestUser();
|
|
||||||
System.out.println("Created testUser: " + testUser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DirtiesContext
|
|
||||||
public void simpleLogin() throws Exception {
|
public void simpleLogin() throws Exception {
|
||||||
this.mockMvc.perform(this.getLoginRequest())
|
final User user = this.seedUser();
|
||||||
|
this.mockMvc.perform(this.getLoginRequest(user.getUsername(), TEST_PASSWORD))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$.username").value("test"))
|
.andExpect(jsonPath("$.username").value(user.getUsername()))
|
||||||
.andExpect(jsonPath("$.accessToken").isString())
|
.andExpect(jsonPath("$.accessToken").isString())
|
||||||
.andExpect(cookie().exists("refresh-token"));
|
.andExpect(cookie().exists("refresh-token"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Cookie getRefreshTokenCookie() throws Exception {
|
private Cookie getRefreshTokenCookie(String username, String password) throws Exception {
|
||||||
final MvcResult loginResult = this.mockMvc.perform(this.getLoginRequest()).andReturn();
|
final MvcResult loginResult = this.mockMvc.perform(this.getLoginRequest(username, password)).andReturn();
|
||||||
final Cookie refreshTokenCookie = loginResult.getResponse().getCookie("refresh-token");
|
final Cookie refreshTokenCookie = loginResult.getResponse().getCookie("refresh-token");
|
||||||
if (refreshTokenCookie == null) {
|
if (refreshTokenCookie == null) {
|
||||||
throw new NullPointerException("refreshTokenCookie is null");
|
throw new NullPointerException("refreshTokenCookie is null");
|
||||||
@ -83,24 +82,24 @@ public class AuthControllerTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DirtiesContext
|
|
||||||
public void simpleLogout() throws Exception {
|
public void simpleLogout() throws Exception {
|
||||||
|
final User user = this.seedUser();
|
||||||
final MockHttpServletRequestBuilder req = post("/auth/logout")
|
final MockHttpServletRequestBuilder req = post("/auth/logout")
|
||||||
.cookie(this.getRefreshTokenCookie());
|
.cookie(this.getRefreshTokenCookie(user.getUsername(), TEST_PASSWORD));
|
||||||
this.mockMvc.perform(req)
|
this.mockMvc.perform(req)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(cookie().maxAge("refresh-token", 0));
|
.andExpect(cookie().maxAge("refresh-token", 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DirtiesContext
|
|
||||||
public void simpleRefresh() throws Exception {
|
public void simpleRefresh() throws Exception {
|
||||||
final Cookie firstRefreshTokenCookie = this.getRefreshTokenCookie();
|
final User user = this.seedUser();
|
||||||
|
final Cookie firstRefreshTokenCookie = this.getRefreshTokenCookie(user.getUsername(), TEST_PASSWORD);
|
||||||
final MockHttpServletRequestBuilder req = post("/auth/refresh")
|
final MockHttpServletRequestBuilder req = post("/auth/refresh")
|
||||||
.cookie(firstRefreshTokenCookie);
|
.cookie(firstRefreshTokenCookie);
|
||||||
final MvcResult res = this.mockMvc.perform(req)
|
final MvcResult res = this.mockMvc.perform(req)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$.username").value("test"))
|
.andExpect(jsonPath("$.username").value(user.getUsername()))
|
||||||
.andExpect(jsonPath("$.accessToken").isString())
|
.andExpect(jsonPath("$.accessToken").isString())
|
||||||
.andExpect(cookie().exists("refresh-token"))
|
.andExpect(cookie().exists("refresh-token"))
|
||||||
.andReturn();
|
.andReturn();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user