From c22350cb3c7a26b235147e13da25735682b7f130 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 5 Oct 2017 07:09:21 +0200 Subject: [PATCH] Add unit test for JSON base page --- .../sone/web/ajax/JsonPageBaseTest.kt | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt new file mode 100644 index 0000000..fd1af27 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt @@ -0,0 +1,113 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.web.Response +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsString +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import java.io.ByteArrayOutputStream +import java.util.concurrent.atomic.AtomicInteger + +/** + * Unit test for [JsonPage]. + */ +class JsonPageBaseTest : TestObjects() { + + private var needsFormPassword = false + private val pageCallCounter = AtomicInteger() + private var pageResponse = { JsonReturnObject(true) } + private val outputStream = ByteArrayOutputStream() + private val response = Response(outputStream) + + private val page = object : JsonPage("path.html", webInterface) { + + override fun needsFormPassword() = this@JsonPageBaseTest.needsFormPassword + + override fun createJsonObject(request: FreenetRequest) = + pageResponse().also { pageCallCounter.incrementAndGet() } + + } + + @Before + fun setupWebInterface() { + whenever(webInterface.core).thenReturn(core) + } + + @Before + fun setupCore() { + whenever(core.preferences).thenReturn(Preferences(eventBus)) + } + + @Before + fun setupFreenetRequest() { + whenever(freenetRequest.toadletContext).thenReturn(toadletContext) + } + + @Test + fun `page returns 403 is full access is required but request is not full access`() { + core.preferences.isRequireFullAccess = true + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(403)) + } + + @Test + fun `page returns 403 if form password is needed but not supplied`() { + needsFormPassword = true + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(403)) + } + + @Test + fun `page returns 403 is form password is supplied but incorrect`() { + needsFormPassword = true + addRequestParameter("formPassword", formPassword + "_false") + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(403)) + } + + @Test + fun `page returns 200 if form password is required and correct`() { + needsFormPassword = true + addRequestParameter("formPassword", formPassword) + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(200)) + } + + @Test + fun `page returns 403 is login is required but current Sone is null`() { + unsetCurrentSone() + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(403)) + } + + @Test + fun `page returns content if login is required and current Sone is set`() { + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(200)) + assertThat(pageCallCounter.get(), equalTo(1)) + } + + @Test + fun `page returns 500 if execution throws exception`() { + pageResponse = { throw IllegalStateException() } + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(500)) + } + + @Test + fun `page returns stack trace if execution throws exception`() { + pageResponse = { throw IllegalStateException() } + page.handleRequest(freenetRequest, response) + assertThat(outputStream.toString(), containsString("IllegalStateException")) + } + + @Test + fun `json page is not a prefix page`() { + assertThat(page.isPrefixPage, equalTo(false)) + } + +} -- 2.7.4