From d29bceb600fd3a0aad3ffb9ff36b6df06d127c89 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 22 Aug 2019 17:12:25 +0200
Subject: [PATCH] tests checking if undefined project is handled properly

---
 .../web/CommentControllerIntegrationTest.java | 141 ++++++++++++++++++
 1 file changed, 141 insertions(+)

diff --git a/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java b/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java
index 50e40f3869..f1f64a0406 100644
--- a/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java
+++ b/web/src/test/java/lcsb/mapviewer/web/CommentControllerIntegrationTest.java
@@ -95,6 +95,22 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
         .getAsJsonArray().size());
   }
 
+  @Test
+  public void testListOfCommentsWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    createComments();
+
+    RequestBuilder request = get("/projects/*/comments/models/" + map.getIdModel() + "/")
+        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+        .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testCuratorSeesAllComments() throws Exception {
     User curator = createCurator(TEST_CURATOR_LOGIN, TEST_CURATOR_PASSWORD);
@@ -354,6 +370,24 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
         .getAsJsonArray().size());
   }
 
+  @Test
+  public void testGetReactionCommentsWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    Comment comment = createReactionComment();
+    commentDao.add(comment);
+
+    RequestBuilder request = get(
+        "/projects/*/comments/models/" + map.getId() + "/bioEntities/reactions/" + reaction.getId())
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testGetReactionCommentsAsCurator() throws Exception {
     User curator = createCurator(TEST_CURATOR_LOGIN, TEST_CURATOR_PASSWORD);
@@ -419,6 +453,24 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
         .getAsJsonArray().size());
   }
 
+  @Test
+  public void testGetElementCommentsWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    Comment comment = createElementComment();
+    commentDao.add(comment);
+
+    RequestBuilder request = get(
+        "/projects/*/comments/models/" + map.getId() + "/bioEntities/elements/" + element.getId())
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testGetElementCommentsAsCurator() throws Exception {
     User curator = createCurator(TEST_CURATOR_LOGIN, TEST_CURATOR_PASSWORD);
@@ -484,6 +536,24 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
         .getAsJsonArray().size());
   }
 
+  @Test
+  public void testGetPointCommentsWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    Comment comment = createComment();
+    commentDao.add(comment);
+
+    RequestBuilder request = get(
+        "/projects/*/comments/models/" + map.getId() + "/points/10.00,20.00")
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testGetPointCommentsAsCurator() throws Exception {
     User curator = createCurator(TEST_CURATOR_LOGIN, TEST_CURATOR_PASSWORD);
@@ -552,6 +622,30 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
     assertEquals(1, commentDao.getCommentByModel(map, null, null).size());
   }
 
+  @Test
+  public void testAddElementCommentWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
+        new BasicNameValuePair("name", "test_user"),
+        new BasicNameValuePair("email", "a@a.lu"),
+        new BasicNameValuePair("content", "tes content"),
+        new BasicNameValuePair("pinned", "true"),
+        new BasicNameValuePair("coordinates", "10,2"),
+        new BasicNameValuePair("modelId", map.getId().toString()))));
+
+    RequestBuilder request = post(
+        "/projects/*/comments/models/" + map.getId() + "/bioEntities/elements/" + element.getId())
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .content(body)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testAddElementCommentWithoutAllNecessaryData() throws Exception {
     createUser(TEST_USER_LOGIN, TEST_USER_PASSWORD, map.getProject());
@@ -650,6 +744,30 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
     assertEquals(1, commentDao.getCommentByModel(map, null, null).size());
   }
 
+  @Test
+  public void testAddReactionCommentWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
+        new BasicNameValuePair("name", "test_user"),
+        new BasicNameValuePair("email", "a@a.lu"),
+        new BasicNameValuePair("content", "tes content"),
+        new BasicNameValuePair("pinned", "true"),
+        new BasicNameValuePair("coordinates", "10,2"),
+        new BasicNameValuePair("modelId", map.getId().toString()))));
+
+    RequestBuilder request = post(
+        "/projects/*/comments/models/" + map.getId() + "/bioEntities/reactions/" + reaction.getId())
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .content(body)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   @Test
   public void testAddPointCommentAsUserWithAccess() throws Exception {
     createUser(TEST_USER_LOGIN, TEST_USER_PASSWORD, map.getProject());
@@ -700,6 +818,29 @@ public class CommentControllerIntegrationTest extends ControllerIntegrationTest
     assertEquals(1, commentDao.getCommentByModel(map, null, null).size());
   }
 
+  @Test
+  public void testAddPointCommentWithUndefinedProject() throws Exception {
+    createAdmin(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
+
+    String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
+        new BasicNameValuePair("name", "test_user"),
+        new BasicNameValuePair("email", "a@a.lu"),
+        new BasicNameValuePair("content", "tes content"),
+        new BasicNameValuePair("pinned", "true"),
+        new BasicNameValuePair("modelId", map.getId().toString()))));
+
+    RequestBuilder request = post(
+        "/projects/*/comments/models/" + map.getId() + "/points/10,2")
+            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+            .content(body)
+            .session(session);
+
+    mockMvc.perform(request)
+        .andExpect(status().isNotFound());
+  }
+
   private Comment createComment() {
     Comment comment = new Comment();
     comment.setModel(map);
-- 
GitLab