ecee2f49fa84db13daf3b108ef0e074308fab48c
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / OptionsPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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.getInstance
10 import net.pterodactylus.sone.test.whenever
11 import net.pterodactylus.sone.web.baseInjector
12 import net.pterodactylus.sone.web.page.*
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.notNullValue
18 import org.hamcrest.Matchers.nullValue
19 import org.junit.Before
20 import org.junit.Test
21
22 /**
23  * Unit test for [OptionsPage].
24  */
25 class OptionsPageTest: WebPageTest(::OptionsPage) {
26
27         @Before
28         fun setupPreferences() {
29                 core.preferences.newInsertionDelay = 1
30                 core.preferences.newCharactersPerPost = 50
31                 core.preferences.newFcpFullAccessRequired = WRITING
32                 core.preferences.newImagesPerPage = 4
33                 core.preferences.newFcpInterfaceActive = true
34                 core.preferences.newRequireFullAccess = true
35                 core.preferences.newNegativeTrust = 7
36                 core.preferences.newPositiveTrust = 8
37                 core.preferences.newPostCutOffLength = 51
38                 core.preferences.newPostsPerPage = 10
39                 core.preferences.newTrustComment = "11"
40         }
41
42         @Before
43         fun setupSoneOptions() {
44                 whenever(currentSone.options).thenReturn(DefaultSoneOptions().apply {
45                         isAutoFollow = true
46                         isShowNewPostNotifications = true
47                         isShowNewReplyNotifications = true
48                         isShowNewSoneNotifications = true
49                         isSoneInsertNotificationEnabled = true
50                         loadLinkedImages = FOLLOWED
51                         showCustomAvatars = FOLLOWED
52                 })
53         }
54
55         @Test
56         fun `page returns correct path`() {
57                 assertThat(page.path, equalTo("options.html"))
58         }
59
60         @Test
61         fun `page does not require login`() {
62                 assertThat(page.requiresLogin(), equalTo(false))
63         }
64
65         @Test
66         fun `page returns correct title`() {
67                 addTranslation("Page.Options.Title", "options page title")
68                 assertThat(page.getPageTitle(soneRequest), equalTo("options page title"))
69         }
70
71         @Test
72         fun `get request stores all preferences in the template context`() {
73                 verifyNoRedirect {
74                         assertThat(templateContext["auto-follow"], equalTo<Any>(true))
75                         assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
76                         assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
77                         assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
78                         assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
79                         assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
80                         assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
81                         assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
82                         assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
83                         assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
84                         assertThat(templateContext["images-per-page"], equalTo<Any>(4))
85                         assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
86                         assertThat(templateContext["require-full-access"], equalTo<Any>(true))
87                         assertThat(templateContext["negative-trust"], equalTo<Any>(7))
88                         assertThat(templateContext["positive-trust"], equalTo<Any>(8))
89                         assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
90                         assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
91                         assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
92                 }
93         }
94
95         @Test
96         fun `get request without sone does not store sone-specific preferences in the template context`() {
97                 unsetCurrentSone()
98                 verifyNoRedirect {
99                         assertThat(templateContext["auto-follow"], nullValue())
100                         assertThat(templateContext["show-notification-new-sones"], nullValue())
101                         assertThat(templateContext["show-notification-new-posts"], nullValue())
102                         assertThat(templateContext["show-notification-new-replies"], nullValue())
103                         assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
104                         assertThat(templateContext["load-linked-images"], nullValue())
105                         assertThat(templateContext["show-custom-avatars"], nullValue())
106                 }
107         }
108
109         private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
110                 setMethod(POST)
111                 addHttpRequestPart("show-custom-avatars", "ALWAYS")
112                 addHttpRequestPart("load-linked-images", "ALWAYS")
113                 setValue?.also { addHttpRequestPart(option, it.toString()) }
114                 verifyRedirect("options.html") {
115                         assertThat(getter(), equalTo(expectedValue))
116                 }
117         }
118
119         @Test
120         fun `auto-follow option can be set`() {
121                 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
122         }
123
124         @Test
125         fun `auto-follow option can be unset`() {
126                 verifyThatOptionCanBeSet("auto-follow", null, false) { currentSone.options.isAutoFollow }
127         }
128
129         @Test
130         fun `show new sone notification option can be set`() {
131                 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
132         }
133
134         @Test
135         fun `show new sone notification option can be unset`() {
136                 verifyThatOptionCanBeSet("" +
137                                 "", null, false) { currentSone.options.isShowNewSoneNotifications }
138         }
139
140         @Test
141         fun `show new post notification option can be set`() {
142                 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
143         }
144
145         @Test
146         fun `show new post notification option can be unset`() {
147                 verifyThatOptionCanBeSet("show-notification-new-posts", null, false) { currentSone.options.isShowNewPostNotifications }
148         }
149
150         @Test
151         fun `show new reply notification option can be set`() {
152                 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
153         }
154
155         @Test
156         fun `show new reply notification option can be unset`() {
157                 verifyThatOptionCanBeSet("show-notification-new-replies", null, false) { currentSone.options.isShowNewReplyNotifications }
158         }
159
160         @Test
161         fun `enable sone insert notifications option can be set`() {
162                 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
163         }
164
165         @Test
166         fun `enable sone insert notifications option can be unset`() {
167                 verifyThatOptionCanBeSet("enable-sone-insert-notifications", null, false) { currentSone.options.isSoneInsertNotificationEnabled }
168         }
169
170         @Test
171         fun `load linked images option can be set`() {
172                 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
173         }
174
175         @Test
176         fun `show custom avatar option can be set`() {
177                 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
178         }
179
180         private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
181                 unsetCurrentSone()
182                 setMethod(POST)
183                 addHttpRequestPart(name, value)
184                 verifyNoRedirect {
185                         assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
186                 }
187         }
188
189         private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
190                 unsetCurrentSone()
191                 setMethod(POST)
192                 setValue?.also { addHttpRequestPart(name, it) }
193                 verifyRedirect("options.html") {
194                         assertThat(getter(), equalTo(expectedValue))
195                 }
196         }
197
198         @Test
199         fun `insertion delay can not be set to less than 0 seconds`() {
200                 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
201         }
202
203         @Test
204         fun `insertion delay can be set to 0 seconds`() {
205                 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
206         }
207
208         @Test
209         fun `setting insertion to an invalid value will reset it`() {
210                 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
211         }
212
213         @Test
214         fun `characters per post can not be set to less than -1`() {
215                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
216         }
217
218         @Test
219         fun `characters per post can be set to -1`() {
220                 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
221         }
222
223         @Test
224         fun `characters per post can not be set to 0`() {
225                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
226         }
227
228         @Test
229         fun `characters per post can not be set to 49`() {
230                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
231         }
232
233         @Test
234         fun `characters per post can be set to 50`() {
235                 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
236         }
237
238         @Test
239         fun `fcp full acess required option can be set to always`() {
240                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
241         }
242
243         @Test
244         fun `fcp full acess required option can be set to writing`() {
245                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
246         }
247
248         @Test
249         fun `fcp full acess required option can be set to no`() {
250                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
251         }
252
253         @Test
254         fun `fcp full acess required option is not changed if invalid value is set`() {
255                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
256         }
257
258         @Test
259         fun `images per page can not be set to 0`() {
260                 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
261         }
262
263         @Test
264         fun `images per page can be set to 1`() {
265                 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
266         }
267
268         @Test
269         fun `images per page is set to 9 if invalid value is requested`() {
270                 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
271         }
272
273         @Test
274         fun `fcp interface can be set to true`() {
275                 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.fcpInterfaceActive }
276         }
277
278         @Test
279         fun `fcp interface can be set to false`() {
280                 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.fcpInterfaceActive }
281         }
282
283         @Test
284         fun `require full access can be set to true`() {
285                 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.requireFullAccess }
286         }
287
288         @Test
289         fun `require full access can be set to false`() {
290                 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.requireFullAccess }
291         }
292
293         @Test
294         fun `negative trust can not be set to -101`() {
295                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
296         }
297
298         @Test
299         fun `negative trust can be set to -100`() {
300                 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
301         }
302
303         @Test
304         fun `negative trust can be set to 100`() {
305                 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
306         }
307
308         @Test
309         fun `negative trust can not be set to 101`() {
310                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
311         }
312
313         @Test
314         fun `negative trust is set to default on invalid value`() {
315                 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
316         }
317
318         @Test
319         fun `positive trust can not be set to -1`() {
320                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
321         }
322
323         @Test
324         fun `positive trust can be set to 0`() {
325                 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
326         }
327
328         @Test
329         fun `positive trust can be set to 100`() {
330                 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
331         }
332
333         @Test
334         fun `positive trust can not be set to 101`() {
335                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
336         }
337
338         @Test
339         fun `positive trust is set to default on invalid value`() {
340                 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
341         }
342
343         @Test
344         fun `post cut off length can not be set to -49`() {
345                 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
346         }
347
348         @Test
349         fun `post cut off length can be set to 50`() {
350                 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
351         }
352
353         @Test
354         fun `post cut off length is set to default on invalid value`() {
355                 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
356         }
357
358         @Test
359         fun `posts per page can not be set to 0`() {
360                 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
361         }
362
363         @Test
364         fun `posts per page can be set to 1`() {
365                 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
366         }
367
368         @Test
369         fun `posts per page is set to default on invalid value`() {
370                 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
371         }
372
373         @Test
374         fun `trust comment can be set`() {
375                 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
376         }
377
378         @Test
379         fun `trust comment is set to default when set to empty value`() {
380                 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }
381         }
382
383         @Test
384         fun `page can be created by dependency injection`() {
385             assertThat(baseInjector.getInstance<OptionsPage>(), notNullValue())
386         }
387
388         @Test
389         fun `page is annotated with correct menuname`() {
390             assertThat(page.menuName, equalTo("Options"))
391         }
392
393 }