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