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.util.template.Template
17 import net.pterodactylus.util.template.TemplateContext
18 import net.pterodactylus.util.web.Method.GET
19 import org.junit.Before
20 import org.mockito.ArgumentMatchers.anyInt
21 import org.mockito.ArgumentMatchers.anyLong
22 import org.mockito.ArgumentMatchers.anyString
25 * Base class for web page tests.
27 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
29 protected val currentSone = mock<Sone>()
30 private val template = mock<Template>()
31 private val webInterface = deepMock<WebInterface>()
32 protected val core = webInterface.core!!
33 private val eventBus = mock<EventBus>()
34 private val preferences = Preferences(eventBus)
35 private val l10n = webInterface.l10n!!
37 protected val page by lazy { pageSupplier(template, webInterface) }
38 private val httpRequest = mock<HTTPRequest>()
39 protected val freenetRequest = mock<FreenetRequest>()
40 protected val templateContext = TemplateContext()
42 private val toadletContext = deepMock<ToadletContext>()
43 private val getRequestParameters = mutableMapOf<String, MutableList<String>>()
44 private val allSones = mutableMapOf<String, Sone>()
45 private val allPosts = mutableMapOf<String, Post>()
46 private val translations = mutableMapOf<String, String>()
50 whenever(core.preferences).thenReturn(preferences)
51 whenever(core.sones).then { allSones.values }
52 whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
53 whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
57 fun setupHttpRequest() {
58 whenever(httpRequest.method).thenReturn("GET")
59 whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
60 whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
61 whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
62 whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
63 whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
64 whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
65 whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
66 whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
67 whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
68 whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
72 fun setupFreenetRequest() {
73 whenever(freenetRequest.method).thenReturn(GET)
74 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
75 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
79 fun setupTranslations() {
80 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
83 fun addHttpRequestParameter(name: String, value: String) {
84 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
87 fun addSone(id: String, sone: Sone) {
91 fun addPost(id: String, post: Post) {
95 fun addTranslation(key: String, value: String) {
96 translations[key] = value
99 fun verifyNoRedirect(assertions: () -> Unit) {
100 var caughtException: Exception? = null
102 page.handleRequest(freenetRequest, templateContext)
103 } catch (e: Exception) {
106 caughtException?.run { throw this } ?: assertions()