Replace web page test base with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / OptionsPageTest.kt
index 7146f2e..4e4c02e 100644 (file)
@@ -18,11 +18,7 @@ import org.junit.Test
 /**
  * Unit test for [OptionsPage].
  */
-class OptionsPageTest : WebPageTest() {
-
-       private val page = OptionsPage(template, webInterface)
-
-       override fun getPage() = page
+class OptionsPageTest: WebPageTest(::OptionsPage) {
 
        @Before
        fun setupPreferences() {
@@ -53,46 +49,64 @@ class OptionsPageTest : WebPageTest() {
        }
 
        @Test
+       fun `page returns correct path`() {
+               assertThat(page.path, equalTo("options.html"))
+       }
+
+       @Test
+       fun `page does not require login`() {
+               assertThat(page.requiresLogin(), equalTo(false))
+       }
+
+       @Test
+       fun `page returns correct title`() {
+               addTranslation("Page.Options.Title", "options page title")
+               assertThat(page.getPageTitle(freenetRequest), equalTo("options page title"))
+       }
+
+       @Test
        fun `get request stores all preferences in the template context`() {
-               page.handleRequest(freenetRequest, templateContext)
-               assertThat(templateContext["auto-follow"], equalTo<Any>(true))
-               assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
-               assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
-               assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
-               assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
-               assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
-               assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
-               assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
-               assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
-               assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
-               assertThat(templateContext["images-per-page"], equalTo<Any>(4))
-               assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
-               assertThat(templateContext["require-full-access"], equalTo<Any>(true))
-               assertThat(templateContext["negative-trust"], equalTo<Any>(7))
-               assertThat(templateContext["positive-trust"], equalTo<Any>(8))
-               assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
-               assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
-               assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
+               verifyNoRedirect {
+                       assertThat(templateContext["auto-follow"], equalTo<Any>(true))
+                       assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
+                       assertThat(templateContext["show-notification-new-posts"], equalTo<Any>(true))
+                       assertThat(templateContext["show-notification-new-replies"], equalTo<Any>(true))
+                       assertThat(templateContext["enable-sone-insert-notifications"], equalTo<Any>(true))
+                       assertThat(templateContext["load-linked-images"], equalTo<Any>("FOLLOWED"))
+                       assertThat(templateContext["show-custom-avatars"], equalTo<Any>("FOLLOWED"))
+                       assertThat(templateContext["insertion-delay"], equalTo<Any>(1))
+                       assertThat(templateContext["characters-per-post"], equalTo<Any>(50))
+                       assertThat(templateContext["fcp-full-access-required"], equalTo<Any>(1))
+                       assertThat(templateContext["images-per-page"], equalTo<Any>(4))
+                       assertThat(templateContext["fcp-interface-active"], equalTo<Any>(true))
+                       assertThat(templateContext["require-full-access"], equalTo<Any>(true))
+                       assertThat(templateContext["negative-trust"], equalTo<Any>(7))
+                       assertThat(templateContext["positive-trust"], equalTo<Any>(8))
+                       assertThat(templateContext["post-cut-off-length"], equalTo<Any>(51))
+                       assertThat(templateContext["posts-per-page"], equalTo<Any>(10))
+                       assertThat(templateContext["trust-comment"], equalTo<Any>("11"))
+               }
        }
 
        @Test
        fun `get request without sone does not store sone-specific preferences in the template context`() {
                unsetCurrentSone()
-               page.handleRequest(freenetRequest, templateContext)
-               assertThat(templateContext["auto-follow"], nullValue())
-               assertThat(templateContext["show-notification-new-sones"], nullValue())
-               assertThat(templateContext["show-notification-new-posts"], nullValue())
-               assertThat(templateContext["show-notification-new-replies"], nullValue())
-               assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
-               assertThat(templateContext["load-linked-images"], nullValue())
-               assertThat(templateContext["show-custom-avatars"], nullValue())
+               verifyNoRedirect {
+                       assertThat(templateContext["auto-follow"], nullValue())
+                       assertThat(templateContext["show-notification-new-sones"], nullValue())
+                       assertThat(templateContext["show-notification-new-posts"], nullValue())
+                       assertThat(templateContext["show-notification-new-replies"], nullValue())
+                       assertThat(templateContext["enable-sone-insert-notifications"], nullValue())
+                       assertThat(templateContext["load-linked-images"], nullValue())
+                       assertThat(templateContext["show-custom-avatars"], nullValue())
+               }
        }
 
        private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
                setMethod(POST)
                addHttpRequestPart("show-custom-avatars", "ALWAYS")
                addHttpRequestPart("load-linked-images", "ALWAYS")
-               addHttpRequestPart(option, setValue.toString())
+               setValue?.also { addHttpRequestPart(option, it.toString()) }
                verifyRedirect("options.html") {
                        assertThat(getter(), equalTo(expectedValue))
                }
@@ -104,26 +118,52 @@ class OptionsPageTest : WebPageTest() {
        }
 
        @Test
+       fun `auto-follow option can be unset`() {
+               verifyThatOptionCanBeSet("auto-follow", null, false) { currentSone.options.isAutoFollow }
+       }
+
+       @Test
        fun `show new sone notification option can be set`() {
                verifyThatOptionCanBeSet("show-notification-new-sones", "checked", true) { currentSone.options.isShowNewSoneNotifications }
        }
 
        @Test
+       fun `show new sone notification option can be unset`() {
+               verifyThatOptionCanBeSet("" +
+                               "", null, false) { currentSone.options.isShowNewSoneNotifications }
+       }
+
+       @Test
        fun `show new post notification option can be set`() {
                verifyThatOptionCanBeSet("show-notification-new-posts", "checked", true) { currentSone.options.isShowNewPostNotifications }
        }
 
        @Test
+       fun `show new post notification option can be unset`() {
+               verifyThatOptionCanBeSet("show-notification-new-posts", null, false) { currentSone.options.isShowNewPostNotifications }
+       }
+
+       @Test
        fun `show new reply notification option can be set`() {
                verifyThatOptionCanBeSet("show-notification-new-replies", "checked", true) { currentSone.options.isShowNewReplyNotifications }
        }
 
        @Test
+       fun `show new reply notification option can be unset`() {
+               verifyThatOptionCanBeSet("show-notification-new-replies", null, false) { currentSone.options.isShowNewReplyNotifications }
+       }
+
+       @Test
        fun `enable sone insert notifications option can be set`() {
                verifyThatOptionCanBeSet("enable-sone-insert-notifications", "checked", true) { currentSone.options.isSoneInsertNotificationEnabled }
        }
 
        @Test
+       fun `enable sone insert notifications option can be unset`() {
+               verifyThatOptionCanBeSet("enable-sone-insert-notifications", null, false) { currentSone.options.isSoneInsertNotificationEnabled }
+       }
+
+       @Test
        fun `load linked images option can be set`() {
                verifyThatOptionCanBeSet("load-linked-images", "TRUSTED", TRUSTED) { currentSone.options.loadLinkedImages }
        }
@@ -137,14 +177,15 @@ class OptionsPageTest : WebPageTest() {
                unsetCurrentSone()
                setMethod(POST)
                addHttpRequestPart(name, value)
-               page.handleRequest(freenetRequest, templateContext)
-               assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
+               verifyNoRedirect {
+                       assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
+               }
        }
 
        private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
                unsetCurrentSone()
                setMethod(POST)
-               addHttpRequestPart(name, setValue)
+               setValue?.also { addHttpRequestPart(name, it) }
                verifyRedirect("options.html") {
                        assertThat(getter(), equalTo(expectedValue))
                }