X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FWebPageTest2.kt;h=b04fc4bcc83562f4c2dc57818ecdb6888de2a906;hb=298ee0947a7a05071583a62ffc2fb149a437b710;hp=6981f9f55cbd5a148adfbc763dfbca6681aba687;hpb=069bcf304238fb08fe7a5d5ed1d48d7d8e73ebc5;p=Sone.git 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 6981f9f..b04fc4b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt @@ -4,6 +4,7 @@ import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext import freenet.support.api.HTTPRequest import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.test.deepMock import net.pterodactylus.sone.test.get @@ -12,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. @@ -40,7 +48,10 @@ 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 localSones = mutableMapOf() + private val allPosts = mutableMapOf() private val translations = mutableMapOf() @Before @@ -48,6 +59,16 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla whenever(core.preferences).thenReturn(preferences) whenever(core.sones).then { allSones.values } whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() } + whenever(core.localSones).then { localSones.values } + whenever(core.getLocalSone(anyString())).then { localSones[it[0]] } + whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() } + } + + @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 @@ -63,8 +84,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()?.take(it[1]) ?: "" } } + private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset) + @Before fun setupFreenetRequest() { whenever(freenetRequest.method).thenReturn(GET) @@ -77,14 +101,31 @@ 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 } + fun addLocalSone(id: String, localSone: Sone) { + localSones[id] = localSone + } + + fun addPost(id: String, post: Post) { + allPosts[id] = post + } + fun addTranslation(key: String, value: String) { translations[key] = value } @@ -99,4 +140,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 + } + } + }