1 package net.pterodactylus.sone.web
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.WRITING
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
10 import net.pterodactylus.util.web.Method.GET
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.hasItem
15 import org.hamcrest.Matchers.nullValue
16 import org.junit.Before
20 * Unit test for [OptionsPage].
22 class OptionsPageTest : WebPageTest() {
24 private val page = OptionsPage(template, webInterface)
27 fun setupPreferences() {
28 core.preferences.insertionDelay = 1
29 core.preferences.charactersPerPost = 50
30 core.preferences.fcpFullAccessRequired = WRITING
31 core.preferences.imagesPerPage = 4
32 core.preferences.isFcpInterfaceActive = true
33 core.preferences.isRequireFullAccess = true
34 core.preferences.negativeTrust = 7
35 core.preferences.positiveTrust = 8
36 core.preferences.postCutOffLength = 51
37 core.preferences.postsPerPage = 10
38 core.preferences.trustComment = "11"
42 fun setupSoneOptions() {
43 whenever(currentSone.options).thenReturn(DefaultSoneOptions().apply {
45 isShowNewPostNotifications = true
46 isShowNewReplyNotifications = true
47 isShowNewSoneNotifications = true
48 isSoneInsertNotificationEnabled = true
49 loadLinkedImages = FOLLOWED
50 showCustomAvatars = FOLLOWED
55 fun `get request stores all preferences in the template context`() {
57 page.handleRequest(freenetRequest, templateContext)
58 assertThat(templateContext["auto-follow"], equalTo<Any>(true))
59 assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
60 assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
61 assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
62 assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
63 assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
64 assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
65 assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
66 assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
67 assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
68 assertThat(templateContext["images-per-page"], equalTo<Any>(4))
69 assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
70 assertThat(templateContext["require-full-access"], equalTo<Any>(true))
71 assertThat(templateContext["negative-trust"], equalTo<Any>(7))
72 assertThat(templateContext["positive-trust"], equalTo<Any>(8))
73 assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
74 assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
75 assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
79 fun `get request without sone does not store sone-specific preferences in the template context`() {
82 page.handleRequest(freenetRequest, templateContext)
83 assertThat(templateContext["auto-follow"], nullValue())
84 assertThat(templateContext["show-notification-new-sones"], nullValue())
85 assertThat(templateContext["show-notification-new-posts"], nullValue())
86 assertThat(templateContext["show-notification-new-replies"], nullValue())
87 assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
88 assertThat(templateContext["load-linked-images"], nullValue())
89 assertThat(templateContext["show-custom-avatars"], nullValue())
92 private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
94 addHttpRequestParameter("show-custom-avatars", "ALWAYS")
95 addHttpRequestParameter("load-linked-images", "ALWAYS")
96 addHttpRequestParameter(option, setValue.toString())
97 expectedException.expect(redirectsTo("options.html"))
99 page.handleRequest(freenetRequest, templateContext)
101 assertThat(getter(), equalTo(expectedValue))
106 fun `auto-follow option can be set`() {
107 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
111 fun `show new sone notification option can be set`() {
112 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
116 fun `show new post notification option can be set`() {
117 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
121 fun `show new reply notification option can be set`() {
122 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
126 fun `enable sone insert notifications option can be set`() {
127 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
131 fun `load linked images option can be set`() {
132 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
136 fun `show custom avatar option can be set`() {
137 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
140 private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
143 addHttpRequestParameter(name, value)
144 page.handleRequest(freenetRequest, templateContext)
145 assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
148 private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
151 addHttpRequestParameter(name, setValue)
152 expectedException.expect(redirectsTo("options.html"))
154 page.handleRequest(freenetRequest, templateContext)
156 assertThat(getter(), equalTo(expectedValue))
161 fun `insertion delay can not be set to less than 0 seconds`() {
162 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
166 fun `insertion delay can be set to 0 seconds`() {
167 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
171 fun `setting insertion to an invalid value will reset it`() {
172 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
176 fun `characters per post can not be set to less than -1`() {
177 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
181 fun `characters per post can be set to -1`() {
182 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
186 fun `characters per post can not be set to 0`() {
187 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
191 fun `characters per post can not be set to 49`() {
192 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
196 fun `characters per post can be set to 50`() {
197 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
201 fun `fcp full acess required option can be set to always`() {
202 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", FullAccessRequired.ALWAYS) { core.preferences.fcpFullAccessRequired }
206 fun `fcp full acess required option can be set to writing`() {
207 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", FullAccessRequired.WRITING) { core.preferences.fcpFullAccessRequired }
211 fun `fcp full acess required option can be set to no`() {
212 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", FullAccessRequired.NO) { core.preferences.fcpFullAccessRequired }
216 fun `fcp full acess required option is not changed if invalid value is set`() {
217 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", FullAccessRequired.WRITING) { core.preferences.fcpFullAccessRequired }
221 fun `images per page can not be set to 0`() {
222 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
226 fun `images per page can be set to 1`() {
227 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
231 fun `images per page is set to 9 if invalid value is requested`() {
232 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
236 fun `fcp interface can be set to true`() {
237 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.isFcpInterfaceActive }
241 fun `fcp interface can be set to false`() {
242 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.isFcpInterfaceActive }
246 fun `require full access can be set to true`() {
247 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.isRequireFullAccess }
251 fun `require full access can be set to false`() {
252 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.isRequireFullAccess }
256 fun `negative trust can not be set to -101`() {
257 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
261 fun `negative trust can be set to -100`() {
262 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
266 fun `negative trust can be set to 100`() {
267 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
271 fun `negative trust can not be set to 101`() {
272 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
276 fun `negative trust is set to default on invalid value`() {
277 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
281 fun `positive trust can not be set to -1`() {
282 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
286 fun `positive trust can be set to 0`() {
287 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
291 fun `positive trust can be set to 100`() {
292 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
296 fun `positive trust can not be set to 101`() {
297 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
301 fun `positive trust is set to default on invalid value`() {
302 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
306 fun `post cut off length can not be set to -49`() {
307 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
311 fun `post cut off length can be set to 50`() {
312 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
316 fun `post cut off length is set to default on invalid value`() {
317 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
321 fun `posts per page can not be set to 0`() {
322 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
326 fun `posts per page can be set to 1`() {
327 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
331 fun `posts per page is set to default on invalid value`() {
332 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
336 fun `trust comment can be set`() {
337 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
341 fun `trust comment is set to default when set to empty value`() {
342 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }