X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FOptionsPageTest.kt;h=4e4c02e0f426f8b4ea392407d07555f94b4b36ef;hp=f239e3ea512b213115966196554643b27f051cd0;hb=2dd40fba7031cffb35a5156435547a5d964535c9;hpb=05fb821e72072bde52f383bdc5a988da67f66d0c diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/OptionsPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/OptionsPageTest.kt index f239e3e..4e4c02e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/OptionsPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/OptionsPageTest.kt @@ -3,13 +3,10 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED -import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.pages.OptionsPage -import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Method.POST import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo @@ -21,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() { @@ -56,48 +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`() { - request("", GET) - page.handleRequest(freenetRequest, templateContext) - assertThat(templateContext["auto-follow"], equalTo(true)) - assertThat(templateContext["show-notification-new-sones"], equalTo(true)) - assertThat(templateContext["show-notification-new-posts"], equalTo(true)) - assertThat(templateContext["show-notification-new-replies"], equalTo(true)) - assertThat(templateContext["enable-sone-insert-notifications"], equalTo(true)) - assertThat(templateContext["load-linked-images"], equalTo("FOLLOWED")) - assertThat(templateContext["show-custom-avatars"], equalTo("FOLLOWED")) - assertThat(templateContext["insertion-delay"], equalTo(1)) - assertThat(templateContext["characters-per-post"], equalTo(50)) - assertThat(templateContext["fcp-full-access-required"], equalTo(1)) - assertThat(templateContext["images-per-page"], equalTo(4)) - assertThat(templateContext["fcp-interface-active"], equalTo(true)) - assertThat(templateContext["require-full-access"], equalTo(true)) - assertThat(templateContext["negative-trust"], equalTo(7)) - assertThat(templateContext["positive-trust"], equalTo(8)) - assertThat(templateContext["post-cut-off-length"], equalTo(51)) - assertThat(templateContext["posts-per-page"], equalTo(10)) - assertThat(templateContext["trust-comment"], equalTo("11")) + verifyNoRedirect { + assertThat(templateContext["auto-follow"], equalTo(true)) + assertThat(templateContext["show-notification-new-sones"], equalTo(true)) + assertThat(templateContext["show-notification-new-posts"], equalTo(true)) + assertThat(templateContext["show-notification-new-replies"], equalTo(true)) + assertThat(templateContext["enable-sone-insert-notifications"], equalTo(true)) + assertThat(templateContext["load-linked-images"], equalTo("FOLLOWED")) + assertThat(templateContext["show-custom-avatars"], equalTo("FOLLOWED")) + assertThat(templateContext["insertion-delay"], equalTo(1)) + assertThat(templateContext["characters-per-post"], equalTo(50)) + assertThat(templateContext["fcp-full-access-required"], equalTo(1)) + assertThat(templateContext["images-per-page"], equalTo(4)) + assertThat(templateContext["fcp-interface-active"], equalTo(true)) + assertThat(templateContext["require-full-access"], equalTo(true)) + assertThat(templateContext["negative-trust"], equalTo(7)) + assertThat(templateContext["positive-trust"], equalTo(8)) + assertThat(templateContext["post-cut-off-length"], equalTo(51)) + assertThat(templateContext["posts-per-page"], equalTo(10)) + assertThat(templateContext["trust-comment"], equalTo("11")) + } } @Test fun `get request without sone does not store sone-specific preferences in the template context`() { - request("", GET) 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 verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) { - request("", POST) + 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)) } @@ -109,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 } } @@ -140,16 +175,17 @@ class OptionsPageTest : WebPageTest() { private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) { unsetCurrentSone() - request("", POST) + 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 verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) { unsetCurrentSone() - request("", POST) - addHttpRequestPart(name, setValue) + setMethod(POST) + setValue?.also { addHttpRequestPart(name, it) } verifyRedirect("options.html") { assertThat(getter(), equalTo(expectedValue)) }