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
29 import java.nio.charset.Charset
30 import kotlin.text.Charsets.UTF_8
33 * Base class for web page tests.
35 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
37 protected val currentSone = mock<Sone>()
38 protected val template = mock<Template>()
39 protected val webInterface = deepMock<WebInterface>()
40 protected val core = webInterface.core!!
41 private val eventBus = mock<EventBus>()
42 protected val preferences = Preferences(eventBus)
43 protected val l10n = webInterface.l10n!!
45 protected val page by lazy { pageSupplier(template, webInterface) }
46 private val httpRequest = mock<HTTPRequest>()
47 protected val freenetRequest = mock<FreenetRequest>()
48 protected val templateContext = TemplateContext()
50 protected val toadletContext = deepMock<ToadletContext>()
51 private val requestHeaders = mutableMapOf<String, String>()
52 private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
53 private val postRequestParameters = mutableMapOf<String, ByteArray>()
54 private val allSones = mutableMapOf<String, Sone>()
55 private val localSones = mutableMapOf<String, Sone>()
56 private val allPosts = mutableMapOf<String, Post>()
57 private val translations = mutableMapOf<String, String>()
61 whenever(core.preferences).thenReturn(preferences)
62 whenever(core.sones).then { allSones.values }
63 whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
64 whenever(core.localSones).then { localSones.values }
65 whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
66 whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
70 fun setupWebInterface() {
71 whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
72 whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
73 whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)
74 whenever(webInterface.getNotifications(currentSone)).thenReturn(emptyList())
78 fun setupHttpRequest() {
79 whenever(httpRequest.method).thenReturn("GET")
80 whenever(httpRequest.getHeader(anyString())).then { requestHeaders[it.get<String>(0).toLowerCase()] }
81 whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
82 whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
83 whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
84 whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
85 whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
86 whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
87 whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
88 whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
89 whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
90 whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
91 whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode()?.take(it[1]) ?: "" }
94 private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
97 fun setupFreenetRequest() {
98 whenever(freenetRequest.method).thenReturn(GET)
99 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
100 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
104 fun setupTranslations() {
105 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
108 fun setMethod(method: Method) {
109 whenever(httpRequest.method).thenReturn(method.name)
110 whenever(freenetRequest.method).thenReturn(method)
113 fun request(uri: String) {
114 whenever(httpRequest.path).thenReturn(uri)
115 whenever(freenetRequest.uri).thenReturn(URI(uri))
118 fun addHttpRequestHeader(name: String, value: String) {
119 requestHeaders[name.toLowerCase()] = value
122 fun addHttpRequestParameter(name: String, value: String) {
123 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
126 fun addHttpRequestPart(name: String, value: String) {
127 postRequestParameters[name] = value.toByteArray(UTF_8)
130 fun unsetCurrentSone() {
131 whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(null)
132 whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
133 whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(null)
136 fun addSone(id: String, sone: Sone) {
140 fun addLocalSone(id: String, localSone: Sone) {
141 localSones[id] = localSone
144 fun addPost(id: String, post: Post) {
148 fun addTranslation(key: String, value: String) {
149 translations[key] = value
152 fun verifyNoRedirect(assertions: () -> Unit) {
153 var caughtException: Exception? = null
155 page.handleRequest(freenetRequest, templateContext)
156 } catch (e: Exception) {
159 caughtException?.run { throw this } ?: assertions()
162 fun verifyRedirect(target: String, assertions: () -> Unit) {
164 page.handleRequest(freenetRequest, templateContext)
166 } catch (re: RedirectException) {
167 if (re.target != target) {
171 } catch (e: Exception) {