1 package net.pterodactylus.sone.web.pages
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
21 * Unit test for [SoneTemplatePage].
23 class SoneTemplatePageTest : WebPageTest({ template, webInterface -> object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {} }) {
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(""))
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"))
41 fun `additional link nodes contain open search link`() {
42 addHttpRequestHeader("Host", "www.example.com")
43 assertThat(page.getAdditionalLinkNodes(freenetRequest), contains(mapOf(
45 "type" to "application/opensearchdescription+xml",
47 "href" to "http://www.example.com/Sone/OpenSearch.xml"
52 fun `style sheets contains sone CSS file`() {
53 assertThat(page.styleSheets, contains("css/sone.css"))
57 fun `shortcut icon is the sone icon`() {
58 assertThat(page.shortcutIcon, equalTo("images/icon.png"))
61 private fun verifyVariableIsSet(name: String, value: Any) = verifyVariableMatches(name, equalTo<Any>(value))
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)
70 fun `preferences are set in template context`() {
71 verifyVariableIsSet("preferences", preferences)
75 fun `current sone is set in template context`() {
76 verifyVariableIsSet("currentSone", currentSone)
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()))
87 fun `freenet request is set in template context`() {
88 verifyVariableIsSet("request", freenetRequest)
92 fun `current version is set in template context`() {
93 verifyVariableIsSet("currentVersion", SonePlugin.getPluginVersion())
97 fun `has latest version is set correctly in template context if true`() {
98 whenever(core.updateChecker.hasLatestVersion()).thenReturn(true)
99 verifyVariableIsSet("hasLatestVersion", true)
103 fun `has latest version is set correctly in template context if false`() {
104 whenever(core.updateChecker.hasLatestVersion()).thenReturn(false)
105 verifyVariableIsSet("hasLatestVersion", false)
109 fun `latest edition is set in template context`() {
110 whenever(core.updateChecker.latestEdition).thenReturn(1234L)
111 verifyVariableIsSet("latestEdition", 1234L)
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))
121 fun `latest version time is set in template context`() {
122 whenever(core.updateChecker.latestVersionDate).thenReturn(12345L)
123 verifyVariableIsSet("latestVersionTime", 12345L)
126 private fun createNotification(time: Long) = mock<Notification>().apply {
127 whenever(createdTime).thenReturn(time)
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]))
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())
145 fun `handleRequest method is called`() {
147 val page = object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {
148 override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
152 page.processTemplate(freenetRequest, templateContext)
153 assertThat(called, equalTo(true))
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())
163 fun `redirect does not happen if sone is logged in`() {
164 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
168 fun `redirect does happen if sone is not logged in`() {
170 request("index.html")
171 assertThat(page.getRedirectTarget(freenetRequest), equalTo("login.html?target=index.html"))
175 fun `redirect does happen with parameters encoded correctly if sone is not logged in`() {
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")
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))
193 fun `page is disabled if login is required but there is no current sone`() {
195 assertThat(page.isEnabled(toadletContext), equalTo(false))
199 fun `page is enabled if login is required and there is a current sone`() {
200 assertThat(page.isEnabled(toadletContext), equalTo(true))
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))
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))