Replace login page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 14 Feb 2017 19:25:05 +0000 (20:25 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 14 Feb 2017 19:25:05 +0000 (20:25 +0100)
src/main/java/net/pterodactylus/sone/web/LoginPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/LoginPage.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/LoginPageTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/web/LoginPage.java b/src/main/java/net/pterodactylus/sone/web/LoginPage.java
deleted file mode 100644 (file)
index 65ab0cf..0000000
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Sone - LoginPage.java - Copyright © 2010–2016 David 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;
-
-import static java.util.logging.Logger.getLogger;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.logging.Logger;
-
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.freenet.wot.OwnIdentity;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.web.Method;
-import freenet.clients.http.ToadletContext;
-
-/**
- * The login page manages logging the user in.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class LoginPage extends SoneTemplatePage {
-
-       /** The logger. */
-       @SuppressWarnings("unused")
-       private static final Logger logger = getLogger(LoginPage.class.getName());
-
-       /**
-        * Creates a new login page.
-        *
-        * @param template
-        *            The template to render
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public LoginPage(Template template, WebInterface webInterface) {
-               super("login.html", template, "Page.Login.Title", webInterface, false);
-       }
-
-       //
-       // TEMPLATEPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               /* get all own identities. */
-               List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
-               Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
-               templateContext.set("sones", localSones);
-               if (request.getMethod() == Method.POST) {
-                       String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone-id", 100);
-                       Sone selectedSone = webInterface.getCore().getLocalSone(soneId);
-                       if (selectedSone != null) {
-                               setCurrentSone(request.getToadletContext(), selectedSone);
-                               String target = request.getHttpRequest().getParam("target");
-                               if ((target == null) || (target.length() == 0)) {
-                                       target = "index.html";
-                               }
-                               throw new RedirectException(target);
-                       }
-               }
-               List<OwnIdentity> ownIdentitiesWithoutSone = CreateSonePage.getOwnIdentitiesWithoutSone(webInterface.getCore());
-               templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected String getRedirectTarget(FreenetRequest request) {
-               if (getCurrentSoneWithoutCreatingSession(request.getToadletContext()) != null) {
-                       return "index.html";
-               }
-               return null;
-       }
-
-       //
-       // SONETEMPLATEPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isEnabled(ToadletContext toadletContext) {
-               if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
-                       return false;
-               }
-               return getCurrentSoneWithoutCreatingSession(toadletContext) == null;
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/LoginPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/LoginPage.kt
new file mode 100644 (file)
index 0000000..a37431a
--- /dev/null
@@ -0,0 +1,37 @@
+package net.pterodactylus.sone.web
+
+import freenet.clients.http.ToadletContext
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+import net.pterodactylus.util.web.Method.POST
+
+/**
+ * The login page lets the user log in.
+ */
+class LoginPage(template: Template, webInterface: WebInterface):
+               SoneTemplatePage("login.html", template, "Page.Login.Title", webInterface) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+               if (request.method == POST) {
+                       val soneId = request.httpRequest.getPartAsStringFailsafe("sone-id", 43)
+                       webInterface.core.getLocalSone(soneId)?.let { sone ->
+                               setCurrentSone(request.toadletContext, sone)
+                               val target = if (request.httpRequest.isParameterSet("target")) request.httpRequest.getPartAsStringFailsafe("target", 256) else "index.html"
+                               throw RedirectException(target)
+                       }
+               }
+               templateContext["sones"] = webInterface.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
+               templateContext["identitiesWithoutSone"] = webInterface.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}" }
+       }
+
+       override public fun getRedirectTarget(request: FreenetRequest) =
+                       getCurrentSone(request.toadletContext)?.let { "index.html" }
+
+       override fun isEnabled(toadletContext: ToadletContext) = when {
+               webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
+               else -> getCurrentSoneWithoutCreatingSession(toadletContext) == null
+       }
+
+}
index 8e77945..2bd2680 100644 (file)
@@ -164,6 +164,13 @@ public abstract class WebPageTest {
                                return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).iterator().next() : invocation.<String>getArgument(1);
                        }
                });
+               when(httpRequest.isParameterSet(anyString())).thenAnswer(new Answer<Boolean>() {
+                       @Override
+                       public Boolean answer(InvocationOnMock invocation) throws Throwable {
+                               return requestParameters.containsKey(invocation.<String>getArgument(0)) &&
+                                               requestParameters.get(invocation.<String>getArgument(0)).iterator().next() != null;
+                       }
+               });
                when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
                        @Override
                        public Boolean answer(InvocationOnMock invocation) throws Throwable {
index a4dea43..1dbe3cd 100644 (file)
@@ -49,11 +49,28 @@ class LoginPageTest : WebPageTest() {
        }
 
        @Test
+       fun `page returns correct path`() {
+           assertThat(page.path, equalTo("login.html"))
+       }
+
+       @Test
+       fun `page does not require login`() {
+           assertThat(page.requiresLogin(), equalTo(false))
+       }
+
+       @Test
        @Suppress("UNCHECKED_CAST")
-       fun `get request stores sone and identities without sones in template context`() {
+       fun `get request stores sones in template context`() {
                request("", GET)
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
+       }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `get request stores identities without sones in template context`() {
+               request("", GET)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
        }
 
@@ -61,13 +78,13 @@ class LoginPageTest : WebPageTest() {
        @Suppress("UNCHECKED_CAST")
        fun `post request with invalid sone sets sones and identities without sone in template context`() {
                request("", POST)
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
                assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
        }
 
        @Test
-       fun `post request with valid sone and redirects to index page`() {
+       fun `post request with valid sone logs in the sone and redirects to index page`() {
                request("", POST)
                addHttpRequestParameter("sone-id", "sone2")
                verifyRedirect("index.html") {