1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.SoneOptions.*
4 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.*
5 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.*
6 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
7 import net.pterodactylus.sone.test.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.web.Method.*
11 import org.hamcrest.MatcherAssert.*
12 import org.hamcrest.Matchers.*
16 * Unit test for [OptionsPage].
18 class OptionsPageTest : WebPageTest(::OptionsPage) {
21 fun setupPreferences() {
22 core.preferences.newInsertionDelay = 1
23 core.preferences.newCharactersPerPost = 50
24 core.preferences.newFcpFullAccessRequired = WRITING
25 core.preferences.newImagesPerPage = 4
26 core.preferences.newFcpInterfaceActive = true
27 core.preferences.newRequireFullAccess = true
28 core.preferences.newNegativeTrust = 7
29 core.preferences.newPositiveTrust = 8
30 core.preferences.newPostCutOffLength = 51
31 core.preferences.newPostsPerPage = 10
32 core.preferences.newTrustComment = "11"
36 fun setupSoneOptions() {
37 whenever(currentSone.options).thenReturn(DefaultSoneOptions().apply {
39 isShowNewPostNotifications = true
40 isShowNewReplyNotifications = true
41 isShowNewSoneNotifications = true
42 isSoneInsertNotificationEnabled = true
43 loadLinkedImages = FOLLOWED
44 showCustomAvatars = FOLLOWED
49 fun `page returns correct path`() {
50 assertThat(page.path, equalTo("options.html"))
54 fun `page does not require login`() {
55 assertThat(page.requiresLogin(), equalTo(false))
59 fun `page returns correct title`() {
60 addTranslation("Page.Options.Title", "options page title")
61 assertThat(page.getPageTitle(soneRequest), equalTo("options page title"))
65 fun `get request stores all preferences in the template context`() {
67 assertThat(templateContext["auto-follow"], equalTo<Any>(true))
68 assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
69 assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
70 assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
71 assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
72 assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
73 assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
74 assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
75 assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
76 assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
77 assertThat(templateContext["images-per-page"], equalTo<Any>(4))
78 assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
79 assertThat(templateContext["require-full-access"], equalTo<Any>(true))
80 assertThat(templateContext["negative-trust"], equalTo<Any>(7))
81 assertThat(templateContext["positive-trust"], equalTo<Any>(8))
82 assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
83 assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
84 assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
89 fun `get request without sone does not store sone-specific preferences in the template context`() {
92 assertThat(templateContext["auto-follow"], nullValue())
93 assertThat(templateContext["show-notification-new-sones"], nullValue())
94 assertThat(templateContext["show-notification-new-posts"], nullValue())
95 assertThat(templateContext["show-notification-new-replies"], nullValue())
96 assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
97 assertThat(templateContext["load-linked-images"], nullValue())
98 assertThat(templateContext["show-custom-avatars"], nullValue())
102 private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
104 addHttpRequestPart("show-custom-avatars", "ALWAYS")
105 addHttpRequestPart("load-linked-images", "ALWAYS")
106 setValue?.also { addHttpRequestPart(option, it.toString()) }
107 verifyRedirect("options.html") {
108 assertThat(getter(), equalTo(expectedValue))
113 fun `auto-follow option can be set`() {
114 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
118 fun `auto-follow option can be unset`() {
119 verifyThatOptionCanBeSet("auto-follow", null, false) { currentSone.options.isAutoFollow }
123 fun `show new sone notification option can be set`() {
124 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
128 fun `show new sone notification option can be unset`() {
129 verifyThatOptionCanBeSet("" +
130 "", null, false) { currentSone.options.isShowNewSoneNotifications }
134 fun `show new post notification option can be set`() {
135 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
139 fun `show new post notification option can be unset`() {
140 verifyThatOptionCanBeSet("show-notification-new-posts", null, false) { currentSone.options.isShowNewPostNotifications }
144 fun `show new reply notification option can be set`() {
145 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
149 fun `show new reply notification option can be unset`() {
150 verifyThatOptionCanBeSet("show-notification-new-replies", null, false) { currentSone.options.isShowNewReplyNotifications }
154 fun `enable sone insert notifications option can be set`() {
155 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
159 fun `enable sone insert notifications option can be unset`() {
160 verifyThatOptionCanBeSet("enable-sone-insert-notifications", null, false) { currentSone.options.isSoneInsertNotificationEnabled }
164 fun `load linked images option can be set`() {
165 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
169 fun `show custom avatar option can be set`() {
170 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
173 private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
176 addHttpRequestPart(name, value)
178 assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
182 private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
185 setValue?.also { addHttpRequestPart(name, it) }
186 verifyRedirect("options.html") {
187 assertThat(getter(), equalTo(expectedValue))
192 fun `insertion delay can not be set to less than 0 seconds`() {
193 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
197 fun `insertion delay can be set to 0 seconds`() {
198 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
202 fun `setting insertion to an invalid value will reset it`() {
203 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
207 fun `characters per post can not be set to less than -1`() {
208 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
212 fun `characters per post can be set to -1`() {
213 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
217 fun `characters per post can not be set to 0`() {
218 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
222 fun `characters per post can not be set to 49`() {
223 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
227 fun `characters per post can be set to 50`() {
228 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
232 fun `fcp full acess required option can be set to always`() {
233 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
237 fun `fcp full acess required option can be set to writing`() {
238 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
242 fun `fcp full acess required option can be set to no`() {
243 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
247 fun `fcp full acess required option is not changed if invalid value is set`() {
248 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
252 fun `images per page can not be set to 0`() {
253 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
257 fun `images per page can be set to 1`() {
258 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
262 fun `images per page is set to 9 if invalid value is requested`() {
263 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
267 fun `fcp interface can be set to true`() {
268 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.fcpInterfaceActive }
272 fun `fcp interface can be set to false`() {
273 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.fcpInterfaceActive }
277 fun `require full access can be set to true`() {
278 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.requireFullAccess }
282 fun `require full access can be set to false`() {
283 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.requireFullAccess }
287 fun `negative trust can not be set to -101`() {
288 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
292 fun `negative trust can be set to -100`() {
293 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
297 fun `negative trust can be set to 100`() {
298 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
302 fun `negative trust can not be set to 101`() {
303 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
307 fun `negative trust is set to default on invalid value`() {
308 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
312 fun `positive trust can not be set to -1`() {
313 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
317 fun `positive trust can be set to 0`() {
318 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
322 fun `positive trust can be set to 100`() {
323 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
327 fun `positive trust can not be set to 101`() {
328 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
332 fun `positive trust is set to default on invalid value`() {
333 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
337 fun `post cut off length can not be set to -49`() {
338 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
342 fun `post cut off length can be set to 50`() {
343 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
347 fun `post cut off length is set to default on invalid value`() {
348 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
352 fun `posts per page can not be set to 0`() {
353 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
357 fun `posts per page can be set to 1`() {
358 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
362 fun `posts per page is set to default on invalid value`() {
363 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
367 fun `trust comment can be set`() {
368 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
372 fun `trust comment is set to default when set to empty value`() {
373 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }
377 fun `page can be created by dependency injection`() {
378 assertThat(baseInjector.getInstance<OptionsPage>(), notNullValue())
382 fun `page is annotated with correct menuname`() {
383 assertThat(page.menuName, equalTo("Options"))
387 fun `page is annotated with correct template path`() {
388 assertThat(page.templatePath, equalTo("/templates/options.html"))