1 package net.pterodactylus.sone.web.pages
3 import com.google.common.eventbus.EventBus
4 import freenet.clients.http.ToadletContext
5 import freenet.support.api.HTTPRequest
6 import net.pterodactylus.sone.core.Preferences
7 import net.pterodactylus.sone.data.Post
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.test.deepMock
10 import net.pterodactylus.sone.test.get
11 import net.pterodactylus.sone.test.mock
12 import net.pterodactylus.sone.test.whenever
13 import net.pterodactylus.sone.utils.asOptional
14 import net.pterodactylus.sone.web.WebInterface
15 import net.pterodactylus.sone.web.page.FreenetRequest
16 import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException
17 import net.pterodactylus.util.template.Template
18 import net.pterodactylus.util.template.TemplateContext
19 import net.pterodactylus.util.web.Method
20 import net.pterodactylus.util.web.Method.GET
21 import org.junit.Assert.fail
22 import org.junit.Before
23 import org.mockito.ArgumentMatchers.anyBoolean
24 import org.mockito.ArgumentMatchers.anyInt
25 import org.mockito.ArgumentMatchers.anyLong
26 import org.mockito.ArgumentMatchers.anyString
27 import org.mockito.ArgumentMatchers.eq
28 import java.nio.charset.Charset
29 import kotlin.text.Charsets.UTF_8
32 * Base class for web page tests.
34 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
36 protected val currentSone = mock<Sone>()
37 private val template = mock<Template>()
38 private val webInterface = deepMock<WebInterface>()
39 protected val core = webInterface.core!!
40 private val eventBus = mock<EventBus>()
41 private val preferences = Preferences(eventBus)
42 private val l10n = webInterface.l10n!!
44 protected val page by lazy { pageSupplier(template, webInterface) }
45 private val httpRequest = mock<HTTPRequest>()
46 protected val freenetRequest = mock<FreenetRequest>()
47 protected val templateContext = TemplateContext()
49 private val toadletContext = deepMock<ToadletContext>()
50 private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
51 private val postRequestParameters = mutableMapOf<String, ByteArray>()
52 private val allSones = mutableMapOf<String, Sone>()
53 private val localSones = mutableMapOf<String, Sone>()
54 private val allPosts = mutableMapOf<String, Post>()
55 private val translations = mutableMapOf<String, String>()
59 whenever(core.preferences).thenReturn(preferences)
60 whenever(core.sones).then { allSones.values }
61 whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
62 whenever(core.localSones).then { localSones.values }
63 whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
64 whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
68 fun setupWebInterface() {
69 whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
70 whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
71 whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)
75 fun setupHttpRequest() {
76 whenever(httpRequest.method).thenReturn("GET")
77 whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
78 whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
79 whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
80 whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
81 whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
82 whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
83 whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
84 whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
85 whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
86 whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
87 whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode()?.take(it[1]) ?: "" }
90 private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
93 fun setupFreenetRequest() {
94 whenever(freenetRequest.method).thenReturn(GET)
95 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
96 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
100 fun setupTranslations() {
101 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
104 fun setMethod(method: Method) {
105 whenever(httpRequest.method).thenReturn(method.name)
106 whenever(freenetRequest.method).thenReturn(method)
109 fun addHttpRequestParameter(name: String, value: String) {
110 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
113 fun addHttpRequestPart(name: String, value: String) {
114 postRequestParameters[name] = value.toByteArray(UTF_8)
117 fun addSone(id: String, sone: Sone) {
121 fun addLocalSone(id: String, localSone: Sone) {
122 localSones[id] = localSone
125 fun addPost(id: String, post: Post) {
129 fun addTranslation(key: String, value: String) {
130 translations[key] = value
133 fun verifyNoRedirect(assertions: () -> Unit) {
134 var caughtException: Exception? = null
136 page.handleRequest(freenetRequest, templateContext)
137 } catch (e: Exception) {
140 caughtException?.run { throw this } ?: assertions()
143 fun verifyRedirect(target: String, assertions: () -> Unit) {
145 page.handleRequest(freenetRequest, templateContext)
147 } catch (re: RedirectException) {
148 if (re.target != target) {
152 } catch (e: Exception) {