Convert and complete unit test and fix bug in page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 17:43:20 +0000 (18:43 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 19:12:37 +0000 (20:12 +0100)
src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java
src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java [deleted file]
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt [new file with mode: 0644]

index 7112b73..aa8bc8f 100644 (file)
@@ -67,7 +67,7 @@ public class DeleteReplyPage extends SoneTemplatePage {
                                throw new RedirectException(returnPage);
                        }
                }
-               templateContext.set("reply", reply);
+               templateContext.set("reply", replyId);
                templateContext.set("returnPage", returnPage);
        }
 
diff --git a/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java b/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java
deleted file mode 100644 (file)
index 88c87a1..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package net.pterodactylus.sone.web;
-
-import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
-import static net.pterodactylus.util.web.Method.POST;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.when;
-
-import net.pterodactylus.sone.data.PostReply;
-
-import com.google.common.base.Optional;
-import org.junit.Test;
-
-/**
- * Unit test for {@link DeleteReplyPage}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class DeleteReplyPageTest extends WebPageTest {
-
-       private final DeleteReplyPage page = new DeleteReplyPage(template, webInterface);
-
-       @Test
-       public void tryingToDeleteAReplyWithAnInvalidIdResultsInNoPermissionPage() throws Exception {
-               request("", POST);
-               when(httpRequest.getPartAsStringFailsafe(eq("reply"), anyInt())).thenReturn("id");
-               when(webInterface.getCore().getPostReply("id")).thenReturn(Optional.<PostReply>absent());
-               expectedException.expect(redirectsTo("noPermission.html"));
-               page.processTemplate(freenetRequest, templateContext);
-       }
-
-}
index 16610e4..b38121f 100644 (file)
@@ -30,6 +30,7 @@ import net.pterodactylus.sone.core.UpdateChecker;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
 import net.pterodactylus.sone.data.TemporaryImage;
@@ -210,6 +211,10 @@ public abstract class WebPageTest {
                when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
        }
 
+       protected void addPostReply(String postReplyId, PostReply postReply) {
+               when(core.getPostReply(postReplyId)).thenReturn(Optional.fromNullable(postReply));
+       }
+
        protected void addSone(String soneId, Sone sone) {
                sones.put(soneId, sone);
        }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt
new file mode 100644 (file)
index 0000000..c2649d8
--- /dev/null
@@ -0,0 +1,108 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import net.pterodactylus.util.web.Method.POST
+import org.mockito.ArgumentMatchers.anyInt
+import org.mockito.ArgumentMatchers.eq
+import org.mockito.Mockito.`when`
+
+import net.pterodactylus.sone.data.PostReply
+
+import com.google.common.base.Optional
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.util.web.Method.GET
+import org.hamcrest.MatcherAssert
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Before
+import org.junit.Test
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [DeleteReplyPage].
+ */
+class DeleteReplyPageTest : WebPageTest() {
+
+       private val page = DeleteReplyPage(template, webInterface)
+
+       private val sone = mock<Sone>()
+       private val reply = mock<PostReply>()
+
+       @Before
+       fun setupReply() {
+               whenever(sone.isLocal).thenReturn(true)
+               whenever(reply.sone).thenReturn(sone)
+       }
+       
+       @Test
+       fun `get request sets reply ID and return page in template context`() {
+           request("", GET)
+               addHttpRequestParameter("reply", "reply-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
+               assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
+       }
+
+       @Test
+       fun `post request without any action sets reply ID and return page in template context`() {
+           request("", POST)
+               addPostReply("reply-id", reply)
+               addHttpRequestParameter("reply", "reply-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
+               assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
+       }
+
+       @Test
+       fun `trying to delete a reply with an invalid ID results in no permission page`() {
+               request("", POST)
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.processTemplate(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `trying to delete a reply from a non-local sone results in no permission page`() {
+           request("", POST)
+               addHttpRequestParameter("reply", "reply-id")
+               whenever(sone.isLocal).thenReturn(false)
+               addPostReply("reply-id", reply)
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.processTemplate(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `confirming deletion of reply deletes the reply and redirects to return page`() {
+           request("", POST)
+               addPostReply("reply-id", reply)
+               addHttpRequestParameter("reply", "reply-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("confirmDelete", "true")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.processTemplate(freenetRequest, templateContext)
+               } finally {
+                       verify(core).deleteReply(reply)
+               }
+       }
+       
+       @Test
+       fun `aborting deletion of reply redirects to return page`() {
+           request("", POST)
+               addPostReply("reply-id", reply)
+               addHttpRequestParameter("reply", "reply-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("abortDelete", "true")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.processTemplate(freenetRequest, templateContext)
+               } finally {
+                       verify(core, never()).deleteReply(reply)
+               }
+       }
+
+}