Commit ba100974 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

projectController tests merged

parent e98c5c66
Pipeline #45334 passed with stage
in 18 minutes and 46 seconds
......@@ -22,7 +22,6 @@ import org.junit.runners.Suite.SuiteClasses;
ParameterControllerIntegrationTest.class,
PluginControllerIntegrationTest.class,
ProjectControllerIntegrationTest.class,
ProjectControllerIntegrationTestForAsyncCalls.class,
PublicationsControllerIntegrationTest.class,
ReactionControllerIntegrationTest.class,
SpringSecurityGeneralIntegrationTest.class,
......
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.web;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
......@@ -65,8 +66,11 @@ import lcsb.mapviewer.web.serialization.ProjectBackgroundUpdateMixIn;
@RunWith(SpringJUnit4ClassRunner.class)
public class ProjectControllerIntegrationTest extends ControllerIntegrationTest {
private static final String CURATOR_PASSWORD = "test_pass";
private static final String CURATOR_LOGIN = "test_user";
private static final String CURATOR_PASSWORD = "curator_pass";
private static final String CURATOR_LOGIN = "curator_user";
private static final String USER_PASSWORD = "user_pass";
private static final String USER_LOGIN = "test_user";
private static final String TEST_PROJECT_2 = "test_project2";
Logger logger = LogManager.getLogger();
......@@ -92,6 +96,7 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
@After
public void tearDown() throws Exception {
removeUserInSeparateThread(curator);
removeUserInSeparateThread(userService.getUserByLogin(USER_LOGIN));
removeProjectInSeparateThread(TEST_PROJECT);
removeProjectInSeparateThread(TEST_PROJECT_2);
}
......@@ -1129,4 +1134,144 @@ public class ProjectControllerIntegrationTest extends ControllerIntegrationTest
return projectPathParameters().and(parameterWithName("backgroundId").description("background identifier"));
}
private void waitForProjectToFinishRemoving(String projectId) throws InterruptedException {
while (projectService.getProjectByProjectId(projectId) != null)
;
}
@Test(timeout = 10000)
public void checkDefaultPrivilegesAfterAddingProject() throws Exception {
User user = createCurator(USER_LOGIN, USER_PASSWORD);
grantPrivilegeInSeparateThread(user, PrivilegeType.READ_PROJECT.name() + ":*");
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
waitForProjectToFinishLoading(TEST_PROJECT);
User u = userService.getUserByLogin(USER_LOGIN, true);
User c = userService.getUserByLogin(CURATOR_LOGIN, true);
assertTrue("User privileges weren't updated after project was created",
u.getPrivileges().contains(new Privilege(PrivilegeType.READ_PROJECT, TEST_PROJECT)));
assertTrue("Curator privileges weren't updated after project was created",
c.getPrivileges().contains(new Privilege(PrivilegeType.WRITE_PROJECT, TEST_PROJECT)));
assertTrue("Curator privileges weren't updated after project was created",
c.getPrivileges().contains(new Privilege(PrivilegeType.READ_PROJECT, TEST_PROJECT)));
}
@Test(timeout = 10000)
public void checkDropPrivilegesAfterRemovingProject() throws Exception {
createProjectInSeparateThread(TEST_PROJECT);
grantPrivilegeInSeparateThread(curator, PrivilegeType.WRITE_PROJECT.name() + ":" + TEST_PROJECT);
RequestBuilder request = delete("/api/projects/" + TEST_PROJECT)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
waitForProjectToFinishRemoving(TEST_PROJECT);
User c = userService.getUserByLogin(CURATOR_LOGIN, true);
assertFalse("Curator privileges weren't updated after project was removed",
c.getPrivileges().contains(new Privilege(PrivilegeType.WRITE_PROJECT, TEST_PROJECT)));
}
private void grantPrivilegeInSeparateThread(User user, String string) throws Exception {
callInSeparateThread(() -> {
userService.grantUserPrivilege(user, PrivilegeType.valueOf(string.split(":")[0]), string.split(":")[1]);
return null;
});
}
@Test(timeout = 10000)
public void addProjectWithInvalidId() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN);
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
admin);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/*")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD));
mockMvc.perform(request).andExpect(status().is4xxClientError());
assertNull(projectService.getProjectByProjectId("*"));
}
@Test(timeout = 10000)
public void addProjectWithoutPrivileges() throws Exception {
User user = createUser(USER_LOGIN, USER_PASSWORD);
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
user);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(USER_LOGIN, USER_PASSWORD));
mockMvc.perform(request).andExpect(status().isForbidden());
assertNull(projectService.getProjectByProjectId(TEST_PROJECT));
}
@Test
public void testUserPrivilegeChangeDuringUpload() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN);
Project defaultProject = createProjectInSeparateThread(TEST_PROJECT_2);
grantPrivilegeInSeparateThread(admin, PrivilegeType.READ_PROJECT.name() + ":" + defaultProject.getProjectId());
grantPrivilegeInSeparateThread(admin, PrivilegeType.WRITE_PROJECT.name() + ":" + defaultProject.getProjectId());
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
waitForProjectToFinishLoading(TEST_PROJECT);
userService.revokeObjectDomainPrivilegesForAllUsers(PrivilegeType.READ_PROJECT,
defaultProject.getProjectId());
Project project = projectService.getProjectByProjectId(TEST_PROJECT);
assertEquals(ProjectStatus.DONE, project.getStatus());
}
}
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.file.Files;
import java.nio.file.Paths;
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.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.ProjectStatus;
import lcsb.mapviewer.model.cache.UploadedFileEntry;
import lcsb.mapviewer.model.security.Privilege;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerIntegrationTest {
private static final String CURATOR_PASSWORD = "curator_pass";
private static final String CURATOR_LOGIN = "test_curator";
private static final String USER_PASSWORD = "user_pass";
private static final String USER_LOGIN = "test_user";
private static String TEST_PROJECT_2 = "test_id2";
Logger logger = LogManager.getLogger();
@Autowired
private IProjectService projectService;
@Autowired
private IUserService userService;
@Before
public void setup() throws Exception {
}
@After
public void tearDown() throws Exception {
removeProjectInSeparateThread(TEST_PROJECT);
}
@Test(timeout = 10000)
public void addProjectAsCurator() throws Exception {
User curator = createCuratorInSeparateThread(CURATOR_LOGIN, CURATOR_PASSWORD);
try {
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
callInSeparateThread(() -> {
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
} catch (Exception e) {
e.printStackTrace();
}
return null;
});
waitForProjectToFinishLoading(TEST_PROJECT);
} finally {
removeUserInSeparateThread(curator);
}
}
private void waitForProjectToFinishRemoving(String projectId) throws InterruptedException {
while (projectService.getProjectByProjectId(projectId) != null)
;
}
@Test(timeout = 10000)
public void checkDefaultPrivilegesAfterAddingProject() throws Exception {
User curator = createCuratorInSeparateThread(CURATOR_LOGIN, CURATOR_PASSWORD);
User user = createCuratorInSeparateThread(USER_LOGIN, USER_PASSWORD);
grantPrivilegeInSeparateThread(user, PrivilegeType.READ_PROJECT.name() + ":*");
try {
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
callInSeparateThread(() -> {
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
} catch (Exception e) {
e.printStackTrace();
}
return null;
});
waitForProjectToFinishLoading(TEST_PROJECT);
callInSeparateThread(() -> {
User u = userService.getUserByLogin(USER_LOGIN);
User c = userService.getUserByLogin(CURATOR_LOGIN);
assertTrue("User privileges weren't updated after project was created",
u.getPrivileges().contains(new Privilege(PrivilegeType.READ_PROJECT, TEST_PROJECT)));
assertTrue("Curator privileges weren't updated after project was created",
c.getPrivileges().contains(new Privilege(PrivilegeType.WRITE_PROJECT, TEST_PROJECT)));
assertTrue("Curator privileges weren't updated after project was created",
c.getPrivileges().contains(new Privilege(PrivilegeType.READ_PROJECT, TEST_PROJECT)));
return null;
});
} finally {
removeUserInSeparateThread(curator);
removeUserInSeparateThread(user);
}
}
@Test(timeout = 10000)
public void checkDropPrivilegesAfterRemovingProject() throws Exception {
createProjectInSeparateThread(TEST_PROJECT);
User curator = createCuratorInSeparateThread(CURATOR_LOGIN, CURATOR_PASSWORD);
grantPrivilegeInSeparateThread(curator, PrivilegeType.WRITE_PROJECT.name() + ":" + TEST_PROJECT);
try {
RequestBuilder request = delete("/api/projects/" + TEST_PROJECT)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
waitForProjectToFinishRemoving(TEST_PROJECT);
callInSeparateThread(() -> {
User c = userService.getUserByLogin(CURATOR_LOGIN);
assertFalse("Curator privileges weren't updated after project was removed",
c.getPrivileges().contains(new Privilege(PrivilegeType.WRITE_PROJECT, TEST_PROJECT)));
return null;
});
} finally {
removeUserInSeparateThread(curator);
}
}
private void grantPrivilegeInSeparateThread(User user, String string) throws Exception {
callInSeparateThread(() -> {
userService.grantUserPrivilege(user, PrivilegeType.valueOf(string.split(":")[0]), string.split(":")[1]);
return null;
});
}
@Test(timeout = 10000)
public void addProjectWithInvalidId() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN);
try {
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
admin);
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/*")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD));
mockMvc.perform(request).andExpect(status().is4xxClientError());
} catch (Exception e) {
e.printStackTrace();
}
assertNull(projectService.getProjectByProjectId("*"));
} finally {
waitForProjectToFinishLoading("*");
removeProjectInSeparateThread("*");
}
}
@Test(timeout = 10000)
public void addProjectWithoutPrivileges() throws Exception {
User curator = createUserInSeparateThread(CURATOR_LOGIN, CURATOR_PASSWORD);
try {
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
callInSeparateThread(() -> {
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().isForbidden());
} catch (Exception e) {
e.printStackTrace();
}
return null;
});
assertNull(projectService.getProjectByProjectId(TEST_PROJECT));
} finally {
removeUserInSeparateThread(curator);
}
}
@Test
public void testUserPrivilegeChangeDuringUpload() throws Exception {
User admin = userService.getUserByLogin(BUILT_IN_TEST_ADMIN_LOGIN);
User curator = createAdminInSeparateThread(CURATOR_LOGIN, CURATOR_PASSWORD);
try {
Project defaultProject = createProjectInSeparateThread(TEST_PROJECT_2);
grantPrivilegeInSeparateThread(admin, PrivilegeType.READ_PROJECT.name() + ":" + defaultProject.getProjectId());
grantPrivilegeInSeparateThread(admin, PrivilegeType.WRITE_PROJECT.name() + ":" + defaultProject.getProjectId());
UploadedFileEntry fileEntry = createFileInSeparateThread(
new String(Files.readAllBytes(Paths.get("./src/test/resources/generic.xml")), "UTF-8"),
curator);
Thread addProjectThread = new Thread(new Runnable() {
@Override
public void run() {
try {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("file-id", String.valueOf(fileEntry.getId())),
new BasicNameValuePair("mapCanvasType", "OPEN_LAYERS"),
new BasicNameValuePair("parser",
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser"))));
RequestBuilder request = post("/api/projects/" + TEST_PROJECT)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(createSession(CURATOR_LOGIN, CURATOR_PASSWORD));
mockMvc.perform(request).andExpect(status().is2xxSuccessful());
waitForProjectToFinishLoading(TEST_PROJECT);
} catch (Exception e) {
e.printStackTrace();
}
}
});
addProjectThread.start();
Thread dropPrivilegesThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(600);
userService.revokeObjectDomainPrivilegesForAllUsers(PrivilegeType.READ_PROJECT,
defaultProject.getProjectId());
} catch (Exception e) {
e.printStackTrace();
}
}
});
dropPrivilegesThread.start();
dropPrivilegesThread.join();
addProjectThread.join();
Project project = projectService.getProjectByProjectId(TEST_PROJECT);
assertEquals(ProjectStatus.DONE, project.getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
removeUserInSeparateThread(curator);
removeProjectInSeparateThread(TEST_PROJECT_2);
removeProjectInSeparateThread(TEST_PROJECT);
}
}
private User createCuratorInSeparateThread(String login, String password) throws Exception {
return callInSeparateThread(() -> {
return createCurator(login, password);
});
}
private User createAdminInSeparateThread(String login, String password) throws Exception {
return callInSeparateThread(() -> {
return createAdmin(login, password);
});
}
}
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