From 2322b148c7f36738c8f738e768f0bcd14e052508 Mon Sep 17 00:00:00 2001 From: AvilaAndre Date: Wed, 6 Nov 2024 16:04:44 +0000 Subject: [PATCH] test: added gallery tests to Project --- .../backend/controller/ProjectController.kt | 14 ++ .../backend/controller/EventControllerTest.kt | 3 +- .../controller/ProjectControllerTest.kt | 192 ++++++++++++++++++ 3 files changed, 208 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt index ab4a2c06..71014234 100644 --- a/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt +++ b/src/main/kotlin/pt/up/fe/ni/website/backend/controller/ProjectController.kt @@ -88,4 +88,18 @@ class ProjectController(private val service: ProjectService) { @PathVariable idProject: Long, @PathVariable idAccount: Long ) = service.removeHallOfFameMemberById(idProject, idAccount) + + @PutMapping("/{idProject}/gallery", consumes = ["multipart/form-data"]) + fun addGalleryImage( + @PathVariable idProject: Long, + @RequestParam + @ValidImage + image: MultipartFile + ) = service.addGalleryImage(idProject, image, ProjectService.IMAGE_FOLDER) + + @DeleteMapping("/{idProject}/gallery") + fun removeGalleryImage( + @PathVariable idProject: Long, + @RequestPart imageUrl: String + ) = service.removeGalleryImage(idProject, imageUrl) } diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt index 413baed9..3c0a8411 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/EventControllerTest.kt @@ -985,7 +985,8 @@ internal class EventControllerTest @Autowired constructor( @Test fun `should fail if image does not exist`() { - val wrongImageUrl = "${uploadConfigProperties.staticServe}/gallery/Another${testEvent.title}-$uuid.jpeg" + val wrongImageUrl = + "${uploadConfigProperties.staticServe}/gallery/events/Another${testEvent.title}-$uuid.jpeg" mockMvc.multipartBuilder("/events/${testEvent.id}/gallery") .asDeleteMethod() diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt index 4ef2b38a..7d54062a 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/ProjectControllerTest.kt @@ -1561,6 +1561,198 @@ internal class ProjectControllerTest @Autowired constructor( } } + @NestedTest + @DisplayName("PUT /projects/{idProject}/gallery") + inner class AddGalleryImage { + + private val uuid: UUID = UUID.randomUUID() + private val mockedSettings = Mockito.mockStatic(UUID::class.java) + + @BeforeAll + fun setupMocks() { + Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) + } + + @AfterAll + fun cleanup() { + mockedSettings.close() + } + + @BeforeEach + fun addToRepositories() { + accountRepository.save(testAccount) + repository.save(testProject) + } + + private val parameters = listOf( + parameterWithName("idProject").description("The id of the project being targeted") + ) + + @Test + fun `should add an image`() { + val expectedImagePath = + "${uploadConfigProperties.staticServe}/projects/gallery/${testProject.title}-$uuid.jpeg" + + mockMvc.multipartBuilder("/projects/${testProject.id}/gallery") + .asPutMethod() + .addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE) + .perform() + .andExpectAll( + status().isOk, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.title").value(testProject.title), + jsonPath("$.description").value(testProject.description), + jsonPath("$.teamMembers.length()").value(testProject.teamMembers.size), + jsonPath("$.isArchived").value(testProject.isArchived), + jsonPath("$.slug").value(testProject.slug), + jsonPath("$.slogan").value(testProject.slogan), + jsonPath("$.targetAudience").value(testProject.targetAudience), + jsonPath("$.github").value(testProject.github), + jsonPath("$.links.length()").value(testProject.links.size), + jsonPath("$.timeline.length()").value(testProject.timeline.size), + jsonPath("$.image").value(testProject.image), + jsonPath("$.gallery.length()").value(1), + jsonPath("$.gallery[0]").value(expectedImagePath) + ) + /* + .andDocument( + documentation, + "Adds an image to the gallery of the selected project", + "This endpoint adds an image to the gallery of an project, returning the project's updated data.", + urlParameters = parameters + ) + */ + } + + @Test + fun `should fail if project does not exist`() { + val nonexistentID = 5 + + mockMvc.multipartBuilder("/projects/$nonexistentID/gallery") + .asPutMethod() + .addFile("image", contentType = MediaType.IMAGE_JPEG_VALUE) + .perform() + .andExpectAll( + status().isNotFound, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.errors.length()").value(1), + jsonPath("$.errors[0].message").value("activity not found with id $nonexistentID") + ).andDocumentErrorResponse(documentation) + } + + @Test + fun `should fail if image in wrong format`() { + mockMvc.multipartBuilder("/projects/${testProject.id}/gallery") + .asPutMethod() + .addFile("image", filename = "image.gif", contentType = MediaType.IMAGE_JPEG_VALUE) + .perform() + .andExpectAll( + status().isBadRequest, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.errors.length()").value(1), + jsonPath("$.errors[0].message").value("invalid image type (png, jpg, jpeg or webp)") + ).andDocumentErrorResponse(documentation) + } + } + + @NestedTest + @DisplayName("DELETE /projects/{idProject}/gallery") + inner class RemoveGalleryImage { + + private val uuid: UUID = UUID.randomUUID() + private val mockedSettings = Mockito.mockStatic(UUID::class.java) + private val mockImageUrl = + "${uploadConfigProperties.staticServe}/projects/gallery/${testProject.title}-$uuid.jpeg" + + @BeforeAll + fun setupMocks() { + Mockito.`when`(UUID.randomUUID()).thenReturn(uuid) + + testProject.gallery.add(mockImageUrl) + } + + @AfterAll + fun cleanup() { + mockedSettings.close() + } + + @BeforeEach + fun addToRepositories() { + accountRepository.save(testAccount) + + repository.save(testProject) + } + + private val parameters = listOf( + parameterWithName("idProject").description("The id of the project being targeted") + ) + + @Test + fun `should remove an image`() { + mockMvc.multipartBuilder("/projects/${testProject.id}/gallery") + .asDeleteMethod() + .addPart("imageUrl", mockImageUrl) + .perform() + .andExpectAll( + status().isOk, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.title").value(testProject.title), + jsonPath("$.description").value(testProject.description), + jsonPath("$.teamMembers.length()").value(testProject.teamMembers.size), + jsonPath("$.isArchived").value(testProject.isArchived), + jsonPath("$.slug").value(testProject.slug), + jsonPath("$.slogan").value(testProject.slogan), + jsonPath("$.targetAudience").value(testProject.targetAudience), + jsonPath("$.github").value(testProject.github), + jsonPath("$.links.length()").value(testProject.links.size), + jsonPath("$.timeline.length()").value(testProject.timeline.size), + jsonPath("$.image").value(testProject.image), + jsonPath("$.gallery.length()").value(0) + ) + /* + .andDocument( + documentation, + "Removes an image from the gallery of a selected project", + "This endpoint removes an image to the gallery of a project, returning the project's updated data.", + urlParameters = parameters + ) + */ + } + + @Test + fun `should fail if project does not exist`() { + val nonexistentID = 5 + + mockMvc.multipartBuilder("/projects/$nonexistentID/gallery") + .asDeleteMethod() + .addPart("imageUrl", mockImageUrl) + .perform() + .andExpectAll( + status().isNotFound, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.errors.length()").value(1), + jsonPath("$.errors[0].message").value("activity not found with id $nonexistentID") + ).andDocumentErrorResponse(documentation) + } + + @Test + fun `should fail if image does not exist`() { + val wrongImageUrl = + "${uploadConfigProperties.staticServe}/gallery/projects/Another${testProject.title}-$uuid.jpeg" + + mockMvc.multipartBuilder("/projects/${testProject.id}/gallery") + .asDeleteMethod() + .addPart("imageUrl", wrongImageUrl) + .perform() + .andExpectAll( + status().isNotFound, + content().contentType(MediaType.APPLICATION_JSON), + jsonPath("$.errors.length()").value(1), + jsonPath("$.errors[0].message").value("file not found with name $wrongImageUrl") + ).andDocumentErrorResponse(documentation) + } + } + fun Date?.toJson(): String { val quotedDate = objectMapper.writeValueAsString(this) // objectMapper adds quotes to the date, so remove them