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.ALWAYS
7 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO
8 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.hasItem
14 import org.hamcrest.Matchers.nullValue
15 import org.junit.Before
19 * Unit test for [OptionsPage].
21 class OptionsPageTest: WebPageTest(::OptionsPage) {
24 fun setupPreferences() {
25 core.preferences.insertionDelay = 1
26 core.preferences.charactersPerPost = 50
27 core.preferences.fcpFullAccessRequired = WRITING
28 core.preferences.imagesPerPage = 4
29 core.preferences.isFcpInterfaceActive = true
30 core.preferences.isRequireFullAccess = true
31 core.preferences.negativeTrust = 7
32 core.preferences.positiveTrust = 8
33 core.preferences.postCutOffLength = 51
34 core.preferences.postsPerPage = 10
35 core.preferences.trustComment = "11"
39 fun setupSoneOptions() {
40 whenever(currentSone.options).thenReturn(DefaultSoneOptions().apply {
42 isShowNewPostNotifications = true
43 isShowNewReplyNotifications = true
44 isShowNewSoneNotifications = true
45 isSoneInsertNotificationEnabled = true
46 loadLinkedImages = FOLLOWED
47 showCustomAvatars = FOLLOWED
52 fun `page returns correct path`() {
53 assertThat(page.path, equalTo("options.html"))
57 fun `page does not require login`() {
58 assertThat(page.requiresLogin(), equalTo(false))
62 fun `page returns correct title`() {
63 addTranslation("Page.Options.Title", "options page title")
64 assertThat(page.getPageTitle(freenetRequest), equalTo("options page title"))
68 fun `get request stores all preferences in the template context`() {
70 assertThat(templateContext["auto-follow"], equalTo<Any>(true))
71 assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
72 assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
73 assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
74 assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
75 assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
76 assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
77 assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
78 assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
79 assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
80 assertThat(templateContext["images-per-page"], equalTo<Any>(4))
81 assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
82 assertThat(templateContext["require-full-access"], equalTo<Any>(true))
83 assertThat(templateContext["negative-trust"], equalTo<Any>(7))
84 assertThat(templateContext["positive-trust"], equalTo<Any>(8))
85 assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
86 assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
87 assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
92 fun `get request without sone does not store sone-specific preferences in the template context`() {
95 assertThat(templateContext["auto-follow"], nullValue())
96 assertThat(templateContext["show-notification-new-sones"], nullValue())
97 assertThat(templateContext["show-notification-new-posts"], nullValue())
98 assertThat(templateContext["show-notification-new-replies"], nullValue())
99 assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
100 assertThat(templateContext["load-linked-images"], nullValue())
101 assertThat(templateContext["show-custom-avatars"], nullValue())
105 private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
107 addHttpRequestPart("show-custom-avatars", "ALWAYS")
108 addHttpRequestPart("load-linked-images", "ALWAYS")
109 setValue?.also { addHttpRequestPart(option, it.toString()) }
110 verifyRedirect("options.html") {
111 assertThat(getter(), equalTo(expectedValue))
116 fun `auto-follow option can be set`() {
117 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
121 fun `auto-follow option can be unset`() {
122 verifyThatOptionCanBeSet("auto-follow", null, false) { currentSone.options.isAutoFollow }
126 fun `show new sone notification option can be set`() {
127 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
131 fun `show new sone notification option can be unset`() {
132 verifyThatOptionCanBeSet("" +
133 "", null, false) { currentSone.options.isShowNewSoneNotifications }
137 fun `show new post notification option can be set`() {
138 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
142 fun `show new post notification option can be unset`() {
143 verifyThatOptionCanBeSet("show-notification-new-posts", null, false) { currentSone.options.isShowNewPostNotifications }
147 fun `show new reply notification option can be set`() {
148 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
152 fun `show new reply notification option can be unset`() {
153 verifyThatOptionCanBeSet("show-notification-new-replies", null, false) { currentSone.options.isShowNewReplyNotifications }
157 fun `enable sone insert notifications option can be set`() {
158 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
162 fun `enable sone insert notifications option can be unset`() {
163 verifyThatOptionCanBeSet("enable-sone-insert-notifications", null, false) { currentSone.options.isSoneInsertNotificationEnabled }
167 fun `load linked images option can be set`() {
168 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
172 fun `show custom avatar option can be set`() {
173 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
176 private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
179 addHttpRequestPart(name, value)
181 assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
185 private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
188 setValue?.also { addHttpRequestPart(name, it) }
189 verifyRedirect("options.html") {
190 assertThat(getter(), equalTo(expectedValue))
195 fun `insertion delay can not be set to less than 0 seconds`() {
196 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
200 fun `insertion delay can be set to 0 seconds`() {
201 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
205 fun `setting insertion to an invalid value will reset it`() {
206 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
210 fun `characters per post can not be set to less than -1`() {
211 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
215 fun `characters per post can be set to -1`() {
216 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
220 fun `characters per post can not be set to 0`() {
221 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
225 fun `characters per post can not be set to 49`() {
226 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
230 fun `characters per post can be set to 50`() {
231 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
235 fun `fcp full acess required option can be set to always`() {
236 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
240 fun `fcp full acess required option can be set to writing`() {
241 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
245 fun `fcp full acess required option can be set to no`() {
246 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
250 fun `fcp full acess required option is not changed if invalid value is set`() {
251 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
255 fun `images per page can not be set to 0`() {
256 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
260 fun `images per page can be set to 1`() {
261 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
265 fun `images per page is set to 9 if invalid value is requested`() {
266 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
270 fun `fcp interface can be set to true`() {
271 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.isFcpInterfaceActive }
275 fun `fcp interface can be set to false`() {
276 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.isFcpInterfaceActive }
280 fun `require full access can be set to true`() {
281 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.isRequireFullAccess }
285 fun `require full access can be set to false`() {
286 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.isRequireFullAccess }
290 fun `negative trust can not be set to -101`() {
291 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
295 fun `negative trust can be set to -100`() {
296 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
300 fun `negative trust can be set to 100`() {
301 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
305 fun `negative trust can not be set to 101`() {
306 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
310 fun `negative trust is set to default on invalid value`() {
311 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
315 fun `positive trust can not be set to -1`() {
316 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
320 fun `positive trust can be set to 0`() {
321 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
325 fun `positive trust can be set to 100`() {
326 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
330 fun `positive trust can not be set to 101`() {
331 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
335 fun `positive trust is set to default on invalid value`() {
336 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
340 fun `post cut off length can not be set to -49`() {
341 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
345 fun `post cut off length can be set to 50`() {
346 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
350 fun `post cut off length is set to default on invalid value`() {
351 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
355 fun `posts per page can not be set to 0`() {
356 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
360 fun `posts per page can be set to 1`() {
361 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
365 fun `posts per page is set to default on invalid value`() {
366 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
370 fun `trust comment can be set`() {
371 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
375 fun `trust comment is set to default when set to empty value`() {
376 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }