7146f2e3551c2af359136a367e407212f150c3ac
[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 `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"))
76         }
77
78         @Test
79         fun `get request without sone does not store sone-specific preferences in the template context`() {
80                 unsetCurrentSone()
81                 page.handleRequest(freenetRequest, templateContext)
82                 assertThat(templateContext["auto-follow"], nullValue())
83                 assertThat(templateContext["show-notification-new-sones"], nullValue())
84                 assertThat(templateContext["show-notification-new-posts"], nullValue())
85                 assertThat(templateContext["show-notification-new-replies"], nullValue())
86                 assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
87                 assertThat(templateContext["load-linked-images"], nullValue())
88                 assertThat(templateContext["show-custom-avatars"], nullValue())
89         }
90
91         private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
92                 setMethod(POST)
93                 addHttpRequestPart("show-custom-avatars", "ALWAYS")
94                 addHttpRequestPart("load-linked-images", "ALWAYS")
95                 addHttpRequestPart(option, setValue.toString())
96                 verifyRedirect("options.html") {
97                         assertThat(getter(), equalTo(expectedValue))
98                 }
99         }
100
101         @Test
102         fun `auto-follow option can be set`() {
103                 verifyThatOptionCanBeSet("auto-follow", "checked", true) { currentSone.options.isAutoFollow }
104         }
105
106         @Test
107         fun `show new sone notification option can be set`() {
108                 verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
109         }
110
111         @Test
112         fun `show new post notification option can be set`() {
113                 verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
114         }
115
116         @Test
117         fun `show new reply notification option can be set`() {
118                 verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
119         }
120
121         @Test
122         fun `enable sone insert notifications option can be set`() {
123                 verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
124         }
125
126         @Test
127         fun `load linked images option can be set`() {
128                 verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
129         }
130
131         @Test
132         fun `show custom avatar option can be set`() {
133                 verifyThatOptionCanBeSet("show-custom-avatars", "TRUSTED", TRUSTED) { currentSone.options.showCustomAvatars }
134         }
135
136         private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
137                 unsetCurrentSone()
138                 setMethod(POST)
139                 addHttpRequestPart(name, value)
140                 page.handleRequest(freenetRequest, templateContext)
141                 assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
142         }
143
144         private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
145                 unsetCurrentSone()
146                 setMethod(POST)
147                 addHttpRequestPart(name, setValue)
148                 verifyRedirect("options.html") {
149                         assertThat(getter(), equalTo(expectedValue))
150                 }
151         }
152
153         @Test
154         fun `insertion delay can not be set to less than 0 seconds`() {
155                 verifyThatWrongValueForPreferenceIsDetected("insertion-delay", "-1")
156         }
157
158         @Test
159         fun `insertion delay can be set to 0 seconds`() {
160                 verifyThatPreferencesCanBeSet("insertion-delay", "0", 0) { core.preferences.insertionDelay }
161         }
162
163         @Test
164         fun `setting insertion to an invalid value will reset it`() {
165                 verifyThatPreferencesCanBeSet("insertion-delay", "foo", 60) { core.preferences.insertionDelay }
166         }
167
168         @Test
169         fun `characters per post can not be set to less than -1`() {
170                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "-2")
171         }
172
173         @Test
174         fun `characters per post can be set to -1`() {
175                 verifyThatPreferencesCanBeSet("characters-per-post", "-1", -1) { core.preferences.charactersPerPost }
176         }
177
178         @Test
179         fun `characters per post can not be set to 0`() {
180                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "0")
181         }
182
183         @Test
184         fun `characters per post can not be set to 49`() {
185                 verifyThatWrongValueForPreferenceIsDetected("characters-per-post", "49")
186         }
187
188         @Test
189         fun `characters per post can be set to 50`() {
190                 verifyThatPreferencesCanBeSet("characters-per-post", "50", 50) { core.preferences.charactersPerPost }
191         }
192
193         @Test
194         fun `fcp full acess required option can be set to always`() {
195                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "2", ALWAYS) { core.preferences.fcpFullAccessRequired }
196         }
197
198         @Test
199         fun `fcp full acess required option can be set to writing`() {
200                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "1", WRITING) { core.preferences.fcpFullAccessRequired }
201         }
202
203         @Test
204         fun `fcp full acess required option can be set to no`() {
205                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "0", NO) { core.preferences.fcpFullAccessRequired }
206         }
207
208         @Test
209         fun `fcp full acess required option is not changed if invalid value is set`() {
210                 verifyThatPreferencesCanBeSet("fcp-full-access-required", "foo", WRITING) { core.preferences.fcpFullAccessRequired }
211         }
212
213         @Test
214         fun `images per page can not be set to 0`() {
215                 verifyThatWrongValueForPreferenceIsDetected("images-per-page", "0")
216         }
217
218         @Test
219         fun `images per page can be set to 1`() {
220                 verifyThatPreferencesCanBeSet("images-per-page", "1", 1) { core.preferences.imagesPerPage }
221         }
222
223         @Test
224         fun `images per page is set to 9 if invalid value is requested`() {
225                 verifyThatPreferencesCanBeSet("images-per-page", "foo", 9) { core.preferences.imagesPerPage }
226         }
227
228         @Test
229         fun `fcp interface can be set to true`() {
230                 verifyThatPreferencesCanBeSet("fcp-interface-active", "checked", true) { core.preferences.isFcpInterfaceActive }
231         }
232
233         @Test
234         fun `fcp interface can be set to false`() {
235                 verifyThatPreferencesCanBeSet("fcp-interface-active", null, false) { core.preferences.isFcpInterfaceActive }
236         }
237
238         @Test
239         fun `require full access can be set to true`() {
240                 verifyThatPreferencesCanBeSet("require-full-access", "checked", true) { core.preferences.isRequireFullAccess }
241         }
242
243         @Test
244         fun `require full access can be set to false`() {
245                 verifyThatPreferencesCanBeSet("require-full-access", null, false) { core.preferences.isRequireFullAccess }
246         }
247
248         @Test
249         fun `negative trust can not be set to -101`() {
250                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "-101")
251         }
252
253         @Test
254         fun `negative trust can be set to -100`() {
255                 verifyThatPreferencesCanBeSet("negative-trust", "-100", -100) { core.preferences.negativeTrust }
256         }
257
258         @Test
259         fun `negative trust can be set to 100`() {
260                 verifyThatPreferencesCanBeSet("negative-trust", "100", 100) { core.preferences.negativeTrust }
261         }
262
263         @Test
264         fun `negative trust can not be set to 101`() {
265                 verifyThatWrongValueForPreferenceIsDetected("negative-trust", "101")
266         }
267
268         @Test
269         fun `negative trust is set to default on invalid value`() {
270                 verifyThatPreferencesCanBeSet("negative-trust", "invalid", -25) { core.preferences.negativeTrust }
271         }
272
273         @Test
274         fun `positive trust can not be set to -1`() {
275                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "-1")
276         }
277
278         @Test
279         fun `positive trust can be set to 0`() {
280                 verifyThatPreferencesCanBeSet("positive-trust", "0", 0) { core.preferences.positiveTrust }
281         }
282
283         @Test
284         fun `positive trust can be set to 100`() {
285                 verifyThatPreferencesCanBeSet("positive-trust", "100", 100) { core.preferences.positiveTrust }
286         }
287
288         @Test
289         fun `positive trust can not be set to 101`() {
290                 verifyThatWrongValueForPreferenceIsDetected("positive-trust", "101")
291         }
292
293         @Test
294         fun `positive trust is set to default on invalid value`() {
295                 verifyThatPreferencesCanBeSet("positive-trust", "invalid", 75) { core.preferences.positiveTrust }
296         }
297
298         @Test
299         fun `post cut off length can not be set to -49`() {
300                 verifyThatWrongValueForPreferenceIsDetected("post-cut-off-length", "-49")
301         }
302
303         @Test
304         fun `post cut off length can be set to 50`() {
305                 verifyThatPreferencesCanBeSet("post-cut-off-length", "50", 50) { core.preferences.postCutOffLength }
306         }
307
308         @Test
309         fun `post cut off length is set to default on invalid value`() {
310                 verifyThatPreferencesCanBeSet("post-cut-off-length", "invalid", 200) { core.preferences.postCutOffLength }
311         }
312
313         @Test
314         fun `posts per page can not be set to 0`() {
315                 verifyThatWrongValueForPreferenceIsDetected("posts-per-page", "-49")
316         }
317
318         @Test
319         fun `posts per page can be set to 1`() {
320                 verifyThatPreferencesCanBeSet("posts-per-page", "1", 1) { core.preferences.postsPerPage }
321         }
322
323         @Test
324         fun `posts per page is set to default on invalid value`() {
325                 verifyThatPreferencesCanBeSet("posts-per-page", "invalid", 10) { core.preferences.postsPerPage }
326         }
327
328         @Test
329         fun `trust comment can be set`() {
330                 verifyThatPreferencesCanBeSet("trust-comment", "trust", "trust") { core.preferences.trustComment }
331         }
332
333         @Test
334         fun `trust comment is set to default when set to empty value`() {
335                 verifyThatPreferencesCanBeSet("trust-comment", "", "Set from Sone Web Interface") { core.preferences.trustComment }
336         }
337
338 }