From: David ‘Bombe’ Roden Date: Sun, 18 Jun 2017 14:02:17 +0000 (+0200) Subject: Convert untrust page test to use new web page test base X-Git-Tag: 0.9.7^2~158 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=62524c0eda8cb1a57f35c4b9a34a6a6495ef4317;p=Sone.git Convert untrust page test to use new web page test base --- diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/UntrustPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/UntrustPageTest.kt index 19cfef3..1f5a31e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/UntrustPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/UntrustPageTest.kt @@ -14,11 +14,7 @@ import org.mockito.Mockito.verify /** * Unit test for [UntrustPage]. */ -class UntrustPageTest : WebPageTest() { - - private val page = UntrustPage(template, webInterface) - - override fun getPage() = page +class UntrustPageTest: WebPageTest2(::UntrustPage) { @Test fun `page returns correct path`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt index 40910fe..6cdff99 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt @@ -13,13 +13,20 @@ import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException import net.pterodactylus.util.template.Template import net.pterodactylus.util.template.TemplateContext +import net.pterodactylus.util.web.Method import net.pterodactylus.util.web.Method.GET +import org.junit.Assert.fail import org.junit.Before +import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyLong import org.mockito.ArgumentMatchers.anyString +import org.mockito.ArgumentMatchers.eq +import java.nio.charset.Charset +import kotlin.text.Charsets.UTF_8 /** * Base class for web page tests. @@ -41,6 +48,7 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla private val toadletContext = deepMock() private val getRequestParameters = mutableMapOf>() + private val postRequestParameters = mutableMapOf() private val allSones = mutableMapOf() private val allPosts = mutableMapOf() private val translations = mutableMapOf() @@ -54,6 +62,13 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla } @Before + fun setupWebInterface() { + whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone) + whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone) + whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone) + } + + @Before fun setupHttpRequest() { whenever(httpRequest.method).thenReturn("GET") whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() } @@ -66,8 +81,11 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] } whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray() } whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray() } + whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode() } } + private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset) + @Before fun setupFreenetRequest() { whenever(freenetRequest.method).thenReturn(GET) @@ -80,10 +98,19 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] } } + fun setMethod(method: Method) { + whenever(httpRequest.method).thenReturn(method.name) + whenever(freenetRequest.method).thenReturn(method) + } + fun addHttpRequestParameter(name: String, value: String) { getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf() }.apply { add(value) } } + fun addHttpRequestPart(name: String, value: String) { + postRequestParameters[name] = value.toByteArray(UTF_8) + } + fun addSone(id: String, sone: Sone) { allSones[id] = sone } @@ -106,4 +133,18 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla caughtException?.run { throw this } ?: assertions() } + fun verifyRedirect(target: String, assertions: () -> Unit) { + try { + page.handleRequest(freenetRequest, templateContext) + fail() + } catch (re: RedirectException) { + if (re.target != target) { + throw re + } + assertions() + } catch (e: Exception) { + throw e + } + } + }