Add unit test for follow sone page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 26 Nov 2016 21:58:21 +0000 (22:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 26 Nov 2016 21:58:21 +0000 (22:58 +0100)
src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt
new file mode 100644 (file)
index 0000000..8eee302
--- /dev/null
@@ -0,0 +1,78 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Method.POST
+import org.junit.Test
+import org.mockito.ArgumentMatchers
+import org.mockito.ArgumentMatchers.any
+import org.mockito.ArgumentMatchers.anyString
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [FollowSonePage].
+ */
+class FollowSonePageTest : WebPageTest() {
+
+       private val page = FollowSonePage(template, webInterface)
+
+       @Test
+       fun `get request does not redirect`() {
+               request("", GET)
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `a single sone can be followed`() {
+               request("", POST)
+               val sone = mock<Sone>()
+               addSone("sone-id", sone)
+               addHttpRequestParameter("sone", "sone-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core).followSone(currentSone, "sone-id")
+                       verify(core).markSoneKnown(sone)
+               }
+       }
+
+       @Test
+       fun `multiple sones can be followed`() {
+               request("", POST)
+               val firstSone = mock<Sone>()
+               addSone("sone-id1", firstSone)
+               val secondSone = mock<Sone>()
+               addSone("sone-id2", secondSone)
+               addHttpRequestParameter("sone", "sone-id1,sone-id2")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core).followSone(currentSone, "sone-id1")
+                       verify(core).followSone(currentSone, "sone-id2")
+                       verify(core).markSoneKnown(firstSone)
+                       verify(core).markSoneKnown(secondSone)
+               }
+       }
+
+       @Test
+       fun `a non-existing sone is not followed`() {
+               request("", POST)
+               addHttpRequestParameter("sone", "sone-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core, never()).followSone(ArgumentMatchers.eq(currentSone), anyString())
+                       verify(core, never()).markSoneKnown(any<Sone>())
+               }
+       }
+
+}