6cdff99916b8ecac477a98c2ef1ce6fafcd02f64
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / WebPageTest2.kt
1 package net.pterodactylus.sone.web.pages
2
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
30
31 /**
32  * Base class for web page tests.
33  */
34 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
35
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!!
43
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()
48
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 allPosts = mutableMapOf<String, Post>()
54         private val translations = mutableMapOf<String, String>()
55
56         @Before
57         fun setupCore() {
58                 whenever(core.preferences).thenReturn(preferences)
59                 whenever(core.sones).then { allSones.values }
60                 whenever(core.getSone(anyString())).then { allSones[it[0]].asOptional() }
61                 whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
62         }
63
64         @Before
65         fun setupWebInterface() {
66                 whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone)
67                 whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
68                 whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone)
69         }
70
71         @Before
72         fun setupHttpRequest() {
73                 whenever(httpRequest.method).thenReturn("GET")
74                 whenever(httpRequest.hasParameters()).then { getRequestParameters.isNotEmpty() }
75                 whenever(httpRequest.parameterNames).then { getRequestParameters.keys }
76                 whenever(httpRequest.isParameterSet(anyString())).then { it[0] in getRequestParameters }
77                 whenever(httpRequest.getParam(anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: "" }
78                 whenever(httpRequest.getParam(anyString(), anyString())).then { getRequestParameters[it[0]]?.firstOrNull() ?: it[1] }
79                 whenever(httpRequest.getIntParam(anyString())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: 0 }
80                 whenever(httpRequest.getIntParam(anyString(), anyInt())).then { getRequestParameters[it[0]]?.first()?.toIntOrNull() ?: it[1] }
81                 whenever(httpRequest.getLongParam(anyString(), anyLong())).then { getRequestParameters[it[0]]?.first()?.toLongOrNull() ?: it[1] }
82                 whenever(httpRequest.getMultipleParam(anyString())).then { getRequestParameters[it[0]]?.toTypedArray() ?: emptyArray<String>() }
83                 whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray<Int>() }
84                 whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode() }
85         }
86
87         private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
88
89         @Before
90         fun setupFreenetRequest() {
91                 whenever(freenetRequest.method).thenReturn(GET)
92                 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
93                 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
94         }
95
96         @Before
97         fun setupTranslations() {
98                 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
99         }
100
101         fun setMethod(method: Method) {
102                 whenever(httpRequest.method).thenReturn(method.name)
103                 whenever(freenetRequest.method).thenReturn(method)
104         }
105
106         fun addHttpRequestParameter(name: String, value: String) {
107                 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
108         }
109
110         fun addHttpRequestPart(name: String, value: String) {
111                 postRequestParameters[name] = value.toByteArray(UTF_8)
112         }
113
114         fun addSone(id: String, sone: Sone) {
115                 allSones[id] = sone
116         }
117
118         fun addPost(id: String, post: Post) {
119                 allPosts[id] = post
120         }
121
122         fun addTranslation(key: String, value: String) {
123                 translations[key] = value
124         }
125
126         fun verifyNoRedirect(assertions: () -> Unit) {
127                 var caughtException: Exception? = null
128                 try {
129                         page.handleRequest(freenetRequest, templateContext)
130                 } catch (e: Exception) {
131                         caughtException = e
132                 }
133                 caughtException?.run { throw this } ?: assertions()
134         }
135
136         fun verifyRedirect(target: String, assertions: () -> Unit) {
137                 try {
138                         page.handleRequest(freenetRequest, templateContext)
139                         fail()
140                 } catch (re: RedirectException) {
141                         if (re.target != target) {
142                                 throw re
143                         }
144                         assertions()
145                 } catch (e: Exception) {
146                         throw e
147                 }
148         }
149
150 }