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.*
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) {} }) {
30 fun `page title is empty string if no page title key was given`() {
31 SoneTemplatePage("path.html", webInterface, template, requiresLogin = false).let { page ->
32 assertThat(page.getPageTitle(freenetRequest), equalTo(""))
37 fun `page title is retrieved from l10n if page title key is given`() {
38 SoneTemplatePage("path.html", webInterface, template, "page.title", false).let { page ->
39 whenever(l10n.getString("page.title")).thenReturn("Page Title")
40 assertThat(page.getPageTitle(freenetRequest), equalTo("Page Title"))
45 fun `additional link nodes contain open search link`() {
46 addHttpRequestHeader("Host", "www.example.com")
47 assertThat(page.getAdditionalLinkNodes(freenetRequest), contains(mapOf(
49 "type" to "application/opensearchdescription+xml",
51 "href" to "http://www.example.com/Sone/OpenSearch.xml"
56 fun `style sheets contains sone CSS file`() {
57 assertThat(page.styleSheets, contains("css/sone.css"))
61 fun `shortcut icon is the sone icon`() {
62 assertThat(page.shortcutIcon, equalTo("images/icon.png"))
65 private fun verifyVariableIsSet(name: String, value: Any) = verifyVariableMatches(name, equalTo<Any>(value))
67 private fun <T> verifyVariableMatches(name: String, matcher: Matcher<T>) {
68 page.processTemplate(freenetRequest, templateContext)
69 @Suppress("UNCHECKED_CAST")
70 assertThat(templateContext[name] as T, matcher)
74 fun `preferences are set in template context`() {
75 verifyVariableIsSet("preferences", preferences)
79 fun `current sone is set in template context`() {
80 verifyVariableIsSet("currentSone", currentSone)
84 fun `local sones are set in template context`() {
85 val localSones = listOf(mock<Sone>(), mock())
86 whenever(core.localSones).thenReturn(localSones)
87 verifyVariableMatches("localSones", containsInAnyOrder(*localSones.toTypedArray()))
91 fun `freenet request is set in template context`() {
92 verifyVariableIsSet("request", freenetRequest)
96 fun `current version is set in template context`() {
97 verifyVariableIsSet("currentVersion", SonePlugin.getPluginVersion())
101 fun `has latest version is set correctly in template context if true`() {
102 whenever(core.updateChecker.hasLatestVersion()).thenReturn(true)
103 verifyVariableIsSet("hasLatestVersion", true)
107 fun `has latest version is set correctly in template context if false`() {
108 whenever(core.updateChecker.hasLatestVersion()).thenReturn(false)
109 verifyVariableIsSet("hasLatestVersion", false)
113 fun `latest edition is set in template context`() {
114 whenever(core.updateChecker.latestEdition).thenReturn(1234L)
115 verifyVariableIsSet("latestEdition", 1234L)
119 fun `latest version is set in template context`() {
120 whenever(core.updateChecker.latestVersion).thenReturn(Version(1, 2, 3))
121 verifyVariableIsSet("latestVersion", Version(1, 2, 3))
125 fun `latest version time is set in template context`() {
126 whenever(core.updateChecker.latestVersionDate).thenReturn(12345L)
127 verifyVariableIsSet("latestVersionTime", 12345L)
130 private fun createNotification(time: Long) = mock<Notification>().apply {
131 whenever(createdTime).thenReturn(time)
135 fun `notifications are set in template context`() {
136 val notifications = listOf(createNotification(3000), createNotification(1000), createNotification(2000))
137 whenever(webInterface.getNotifications(currentSone)).thenReturn(notifications)
138 verifyVariableMatches("notifications", contains(notifications[1], notifications[2], notifications[0]))
142 fun `notification hash is set in template context`() {
143 val notifications = listOf(createNotification(3000), createNotification(1000), createNotification(2000))
144 whenever(webInterface.getNotifications(currentSone)).thenReturn(notifications)
145 verifyVariableIsSet("notificationHash", listOf(notifications[1], notifications[2], notifications[0]).hashCode())
149 fun `handleRequest method is called`() {
151 val page = object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {
152 override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
156 page.processTemplate(freenetRequest, templateContext)
157 assertThat(called, equalTo(true))
161 fun `redirect does not happen if login is not required`() {
162 val page = SoneTemplatePage("page.html", webInterface, template, requiresLogin = false)
163 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
167 fun `redirect does not happen if sone is logged in`() {
168 assertThat(page.getRedirectTarget(freenetRequest), nullValue())
172 fun `redirect does happen if sone is not logged in`() {
174 assertThat(page.getRedirectTarget(freenetRequest), equalTo("login.html?target=index.html"))
178 fun `redirect does happen with parameters encoded correctly if sone is not logged in`() {
180 addHttpRequestParameter("foo", "b=r")
181 addHttpRequestParameter("baz", "q&o")
182 assertThat(page.getRedirectTarget(freenetRequest), anyOf(
183 equalTo("login.html?target=index.html%3Ffoo%3Db%253Dr%26baz%3Dq%2526o"),
184 equalTo("login.html?target=index.html%3Fbaz%3Dq%2526o%26foo%3Db%253Dr")
189 fun `page is disabled if full access is required but request does not have full access`() {
190 core.preferences.newRequireFullAccess = true
191 assertThat(page.isEnabled(toadletContext), equalTo(false))
195 fun `page is disabled if login is required but there is no current sone`() {
197 assertThat(page.isEnabled(toadletContext), equalTo(false))
201 fun `page is enabled if login is required and there is a current sone`() {
202 assertThat(page.isEnabled(toadletContext), equalTo(true))
206 fun `page is enabled if full access is required and request has full access and login is required and there is a current sone`() {
207 core.preferences.newRequireFullAccess = true
208 whenever(toadletContext.isAllowedFullAccess).thenReturn(true)
209 assertThat(page.isEnabled(toadletContext), equalTo(true))
213 fun `page is enabled if no full access is required and login is not required`() {
214 SoneTemplatePage("path.html", webInterface, template, requiresLogin = false).let { page ->
215 assertThat(page.isEnabled(toadletContext), equalTo(true))
220 fun `handle request with sone request is called`() {
222 val page = object : SoneTemplatePage("path.html", webInterface, template) {
223 override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
227 page.processTemplate(freenetRequest, templateContext)
228 assertThat(called, equalTo(true))