Convert unlock Sone page test to use new web page test base
[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 localSones = mutableMapOf<String, Sone>()
54         private val allPosts = mutableMapOf<String, Post>()
55         private val translations = mutableMapOf<String, String>()
56
57         @Before
58         fun setupCore() {
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() }
65         }
66
67         @Before
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)
72         }
73
74         @Before
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]) ?: "" }
88         }
89
90         private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset)
91
92         @Before
93         fun setupFreenetRequest() {
94                 whenever(freenetRequest.method).thenReturn(GET)
95                 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
96                 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
97         }
98
99         @Before
100         fun setupTranslations() {
101                 whenever(l10n.getString(anyString())).then { translations[it[0]] ?: it[0] }
102         }
103
104         fun setMethod(method: Method) {
105                 whenever(httpRequest.method).thenReturn(method.name)
106                 whenever(freenetRequest.method).thenReturn(method)
107         }
108
109         fun addHttpRequestParameter(name: String, value: String) {
110                 getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
111         }
112
113         fun addHttpRequestPart(name: String, value: String) {
114                 postRequestParameters[name] = value.toByteArray(UTF_8)
115         }
116
117         fun addSone(id: String, sone: Sone) {
118                 allSones[id] = sone
119         }
120
121         fun addLocalSone(id: String, localSone: Sone) {
122                 localSones[id] = localSone
123         }
124
125         fun addPost(id: String, post: Post) {
126                 allPosts[id] = post
127         }
128
129         fun addTranslation(key: String, value: String) {
130                 translations[key] = value
131         }
132
133         fun verifyNoRedirect(assertions: () -> Unit) {
134                 var caughtException: Exception? = null
135                 try {
136                         page.handleRequest(freenetRequest, templateContext)
137                 } catch (e: Exception) {
138                         caughtException = e
139                 }
140                 caughtException?.run { throw this } ?: assertions()
141         }
142
143         fun verifyRedirect(target: String, assertions: () -> Unit) {
144                 try {
145                         page.handleRequest(freenetRequest, templateContext)
146                         fail()
147                 } catch (re: RedirectException) {
148                         if (re.target != target) {
149                                 throw re
150                         }
151                         assertions()
152                 } catch (e: Exception) {
153                         throw e
154                 }
155         }
156
157 }