From 0a7b2d9d7038c15c3d01e5553fc109d4a696cc48 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Thu, 25 Jul 2019 18:03:04 +0200 Subject: [PATCH] file upload didn't work --- .../mapviewer/api/files/FileController.java | 2 +- .../mapviewer/services/impl/FileService.java | 12 +++++++ .../services/interfaces/IFileService.java | 3 ++ ...llerIntegrationTestWithoutTransaction.java | 36 +++++++++++++++++-- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/files/FileController.java b/rest-api/src/main/java/lcsb/mapviewer/api/files/FileController.java index fee30ce281..4b4a99b8a0 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/files/FileController.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/files/FileController.java @@ -40,7 +40,7 @@ public class FileController extends BaseController { return fileRest.getFile(id); } - @PreAuthorize("@fileService.getById(#id)?.owner?.login == authentication.name") + @PreAuthorize("@fileService.getOwnerByFileId(#id)?.login == authentication.name") @PostMapping(value = "/{id}:uploadContent") public Map<String, Object> uploadContent(@PathVariable(value = "id") String id, @RequestBody byte[] data) throws ObjectNotFoundException { diff --git a/service/src/main/java/lcsb/mapviewer/services/impl/FileService.java b/service/src/main/java/lcsb/mapviewer/services/impl/FileService.java index 8519510c35..97ff42bdc8 100644 --- a/service/src/main/java/lcsb/mapviewer/services/impl/FileService.java +++ b/service/src/main/java/lcsb/mapviewer/services/impl/FileService.java @@ -5,6 +5,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import lcsb.mapviewer.model.cache.UploadedFileEntry; +import lcsb.mapviewer.model.user.User; import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao; import lcsb.mapviewer.services.interfaces.IFileService; @@ -24,4 +25,15 @@ public class FileService implements IFileService { return uploadedFileEntryDao.getById(id); } + @Override + public User getOwnerByFileId(int id) { + UploadedFileEntry entry = uploadedFileEntryDao.getById(id); + if (entry != null) { + uploadedFileEntryDao.getById(id).getOwner().getLogin(); + return uploadedFileEntryDao.getById(id).getOwner(); + } else { + return null; + } + } + } diff --git a/service/src/main/java/lcsb/mapviewer/services/interfaces/IFileService.java b/service/src/main/java/lcsb/mapviewer/services/interfaces/IFileService.java index 809e597a35..4ea6298077 100644 --- a/service/src/main/java/lcsb/mapviewer/services/interfaces/IFileService.java +++ b/service/src/main/java/lcsb/mapviewer/services/interfaces/IFileService.java @@ -1,9 +1,12 @@ package lcsb.mapviewer.services.interfaces; import lcsb.mapviewer.model.cache.UploadedFileEntry; +import lcsb.mapviewer.model.user.User; public interface IFileService { UploadedFileEntry getById(int id); + User getOwnerByFileId(int id); + } diff --git a/web/src/test/java/lcsb/mapviewer/web/FileControllerIntegrationTestWithoutTransaction.java b/web/src/test/java/lcsb/mapviewer/web/FileControllerIntegrationTestWithoutTransaction.java index 9d32a6cf18..2b351c9093 100644 --- a/web/src/test/java/lcsb/mapviewer/web/FileControllerIntegrationTestWithoutTransaction.java +++ b/web/src/test/java/lcsb/mapviewer/web/FileControllerIntegrationTestWithoutTransaction.java @@ -8,18 +8,23 @@ import java.util.Arrays; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; -import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.RequestBuilder; +import com.google.gson.JsonParser; + @RunWith(SpringJUnit4ClassRunner.class) -@Rollback public class FileControllerIntegrationTestWithoutTransaction extends ControllerIntegrationTest { + Logger logger = LogManager.getLogger(); + private static final String BUILT_IN_TEST_ADMIN_PASSWORD = "admin"; private static final String BUILT_IN_TEST_ADMIN_LOGIN = "admin"; @@ -42,4 +47,31 @@ public class FileControllerIntegrationTestWithoutTransaction extends ControllerI mockMvc.perform(request) .andExpect(status().is4xxClientError()); } + + @Test + public void createAndAppendToExistingFile() throws Exception { + MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD); + + String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList( + new BasicNameValuePair("filename", "unknown.txt"), + new BasicNameValuePair("length", "29")))); + + RequestBuilder request = post("/files/") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .content(body) + .session(session); + + String response = mockMvc.perform(request) + .andExpect(status().is2xxSuccessful()).andReturn().getResponse().getContentAsString(); + int id = new JsonParser().parse(response).getAsJsonObject().get("id").getAsInt(); + + String fileContent = "elementIdentifier\tvalue\nxx\t-1"; + + RequestBuilder postContentRequest = post("/files/" + id + ":uploadContent") + .content(fileContent) + .session(session); + + mockMvc.perform(postContentRequest) + .andExpect(status().is2xxSuccessful()); + } } -- GitLab