diff --git a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java index 4f5391c..68dbe23 100644 --- a/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeControllerTests.java @@ -3,6 +3,10 @@ package app.mealsmadeeasy.api.recipe; import app.mealsmadeeasy.api.auth.AuthService; import app.mealsmadeeasy.api.auth.LoginDetails; import app.mealsmadeeasy.api.auth.LoginException; +import app.mealsmadeeasy.api.image.Image; +import app.mealsmadeeasy.api.image.ImageService; +import app.mealsmadeeasy.api.image.S3ImageServiceTests; +import app.mealsmadeeasy.api.image.spec.ImageCreateInfoSpec; import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec; import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec; import app.mealsmadeeasy.api.recipe.star.RecipeStarService; @@ -17,7 +21,15 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; +import org.testcontainers.containers.MinIOContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +import java.io.InputStream; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.nullValue; @@ -25,10 +37,27 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@Testcontainers @SpringBootTest @AutoConfigureMockMvc public class RecipeControllerTests { + @Container + private static final MinIOContainer container = new MinIOContainer( + DockerImageName.parse("minio/minio:latest") + ); + + @DynamicPropertySource + public static void minioProperties(DynamicPropertyRegistry registry) { + registry.add("app.mealsmadeeasy.api.minio.endpoint", container::getS3URL); + registry.add("app.mealsmadeeasy.api.minio.accessKey", container::getUserName); + registry.add("app.mealsmadeeasy.api.minio.secretKey", container::getPassword); + } + + private static InputStream getHal9000() { + return S3ImageServiceTests.class.getClassLoader().getResourceAsStream("HAL9000.svg"); + } + @Autowired private MockMvc mockMvc; @@ -44,6 +73,9 @@ public class RecipeControllerTests { @Autowired private AuthService authService; + @Autowired + private ImageService imageService; + @Autowired private ObjectMapper objectMapper; @@ -77,6 +109,20 @@ public class RecipeControllerTests { .getToken(); } + private Image createHal9000(User owner) { + try (final InputStream hal9000 = getHal9000()) { + return this.imageService.create( + owner, + "HAL9000.svg", + hal9000, + 27881L, + new ImageCreateInfoSpec() + ); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Test @DirtiesContext public void getRecipePageViewByIdPublicRecipeNoPrincipal() throws Exception { @@ -253,6 +299,41 @@ public class RecipeControllerTests { .andExpect(jsonPath("$.recipe.rawText").value("# Hello, Updated World!")); } + @Test + @DirtiesContext + public void updateRecipeReturnsViewWithMainImage() throws Exception { + final User owner = this.createTestUser("owner"); + + final Image hal9000 = this.createHal9000(owner); + + final RecipeCreateSpec createSpec = new RecipeCreateSpec(); + createSpec.setTitle("Test Recipe"); + createSpec.setSlug("test-recipe"); + createSpec.setPublic(false); + createSpec.setRawText("# Hello, World!"); + createSpec.setMainImage(hal9000); + Recipe recipe = this.recipeService.create(owner, createSpec); + + final RecipeUpdateSpec updateSpec = new RecipeUpdateSpec(); + updateSpec.setTitle("Updated Test Recipe"); + updateSpec.setRawText("# Hello, Updated World!"); + final RecipeUpdateSpec.MainImageUpdateSpec mainImageUpdateSpec = new RecipeUpdateSpec.MainImageUpdateSpec(); + mainImageUpdateSpec.setUsername(hal9000.getOwner().getUsername()); + mainImageUpdateSpec.setFilename(hal9000.getUserFilename()); + updateSpec.setMainImageUpdateSpec(mainImageUpdateSpec); + final String body = this.objectMapper.writeValueAsString(updateSpec); + + final String accessToken = this.getAccessToken(owner); + this.mockMvc.perform( + post("/recipes/{username}/{slug}", owner.getUsername(), recipe.getSlug()) + .header("Authorization", "Bearer " + accessToken) + .contentType(MediaType.APPLICATION_JSON) + .content(body) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.recipe.mainImage").isMap()); + } + @Test @DirtiesContext public void addStarToRecipe() throws Exception { diff --git a/src/integrationTest/resources/HAL9000.svg b/src/integrationTest/resources/HAL9000.svg new file mode 100644 index 0000000..e6394c0 --- /dev/null +++ b/src/integrationTest/resources/HAL9000.svg @@ -0,0 +1,740 @@ + + + + + HAL9000 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + HAL9000 + + + + MorningLemon + + + German + + + HAL + 9000 + HAL9000 + robot + space + + + The famous red eye of HAL 9000 from Stanley Kubricks Film "2001: A Space Odyssey". + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +