✨ Add page to activate debug mode
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jul 2019 15:16:14 +0000 (17:16 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jul 2019 19:06:59 +0000 (21:06 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/pages/DebugPage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/DebugPageTest.kt [new file with mode: 0644]

index d7dd16c..4a572a2 100644 (file)
@@ -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.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css"));
                pageToadletRegistry.addPage(loaders.<FreenetRequest>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 (file)
index 0000000..29a8f8c
--- /dev/null
@@ -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 (file)
index 0000000..3b2293e
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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<DebugPage>(), 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)
+       }
+
+}
+