Fix AuthControllerTests.

This commit is contained in:
Jesse Brault 2025-12-26 23:32:28 -06:00
parent 2642f6100e
commit cf8ebe984b

View File

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