Clean up SoneTemplatePage’s constructors
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / SoneTemplatePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.main.SonePlugin
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.notify.Notification
9 import net.pterodactylus.util.template.TemplateContext
10 import net.pterodactylus.util.version.Version
11 import org.hamcrest.Matcher
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.anyOf
14 import org.hamcrest.Matchers.contains
15 import org.hamcrest.Matchers.containsInAnyOrder
16 import org.hamcrest.Matchers.equalTo
17 import org.hamcrest.Matchers.nullValue
18 import org.junit.Test
19
20 /**
21  * Unit test for [SoneTemplatePage].
22  */
23 class SoneTemplatePageTest : WebPageTest({ template, webInterface -> object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {} }) {
24
25         @Test
26         fun `page title is empty string if no page title key was given`() {
27                 SoneTemplatePage("path.html", webInterface, template, requiresLogin = false).let { page ->
28                         assertThat(page.getPageTitle(freenetRequest), equalTo(""))
29                 }
30         }
31
32         @Test
33         fun `page title is retrieved from l10n if page title key is given`() {
34                 SoneTemplatePage("path.html", webInterface, template, "page.title", false).let { page ->
35                         whenever(l10n.getString("page.title")).thenReturn("Page Title")
36                         assertThat(page.getPageTitle(freenetRequest), equalTo("Page Title"))
37                 }
38         }
39
40         @Test
41         fun `additional link nodes contain open search link`() {
42                 addHttpRequestHeader("Host", "www.example.com")
43                 assertThat(page.getAdditionalLinkNodes(freenetRequest), contains(mapOf(
44                                 "rel" to "search",
45                                 "type" to "application/opensearchdescription+xml",
46                                 "title" to "Sone",
47                                 "href" to "http://www.example.com/Sone/OpenSearch.xml"
48                 )))
49         }
50
51         @Test
52         fun `style sheets contains sone CSS file`() {
53                 assertThat(page.styleSheets, contains("css/sone.css"))
54         }
55
56         @Test
57         fun `shortcut icon is the sone icon`() {
58                 assertThat(page.shortcutIcon, equalTo("images/icon.png"))
59         }
60
61         private fun verifyVariableIsSet(name: String, value: Any) = verifyVariableMatches(name, equalTo<Any>(value))
62
63         private fun <T> verifyVariableMatches(name: String, matcher: Matcher<T>) {
64                 page.processTemplate(freenetRequest, templateContext)
65                 @Suppress("UNCHECKED_CAST")
66                 assertThat(templateContext[name] as T, matcher)
67         }
68
69         @Test
70         fun `preferences are set in template context`() {
71                 verifyVariableIsSet("preferences", preferences)
72         }
73
74         @Test
75         fun `current sone is set in template context`() {
76                 verifyVariableIsSet("currentSone", currentSone)
77         }
78
79         @Test
80         fun `local sones are set in template context`() {
81                 val localSones = listOf(mock<Sone>(), mock<Sone>())
82                 whenever(core.localSones).thenReturn(localSones)
83                 verifyVariableMatches("localSones", containsInAnyOrder(*localSones.toTypedArray()))
84         }
85
86         @Test
87         fun `freenet request is set in template context`() {
88                 verifyVariableIsSet("request", freenetRequest)
89         }
90
91         @Test
92         fun `current version is set in template context`() {
93                 verifyVariableIsSet("currentVersion", SonePlugin.getPluginVersion())
94         }
95
96         @Test
97         fun `has latest version is set correctly in template context if true`() {
98                 whenever(core.updateChecker.hasLatestVersion()).thenReturn(true)
99                 verifyVariableIsSet("hasLatestVersion", true)
100         }
101
102         @Test
103         fun `has latest version is set correctly in template context if false`() {
104                 whenever(core.updateChecker.hasLatestVersion()).thenReturn(false)
105                 verifyVariableIsSet("hasLatestVersion", false)
106         }
107
108         @Test
109         fun `latest edition is set in template context`() {
110                 whenever(core.updateChecker.latestEdition).thenReturn(1234L)
111                 verifyVariableIsSet("latestEdition", 1234L)
112         }
113
114         @Test
115         fun `latest version is set in template context`() {
116                 whenever(core.updateChecker.latestVersion).thenReturn(Version(1, 2, 3))
117                 verifyVariableIsSet("latestVersion", Version(1, 2, 3))
118         }
119
120         @Test
121         fun `latest version time is set in template context`() {
122                 whenever(core.updateChecker.latestVersionDate).thenReturn(12345L)
123                 verifyVariableIsSet("latestVersionTime", 12345L)
124         }
125
126         private fun createNotification(time: Long) = mock<Notification>().apply {
127                 whenever(createdTime).thenReturn(time)
128         }
129
130         @Test
131         fun `notifications are set in template context`() {
132                 val notifications = listOf(createNotification(3000), createNotification(1000), createNotification(2000))
133                 whenever(webInterface.getNotifications(currentSone)).thenReturn(notifications)
134                 verifyVariableMatches("notifications", contains(notifications[1], notifications[2], notifications[0]))
135         }
136
137         @Test
138         fun `notification hash is set in template context`() {
139                 val notifications = listOf(createNotification(3000), createNotification(1000), createNotification(2000))
140                 whenever(webInterface.getNotifications(currentSone)).thenReturn(notifications)
141                 verifyVariableIsSet("notificationHash", listOf(notifications[1], notifications[2], notifications[0]).hashCode())
142         }
143
144         @Test
145         fun `handleRequest method is called`() {
146                 var called = false
147                 val page = object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {
148                         override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
149                                 called = true
150                         }
151                 }
152                 page.processTemplate(freenetRequest, templateContext)
153                 assertThat(called, equalTo(true))
154         }
155
156         @Test
157         fun `redirect does not happen if login is not required`() {
158                 val page = SoneTemplatePage("page.html", webInterface, template, requiresLogin = false)
159                 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
160         }
161
162         @Test
163         fun `redirect does not happen if sone is logged in`() {
164                 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
165         }
166
167         @Test
168         fun `redirect does happen if sone is not logged in`() {
169                 unsetCurrentSone()
170                 request("index.html")
171                 assertThat(page.getRedirectTarget(freenetRequest), equalTo("login.html?target=index.html"))
172         }
173
174         @Test
175         fun `redirect does happen with parameters encoded correctly if sone is not logged in`() {
176                 unsetCurrentSone()
177                 request("index.html")
178                 addHttpRequestParameter("foo", "b=r")
179                 addHttpRequestParameter("baz", "q&o")
180                 assertThat(page.getRedirectTarget(freenetRequest), anyOf(
181                                 equalTo("login.html?target=index.html%3Ffoo%3Db%253Dr%26baz%3Dq%2526o"),
182                                 equalTo("login.html?target=index.html%3Fbaz%3Dq%2526o%26foo%3Db%253Dr")
183                 ))
184         }
185
186         @Test
187         fun `page is disabled if full access is required but request does not have full access`() {
188                 core.preferences.isRequireFullAccess = true
189                 assertThat(page.isEnabled(toadletContext), equalTo(false))
190         }
191
192         @Test
193         fun `page is disabled if login is required but there is no current sone`() {
194                 unsetCurrentSone()
195                 assertThat(page.isEnabled(toadletContext), equalTo(false))
196         }
197
198         @Test
199         fun `page is enabled if login is required and there is a current sone`() {
200                 assertThat(page.isEnabled(toadletContext), equalTo(true))
201         }
202
203         @Test
204         fun `page is enabled if full access is required and request has full access and login is required and there is a current sone`() {
205                 core.preferences.isRequireFullAccess = true
206                 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
207                 assertThat(page.isEnabled(toadletContext), equalTo(true))
208         }
209
210         @Test
211         fun `page is enabled if no full access is required and login is not required`() {
212                 SoneTemplatePage("path.html", webInterface, template, requiresLogin = false).let { page ->
213                         assertThat(page.isEnabled(toadletContext), equalTo(true))
214                 }
215         }
216
217 }