1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions
4 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED
5 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED
6 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired
7 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
8 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO
9 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING
10 import net.pterodactylus.sone.test.whenever
11 import net.pterodactylus.sone.web.pages.OptionsPage
12 import net.pterodactylus.util.web.Method.GET
13 import net.pterodactylus.util.web.Method.POST
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.equalTo
16 import org.hamcrest.Matchers.hasItem
17 import org.hamcrest.Matchers.nullValue
18 import org.junit.Before
22 * Unit test for [OptionsPage].
24 class OptionsPageTest : WebPageTest() {
26 private val page = OptionsPage(template, webInterface)
28 override fun getPage() = page
31 fun setupPreferences() {
32 core.preferences.insertionDelay = 1
33 core.preferences.charactersPerPost = 50
34 core.preferences.fcpFullAccessRequired = WRITING
35 core.preferences.imagesPerPage = 4
36 core.preferences.isFcpInterfaceActive = true
37 core.preferences.isRequireFullAccess = true
38 core.preferences.negativeTrust = 7
39 core.preferences.positiveTrust = 8
40 core.preferences.postCutOffLength = 51
41 core.preferences.postsPerPage = 10
42 core.preferences.trustComment = "11"
46 fun setupSoneOptions() {
47 whenever(currentSone.options).thenReturn(DefaultSoneOptions().apply {
49 isShowNewPostNotifications = true
50 isShowNewReplyNotifications = true
51 isShowNewSoneNotifications = true
52 isSoneInsertNotificationEnabled = true
53 loadLinkedImages = FOLLOWED
54 showCustomAvatars = FOLLOWED
59 fun `get request stores all preferences in the template context`() {
61 page.handleRequest(freenetRequest, templateContext)
62 assertThat(templateContext["auto-follow"], equalTo<Any>(true))
63 assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
64 assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
65 assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
66 assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
67 assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
68 assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
69 assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
70 assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
71 assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
72 assertThat(templateContext["images-per-page"], equalTo<Any>(4))
73 assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
74 assertThat(templateContext["require-full-access"], equalTo<Any>(true))
75 assertThat(templateContext["negative-trust"], equalTo<Any>(7))
76 assertThat(templateContext["positive-trust"], equalTo<Any>(8))
77 assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
78 assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
79 assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
83 fun `get request without sone does not store sone-specific preferences in the template context`() {
86 page.handleRequest(freenetRequest, templateContext)
87 assertThat(templateContext["auto-follow"], nullValue())
88 assertThat(templateContext["show-notification-new-sones"], nullValue())
89 assertThat(templateContext["show-notification-new-posts"], nullValue())
90 assertThat(templateContext["show-notification-new-replies"], nullValue())
91 assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
92 assertThat(templateContext["load-linked-images"], nullValue())
93 assertThat(templateContext["show-custom-avatars"], nullValue())
96 private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
98 addHttpRequestParameter(option, setValue.toString())
99 addHttpRequestParameter("show-custom-avatars", "ALWAYS")
100 addHttpRequestParameter("load-linked-images", "ALWAYS")
101 verifyRedirect("options.html") {
102 assertThat(getter(), equalTo(expectedValue))
107 fun `auto-follow option can be set`() {
108 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
112 fun `show new sone notification option can be set`() {
113 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
117 fun `show new post notification option can be set`() {
118 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
122 fun `show new reply notification option can be set`() {
123 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
127 fun `enable sone insert notifications option can be set`() {
128 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
132 fun `load linked images option can be set`() {
133 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
137 fun `show custom avatar option can be set`() {
138 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
141 private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
144 addHttpRequestParameter(name, value)
145 page.handleRequest(freenetRequest, templateContext)
146 assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
149 private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
152 addHttpRequestParameter(name, setValue)
153 verifyRedirect("options.html") {
154 assertThat(getter(), equalTo(expectedValue))
159 fun `insertion delay can not be set to less than 0 seconds`() {
160 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
164 fun `insertion delay can be set to 0 seconds`() {
165 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
169 fun `setting insertion to an invalid value will reset it`() {
170 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
174 fun `characters per post can not be set to less than -1`() {
175 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
179 fun `characters per post can be set to -1`() {
180 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
184 fun `characters per post can not be set to 0`() {
185 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
189 fun `characters per post can not be set to 49`() {
190 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
194 fun `characters per post can be set to 50`() {
195 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
199 fun `fcp full acess required option can be set to always`() {
200 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
204 fun `fcp full acess required option can be set to writing`() {
205 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
209 fun `fcp full acess required option can be set to no`() {
210 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
214 fun `fcp full acess required option is not changed if invalid value is set`() {
215 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
219 fun `images per page can not be set to 0`() {
220 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
224 fun `images per page can be set to 1`() {
225 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
229 fun `images per page is set to 9 if invalid value is requested`() {
230 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
234 fun `fcp interface can be set to true`() {
235 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.isFcpInterfaceActive }
239 fun `fcp interface can be set to false`() {
240 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.isFcpInterfaceActive }
244 fun `require full access can be set to true`() {
245 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.isRequireFullAccess }
249 fun `require full access can be set to false`() {
250 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.isRequireFullAccess }
254 fun `negative trust can not be set to -101`() {
255 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
259 fun `negative trust can be set to -100`() {
260 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
264 fun `negative trust can be set to 100`() {
265 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
269 fun `negative trust can not be set to 101`() {
270 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
274 fun `negative trust is set to default on invalid value`() {
275 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
279 fun `positive trust can not be set to -1`() {
280 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
284 fun `positive trust can be set to 0`() {
285 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
289 fun `positive trust can be set to 100`() {
290 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
294 fun `positive trust can not be set to 101`() {
295 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
299 fun `positive trust is set to default on invalid value`() {
300 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
304 fun `post cut off length can not be set to -49`() {
305 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
309 fun `post cut off length can be set to 50`() {
310 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
314 fun `post cut off length is set to default on invalid value`() {
315 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
319 fun `posts per page can not be set to 0`() {
320 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
324 fun `posts per page can be set to 1`() {
325 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
329 fun `posts per page is set to default on invalid value`() {
330 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
334 fun `trust comment can be set`() {
335 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
339 fun `trust comment is set to default when set to empty value`() {
340 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }