Add unit test for like page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Nov 2016 13:57:36 +0000 (14:57 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Nov 2016 13:57:36 +0000 (14:57 +0100)
src/test/kotlin/net/pterodactylus/sone/web/LikePageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/LikePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/LikePageTest.kt
new file mode 100644 (file)
index 0000000..aafa8fa
--- /dev/null
@@ -0,0 +1,63 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Method.POST
+import org.junit.Test
+import org.mockito.Mockito.verify
+import org.mockito.Mockito.verifyNoMoreInteractions
+
+/**
+ * Unit test for [LikePage].
+ */
+class LikePageTest : WebPageTest() {
+
+       private val page = LikePage(template, webInterface)
+
+       @Test
+       fun `get request does not redirect`() {
+               request("", GET)
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with post id likes post and redirects to return page`() {
+               request("", POST)
+               addHttpRequestParameter("type", "post")
+               addHttpRequestParameter("post", "post-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(WebTestUtils.redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(currentSone).addLikedPostId("post-id")
+               }
+       }
+
+       @Test
+       fun `post request with reply id likes post and redirects to return page`() {
+               request("", POST)
+               addHttpRequestParameter("type", "reply")
+               addHttpRequestParameter("reply", "reply-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(WebTestUtils.redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(currentSone).addLikedReplyId("reply-id")
+               }
+       }
+
+       @Test
+       fun `post request with invalid likes redirects to return page`() {
+               request("", POST)
+               addHttpRequestParameter("type", "foo")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(WebTestUtils.redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verifyNoMoreInteractions(currentSone)
+               }
+       }
+
+}