Add unit test for untrust page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2017 19:38:28 +0000 (20:38 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2017 19:38:28 +0000 (20:38 +0100)
src/test/kotlin/net/pterodactylus/sone/web/UntrustPageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/UntrustPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/UntrustPageTest.kt
new file mode 100644 (file)
index 0000000..166fd34
--- /dev/null
@@ -0,0 +1,58 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.util.web.Method.POST
+import org.junit.Test
+import org.mockito.ArgumentMatchers.any
+import org.mockito.ArgumentMatchers.eq
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [UntrustPage].
+ */
+class UntrustPageTest : WebPageTest() {
+
+       private val page = UntrustPage(template, webInterface)
+
+       override fun getPage() = page
+
+       @Test
+       fun `get request does not redirect`() {
+               page.handleRequest(freenetRequest, templateContext)
+               verify(core, never()).untrustSone(eq(currentSone), any())
+       }
+
+       @Test
+       fun `post request without sone parameter does not untrust but redirects`() {
+               request("", POST)
+               addHttpRequestParameter("returnPage", "return.html")
+               verifyRedirect("return.html") {
+                       verify(core, never()).untrustSone(eq(currentSone), any())
+               }
+       }
+
+       @Test
+       fun `post request with invalid sone parameter does not untrust but redirects`() {
+               request("", POST)
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("sone", "no-sone")
+               verifyRedirect("return.html") {
+                       verify(core, never()).untrustSone(eq(currentSone), any())
+               }
+       }
+
+       @Test
+       fun `post request with valid sone parameter untrusts and redirects`() {
+               request("", POST)
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("sone", "sone-id")
+               val sone = mock<Sone>()
+               addSone("sone-id", sone)
+               verifyRedirect("return.html") {
+                       verify(core).untrustSone(currentSone, sone)
+               }
+       }
+
+}