diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java index b736a1d..4ba7d0b 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/FileRepository.java @@ -1,5 +1,6 @@ package ovh.herisson.Clyde.Repositories; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import ovh.herisson.Clyde.Tables.StorageFile; @@ -9,4 +10,7 @@ import java.util.ArrayList; public interface FileRepository extends CrudRepository { public StorageFile getStorageFileByName(String name); + + @Query("select s from StorageFile s") + public ArrayList findAll(); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java index 24e53c0..79cce04 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/StorageService.java @@ -53,7 +53,7 @@ public class StorageService { String url = this.rootLocation.resolve(Paths.get(Objects.requireNonNull(stringUuid))) .normalize().toString(); - return fileRepo.save(new StorageFile(file.getName(),url, fileType)); + return fileRepo.save(new StorageFile(file.getOriginalFilename(),url, fileType)); } public void delete(StorageFile file) throws SecurityException { diff --git a/backend/src/test/java/ovh/herisson/Clyde/Endpoints/StorageControllerTest.java b/backend/src/test/java/ovh/herisson/Clyde/Endpoints/StorageControllerTest.java new file mode 100644 index 0000000..c9aa264 --- /dev/null +++ b/backend/src/test/java/ovh/herisson/Clyde/Endpoints/StorageControllerTest.java @@ -0,0 +1,103 @@ +package ovh.herisson.Clyde.Endpoints; + +import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.HostConfig; +import com.github.dockerjava.api.model.PortBinding; +import com.github.dockerjava.api.model.Ports; +import io.restassured.RestAssured; +import io.restassured.http.ContentType; +import org.junit.Assert; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import ovh.herisson.Clyde.ClydeApplication; +import ovh.herisson.Clyde.EndPoints.StorageController; +import ovh.herisson.Clyde.Repositories.FileRepository; +import ovh.herisson.Clyde.Services.StorageService; +import ovh.herisson.Clyde.Tables.FileType; +import ovh.herisson.Clyde.Tables.StorageFile; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +import static io.restassured.RestAssured.with; + +@Testcontainers +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class StorageControllerTest { + @LocalServerPort + private Integer port; + + @Autowired + private ClydeApplication controller; + + @Autowired + private StorageController storageController; + + @Autowired + private StorageService storageService; + @Autowired + private FileRepository fileRepository; + @Container + @ServiceConnection + static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:alpine") + .withDatabaseName("clyde") + .withUsername("devel") + .withPassword("devel") + .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(5432), new ExposedPort(5432))))); + + @BeforeAll + static void beforeAll(){ + postgres.start(); + } + + @AfterAll + static void afterAll(){ + postgres.stop(); + } + + @BeforeEach + void setup(){ + RestAssured.baseURI = "http://localhost:" + port; + } + + @AfterEach + void afterEach(){ + fileRepository.deleteAll(); + } + @Test + public void uploadFileTest() throws IOException { + File mmf = new File("test.txt"); + mmf.createNewFile(); + + FileWriter fw = new FileWriter(mmf); + fw.write("Ceci est un test"); + fw.close(); + + with().multiPart("file", mmf).pathParam("fileType", FileType.ProfilePicture).when().request("POST", "/upload/{fileType}").then().statusCode(200); + + StorageFile sf = fileRepository.getStorageFileByName("test.txt"); + + //On vérifie que le fichier a bien été stocké dans la db + Assert.assertFalse(sf == null); + + //On vérifie que le fichier a bel et bien été stocké dans le dossier cdn + File extFile = new File(sf.getUrl()); + Assert.assertTrue(extFile.exists()); + + Assert.assertEquals("cdn", extFile.getParent()); + + //On delete tranquillement les fichiers (la méthode delete a déja été testée dans StorageServiceTest) + storageService.delete(sf); + mmf.delete(); + } +}