6c4372d3ca7ab3673827a453acfcad44ec8b7f62
[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.net.URI
29 import java.nio.charset.Charset
30 import kotlin.text.Charsets.UTF_8
31
32 /**
33  * Base class for web page tests.
34  */
35 abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) {
36
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!!
44
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()
49
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>()
58
59         @Before
60         fun setupCore() {
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() }
67         }
68
69         @Before
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())
75         }
76
77         @Before
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]) ?: "" }
92         }
93
94         private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
95
96         @Before
97         fun setupFreenetRequest() {
98                 whenever(freenetRequest.method).thenReturn(GET)
99                 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
100                 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
101         }
102
103         @Before
104         fun setupTranslations() {
105                 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
106         }
107
108         fun setMethod(method: Method) {
109                 whenever(httpRequest.method).thenReturn(method.name)
110                 whenever(freenetRequest.method).thenReturn(method)
111         }
112
113         fun request(uri: String) {
114                 whenever(httpRequest.path).thenReturn(uri)
115                 whenever(freenetRequest.uri).thenReturn(URI(uri))
116         }
117
118         fun addHttpRequestHeader(name: String, value: String) {
119                 requestHeaders[name.toLowerCase()] = value
120         }
121
122         fun addHttpRequestParameter(name: String, value: String) {
123                 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
124         }
125
126         fun addHttpRequestPart(name: String, value: String) {
127                 postRequestParameters[name] = value.toByteArray(UTF_8)
128         }
129
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)
134         }
135
136         fun addSone(id: String, sone: Sone) {
137                 allSones[id] = sone
138         }
139
140         fun addLocalSone(id: String, localSone: Sone) {
141                 localSones[id] = localSone
142         }
143
144         fun addPost(id: String, post: Post) {
145                 allPosts[id] = post
146         }
147
148         fun addTranslation(key: String, value: String) {
149                 translations[key] = value
150         }
151
152         fun verifyNoRedirect(assertions: () -> Unit) {
153                 var caughtException: Exception? = null
154                 try {
155                         page.handleRequest(freenetRequest, templateContext)
156                 } catch (e: Exception) {
157                         caughtException = e
158                 }
159                 caughtException?.run { throw this } ?: assertions()
160         }
161
162         fun verifyRedirect(target: String, assertions: () -> Unit) {
163                 try {
164                         page.handleRequest(freenetRequest, templateContext)
165                         fail()
166                 } catch (re: RedirectException) {
167                         if (re.target != target) {
168                                 throw re
169                         }
170                         assertions()
171                 } catch (e: Exception) {
172                         throw e
173                 }
174         }
175
176 }