Using dev profile for running locally, and all integration tests now passing.

This commit is contained in:
Jesse Brault 2024-07-23 10:17:41 -05:00
parent d98101b8a4
commit 467a69460d
3 changed files with 64 additions and 29 deletions

View File

@ -1,12 +1,17 @@
package app.mealsmadeeasy.api.auth;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserCreateException;
import app.mealsmadeeasy.api.user.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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;
@ -30,6 +35,17 @@ public class AuthControllerTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private UserService userService;
private User createTestUser() {
try {
return this.userService.createUser("test", "test@test.com", "test");
} catch (UserCreateException e) {
throw new RuntimeException(e);
}
}
private MockHttpServletRequestBuilder getLoginRequest() throws Exception {
final Map<String, ?> body = Map.of(
"username", "test",
@ -41,7 +57,14 @@ public class AuthControllerTests {
.with(user("test").password("test"));
}
@BeforeEach
public void setup() {
final User testUser = this.createTestUser();
System.out.println("Created testUser: " + testUser);
}
@Test
@DirtiesContext
public void simpleLogin() throws Exception {
this.mockMvc.perform(this.getLoginRequest())
.andExpect(status().isOk())
@ -60,6 +83,7 @@ public class AuthControllerTests {
}
@Test
@DirtiesContext
public void simpleLogout() throws Exception {
final MockHttpServletRequestBuilder req = post("/auth/logout")
.cookie(this.getRefreshTokenCookie());
@ -69,6 +93,7 @@ public class AuthControllerTests {
}
@Test
@DirtiesContext
public void simpleRefresh() throws Exception {
final Cookie firstRefreshTokenCookie = this.getRefreshTokenCookie();
final MockHttpServletRequestBuilder req = post("/auth/refresh")

View File

@ -0,0 +1,39 @@
package app.mealsmadeeasy.api;
import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.recipe.RecipeService;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.Set;
@Configuration
@Profile("dev")
public class DevConfiguration {
private final UserService userService;
private final RecipeService recipeService;
public DevConfiguration(UserService userService, RecipeService recipeService) {
this.userService = userService;
this.recipeService = recipeService;
}
@Bean
public CommandLineRunner addTestData() {
return args -> {
final User testUser = this.userService.createUser(
"test", "test@test.com", "test", Set.of()
);
final Recipe recipe = this.recipeService.create(testUser, "Test Recipe", "Hello, World!");
this.recipeService.setPublic(recipe, testUser, true);
System.out.println("Created " + testUser);
System.out.println("Created " + recipe);
};
}
}

View File

@ -1,15 +1,7 @@
package app.mealsmadeeasy.api;
import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.recipe.RecipeService;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.Set;
@SpringBootApplication
public class MealsMadeEasyApiApplication {
@ -18,25 +10,4 @@ public class MealsMadeEasyApiApplication {
SpringApplication.run(MealsMadeEasyApiApplication.class, args);
}
private final UserService userService;
private final RecipeService recipeService;
public MealsMadeEasyApiApplication(UserService userService, RecipeService recipeService) {
this.userService = userService;
this.recipeService = recipeService;
}
@Bean
public CommandLineRunner addTestData() {
return args -> {
final User testUser = this.userService.createUser(
"test", "test@test.com", "test", Set.of()
);
final Recipe recipe = this.recipeService.create(testUser, "Test Recipe", "Hello, World!");
this.recipeService.setPublic(recipe, testUser, true);
System.out.println("Created " + testUser);
System.out.println("Created " + recipe);
};
}
}