Add unit test for unlock sone page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2017 15:52:58 +0000 (16:52 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2017 15:52:58 +0000 (16:52 +0100)
src/test/kotlin/net/pterodactylus/sone/web/UnlockSonePageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/UnlockSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/UnlockSonePageTest.kt
new file mode 100644 (file)
index 0000000..4785156
--- /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.sone.test.whenever
+import org.junit.Test
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [UnlockSonePage].
+ */
+class UnlockSonePageTest : WebPageTest() {
+
+       private val page = UnlockSonePage(template, webInterface)
+
+       override fun getPage() = page
+
+       @Test
+       fun `get request without sone redirects to return page`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               verifyRedirect("return.html") {
+                       verify(core, never()).unlockSone(any())
+               }
+       }
+
+       @Test
+       fun `get request without invalid local sone does not unlock any sone and redirects to return page`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("sone", "invalid-sone")
+               verifyRedirect("return.html") {
+                       verify(core, never()).unlockSone(any())
+               }
+       }
+
+       @Test
+       fun `get request without remote sone does not unlock any sone and redirects to return page`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("sone", "remote-sone")
+               addSone("remote-sone", mock<Sone>())
+               verifyRedirect("return.html") {
+                       verify(core, never()).unlockSone(any())
+               }
+       }
+
+       @Test
+       fun `get request with local sone unlocks sone and redirects to return page`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("sone", "local-sone")
+               val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
+               addLocalSone("local-sone", sone)
+               verifyRedirect("return.html") {
+                       verify(core).unlockSone(sone)
+               }
+       }
+
+}