Add unit test for delete post page and fix bug
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 18:30:10 +0000 (19:30 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 18:30:46 +0000 (19:30 +0100)
src/main/java/net/pterodactylus/sone/web/DeletePostPage.java
src/test/kotlin/net/pterodactylus/sone/web/DeletePostPageTest.kt [new file with mode: 0644]

index c2bae56..9079dce 100644 (file)
@@ -75,7 +75,7 @@ public class DeletePostPage extends SoneTemplatePage {
                        } else if (request.getHttpRequest().isPartSet("abortDelete")) {
                                throw new RedirectException(returnPage);
                        }
-                       templateContext.set("post", post);
+                       templateContext.set("post", post.get());
                        templateContext.set("returnPage", returnPage);
                }
        }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeletePostPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeletePostPageTest.kt
new file mode 100644 (file)
index 0000000..dd42f5e
--- /dev/null
@@ -0,0 +1,115 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Method.POST
+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
+import kotlin.test.fail
+
+/**
+ * Unit test for [DeletePostPage].
+ */
+class DeletePostPageTest : WebPageTest() {
+
+       private val page = DeletePostPage(template, webInterface)
+
+       private val post = mock<Post>()
+       private val sone = mock<Sone>()
+
+       @Before
+       fun setupPost() {
+               whenever(post.sone).thenReturn(sone)
+               whenever(sone.isLocal).thenReturn(true)
+       }
+
+       @Test
+       fun `get request with invalid post redirects to no permission page`() {
+               request("", GET)
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.processTemplate(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `get request with valid post sets post and return page in template context`() {
+               request("", GET)
+               addPost("post-id", post)
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["post"], equalTo<Any>(post))
+               assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
+       }
+
+       @Test
+       fun `post request with invalid post redirects to no permission page`() {
+               request("", POST)
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.processTemplate(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with post from non-local sone redirects to no permission page`() {
+               request("", POST)
+               whenever(sone.isLocal).thenReturn(false)
+               addPost("post-id", post)
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.processTemplate(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with confirmation deletes post and redirects to return page`() {
+               request("", POST)
+               addPost("post-id", post)
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("confirmDelete", "true")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.processTemplate(freenetRequest, templateContext)
+                       fail()
+               } catch (e: Exception) {
+                       verify(core).deletePost(post)
+                       throw e
+               }
+       }
+
+       @Test
+       fun `post request with abort delete does not delete post and redirects to return page`() {
+               request("", POST)
+               addPost("post-id", post)
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("abortDelete", "true")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.processTemplate(freenetRequest, templateContext)
+                       fail()
+               } catch (e: Exception) {
+                       verify(core, never()).deletePost(post)
+                       throw e
+               }
+       }
+
+       @Test
+       fun `post request without delete or abort sets post in template context`() {
+               request("", POST)
+               addPost("post-id", post)
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               page.processTemplate(freenetRequest, templateContext)
+               assertThat(templateContext["post"], equalTo<Any>(post))
+               assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
+       }
+
+}