From 14d030de10eff6c2279700b73150d8e3f84bc652 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Jul 2019 17:16:14 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20page=20to=20activate=20debug?= =?utf8?q?=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 1 + .../net/pterodactylus/sone/web/pages/DebugPage.kt | 18 ++++++ .../pterodactylus/sone/web/pages/DebugPageTest.kt | 69 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/DebugPage.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index d7dd16c..4a572a2 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -616,6 +616,7 @@ public class WebInterface implements SessionProvider { pageToadletRegistry.addPage(new EmptyImageTitlePage(this, loaders, templateRenderer)); pageToadletRegistry.addPage(new EmptyAlbumTitlePage(this, loaders, templateRenderer)); pageToadletRegistry.addPage(new DismissNotificationPage(this, loaders, templateRenderer)); + pageToadletRegistry.addPage(new DebugPage(this, loaders, templateRenderer)); pageToadletRegistry.addDebugPage(new MetricsPage(this, loaders, templateRenderer, metricRegistry)); pageToadletRegistry.addPage(loaders.loadStaticPage("css/", "/static/css/", "text/css")); pageToadletRegistry.addPage(loaders.loadStaticPage("javascript/", "/static/javascript/", "text/javascript")); diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/DebugPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/DebugPage.kt new file mode 100644 index 0000000..29a8f8c --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/DebugPage.kt @@ -0,0 +1,18 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.main.* +import net.pterodactylus.sone.web.* +import net.pterodactylus.sone.web.page.* +import net.pterodactylus.util.template.* +import javax.inject.* + +@ToadletPath("debug") +class DebugPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) : + SoneTemplatePage(webInterface, loaders, templateRenderer) { + + override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { + soneRequest.core.setDebug() + throw RedirectException("./") + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt new file mode 100644 index 0000000..3b2293e --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt @@ -0,0 +1,69 @@ +/** + * Sone - DebugPageTest.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.web.* +import net.pterodactylus.sone.web.WebTestUtils.* +import net.pterodactylus.sone.web.page.FreenetTemplatePage.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.Rule +import org.junit.rules.* +import org.junit.rules.ExpectedException.* +import org.mockito.Mockito.* +import kotlin.test.* + +class DebugPageTest : WebPageTest(::DebugPage) { + + @Rule + @JvmField + val expectedException: ExpectedException = none() + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("debug")) + } + + @Test + fun `page does not require login`() { + assertThat(page.requiresLogin(), equalTo(false)) + } + + @Test + fun `page can be created by dependency injection`() { + assertThat(baseInjector.getInstance(), notNullValue()) + } + + @Test + fun `get request activates debug mode`() { + try { + page.handleRequest(soneRequest, templateContext) + } catch (_: RedirectException) { + } + verify(core).setDebug() + } + + @Test + fun `get request redirects to index`() { + expectedException.expect(redirectsTo("./")) + page.handleRequest(soneRequest, templateContext) + } + +} + -- 2.7.4