Fix test formatting
[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.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
16 import org.junit.Test
17
18 /**
19  * Unit test for [OptionsPage].
20  */
21 class OptionsPageTest: WebPageTest() {
22
23         private val page = OptionsPage(template, webInterface)
24
25         override fun getPage() = page
26
27         @Before
28         fun setupPreferences() {
29                 core.preferences.insertionDelay = 1
30                 core.preferences.charactersPerPost = 50
31                 core.preferences.fcpFullAccessRequired = WRITING
32                 core.preferences.imagesPerPage = 4
33                 core.preferences.isFcpInterfaceActive = true
34                 core.preferences.isRequireFullAccess = true
35                 core.preferences.negativeTrust = 7
36                 core.preferences.positiveTrust = 8
37                 core.preferences.postCutOffLength = 51
38                 core.preferences.postsPerPage = 10
39                 core.preferences.trustComment = "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(freenetRequest), 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                 addHttpRequestPart(option, setValue.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 `show new sone notification option can be set`() {
126                 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
127         }
128
129         @Test
130         fun `show new post notification option can be set`() {
131                 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
132         }
133
134         @Test
135         fun `show new reply notification option can be set`() {
136                 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
137         }
138
139         @Test
140         fun `enable sone insert notifications option can be set`() {
141                 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
142         }
143
144         @Test
145         fun `load linked images option can be set`() {
146                 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
147         }
148
149         @Test
150         fun `show custom avatar option can be set`() {
151                 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
152         }
153
154         private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
155                 unsetCurrentSone()
156                 setMethod(POST)
157                 addHttpRequestPart(name, value)
158                 verifyNoRedirect {
159                         assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
160                 }
161         }
162
163         private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
164                 unsetCurrentSone()
165                 setMethod(POST)
166                 addHttpRequestPart(name, setValue)
167                 verifyRedirect("options.html") {
168                         assertThat(getter(), equalTo(expectedValue))
169                 }
170         }
171
172         @Test
173         fun `insertion delay can not be set to less than 0 seconds`() {
174                 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
175         }
176
177         @Test
178         fun `insertion delay can be set to 0 seconds`() {
179                 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
180         }
181
182         @Test
183         fun `setting insertion to an invalid value will reset it`() {
184                 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
185         }
186
187         @Test
188         fun `characters per post can not be set to less than -1`() {
189                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
190         }
191
192         @Test
193         fun `characters per post can be set to -1`() {
194                 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
195         }
196
197         @Test
198         fun `characters per post can not be set to 0`() {
199                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
200         }
201
202         @Test
203         fun `characters per post can not be set to 49`() {
204                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
205         }
206
207         @Test
208         fun `characters per post can be set to 50`() {
209                 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
210         }
211
212         @Test
213         fun `fcp full acess required option can be set to always`() {
214                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
215         }
216
217         @Test
218         fun `fcp full acess required option can be set to writing`() {
219                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
220         }
221
222         @Test
223         fun `fcp full acess required option can be set to no`() {
224                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
225         }
226
227         @Test
228         fun `fcp full acess required option is not changed if invalid value is set`() {
229                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
230         }
231
232         @Test
233         fun `images per page can not be set to 0`() {
234                 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
235         }
236
237         @Test
238         fun `images per page can be set to 1`() {
239                 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
240         }
241
242         @Test
243         fun `images per page is set to 9 if invalid value is requested`() {
244                 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
245         }
246
247         @Test
248         fun `fcp interface can be set to true`() {
249                 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.isFcpInterfaceActive }
250         }
251
252         @Test
253         fun `fcp interface can be set to false`() {
254                 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.isFcpInterfaceActive }
255         }
256
257         @Test
258         fun `require full access can be set to true`() {
259                 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.isRequireFullAccess }
260         }
261
262         @Test
263         fun `require full access can be set to false`() {
264                 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.isRequireFullAccess }
265         }
266
267         @Test
268         fun `negative trust can not be set to -101`() {
269                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
270         }
271
272         @Test
273         fun `negative trust can be set to -100`() {
274                 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
275         }
276
277         @Test
278         fun `negative trust can be set to 100`() {
279                 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
280         }
281
282         @Test
283         fun `negative trust can not be set to 101`() {
284                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
285         }
286
287         @Test
288         fun `negative trust is set to default on invalid value`() {
289                 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
290         }
291
292         @Test
293         fun `positive trust can not be set to -1`() {
294                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
295         }
296
297         @Test
298         fun `positive trust can be set to 0`() {
299                 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
300         }
301
302         @Test
303         fun `positive trust can be set to 100`() {
304                 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
305         }
306
307         @Test
308         fun `positive trust can not be set to 101`() {
309                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
310         }
311
312         @Test
313         fun `positive trust is set to default on invalid value`() {
314                 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
315         }
316
317         @Test
318         fun `post cut off length can not be set to -49`() {
319                 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
320         }
321
322         @Test
323         fun `post cut off length can be set to 50`() {
324                 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
325         }
326
327         @Test
328         fun `post cut off length is set to default on invalid value`() {
329                 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
330         }
331
332         @Test
333         fun `posts per page can not be set to 0`() {
334                 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
335         }
336
337         @Test
338         fun `posts per page can be set to 1`() {
339                 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
340         }
341
342         @Test
343         fun `posts per page is set to default on invalid value`() {
344                 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
345         }
346
347         @Test
348         fun `trust comment can be set`() {
349                 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
350         }
351
352         @Test
353         fun `trust comment is set to default when set to empty value`() {
354                 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }
355         }
356
357 }