+++ /dev/null
-/*
- * 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;
- }
-
-}
--- /dev/null
+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
+ }
+
+}
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 {
}
@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))
}
@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") {