Commit 0a7b2d9d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

file upload didn't work

parent bb9cd9ac
......@@ -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 {
......
......@@ -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;
}
}
}
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);
}
......@@ -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());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment