Add unit test for logout page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 19:37:02 +0000 (20:37 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 19:37:02 +0000 (20:37 +0100)
src/test/kotlin/net/pterodactylus/sone/web/LogoutPageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/LogoutPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/LogoutPageTest.kt
new file mode 100644 (file)
index 0000000..65b75e0
--- /dev/null
@@ -0,0 +1,59 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [LogoutPage].
+ */
+class LogoutPageTest : WebPageTest() {
+
+       private val page = LogoutPage(template, webInterface)
+
+       @Test
+       fun `page unsets current sone and redirects to index`() {
+               expectedException.expect(redirectsTo("index.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(webInterface).setCurrentSone(toadletContext, null)
+               }
+       }
+
+       @Test
+       fun `page is not enabled if sone requires full access and request does not have full access`() {
+               core.preferences.isRequireFullAccess = true
+               assertThat(page.isEnabled(toadletContext), equalTo(false))
+       }
+
+       @Test
+       fun `page is disabled if no sone is logged in`() {
+               unsetCurrentSone()
+               assertThat(page.isEnabled(toadletContext), equalTo(false))
+       }
+
+       @Test
+       fun `page is disabled if sone is logged in but there is only one sone`() {
+               whenever(core.localSones).thenReturn(listOf(currentSone))
+               assertThat(page.isEnabled(toadletContext), equalTo(false))
+       }
+
+       @Test
+       fun `page is enabled if sone is logged in and there is more than one sone`() {
+               whenever(core.localSones).thenReturn(listOf(currentSone, currentSone))
+               assertThat(page.isEnabled(toadletContext), equalTo(true))
+       }
+
+       @Test
+       fun `page is enabled if full access is required and present and sone is logged in and there is more than one sone`() {
+               core.preferences.isRequireFullAccess = true
+               whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
+               whenever(core.localSones).thenReturn(listOf(currentSone, currentSone))
+               assertThat(page.isEnabled(toadletContext), equalTo(true))
+       }
+
+}