From: David ‘Bombe’ Roden Date: Sat, 4 May 2019 15:53:01 +0000 (+0200) Subject: ♻️ Add L10n to FreenetRequest, WebInterface to SoneRequest X-Git-Tag: v79^2~56 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cd1f93dc057bdc6e89baeb41db0f749f2287224e;p=Sone.git ♻️ Add L10n to FreenetRequest, WebInterface to SoneRequest --- diff --git a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java index 5b310a1..6209d32 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java +++ b/src/main/java/net/pterodactylus/sone/web/page/PageToadlet.java @@ -33,6 +33,7 @@ import freenet.clients.http.LinkFilterExceptedToadlet; import freenet.clients.http.Toadlet; import freenet.clients.http.ToadletContext; import freenet.clients.http.ToadletContextClosedException; +import freenet.l10n.NodeL10n; import freenet.support.MultiValueTable; import freenet.support.api.HTTPRequest; @@ -102,7 +103,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * if the toadlet context is closed */ public void handleMethodGET(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext)); + handleRequest(new FreenetRequest(uri, Method.GET, httpRequest, toadletContext, NodeL10n.getBase())); } /** @@ -120,7 +121,7 @@ public class PageToadlet extends Toadlet implements LinkEnabledCallback, LinkFil * if the toadlet context is closed */ public void handleMethodPOST(URI uri, HTTPRequest httpRequest, ToadletContext toadletContext) throws IOException, ToadletContextClosedException { - handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext)); + handleRequest(new FreenetRequest(uri, Method.POST, httpRequest, toadletContext, NodeL10n.getBase())); } /** diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt index 914d70f..03985a8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt @@ -18,8 +18,13 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.util.web.* import java.net.* -open class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext) : Request(uri, method) +open class FreenetRequest(uri: URI, method: Method, + val httpRequest: HTTPRequest, + val toadletContext: ToadletContext, + val l10n: BaseL10n +) : Request(uri, method) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt index 1cc6911..76b4843 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt @@ -1,11 +1,16 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.web.* import net.pterodactylus.util.web.* import java.net.* -class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, val core: Core) : FreenetRequest(uri, method, httpRequest, toadletContext) +class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, l10n: BaseL10n, + val core: Core, + val webInterface: WebInterface +) : FreenetRequest(uri, method, httpRequest, toadletContext, l10n) -fun FreenetRequest.toSoneRequest(core: Core) = SoneRequest(uri, method, httpRequest, toadletContext, core) +fun FreenetRequest.toSoneRequest(core: Core, webInterface: WebInterface) = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index 4290c3a..1ff35a8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -1,6 +1,6 @@ package net.pterodactylus.sone.web.pages -import freenet.clients.http.ToadletContext +import freenet.clients.http.* import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.main.SonePlugin import net.pterodactylus.sone.utils.emptyToNull @@ -10,6 +10,7 @@ import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.Template import net.pterodactylus.util.template.TemplateContext +import net.pterodactylus.util.web.* import java.net.URLEncoder /** @@ -35,7 +36,9 @@ open class SoneTemplatePage @JvmOverloads constructor( fun requiresLogin() = requiresLogin - override public fun getPageTitle(freenetRequest: FreenetRequest) = pageTitle(freenetRequest) + override public fun getPageTitle(freenetRequest: FreenetRequest) = getPageTitle(freenetRequest.toSoneRequest(core, webInterface)) + + open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest) override public fun getStyleSheets() = listOf("css/sone.css") @@ -69,7 +72,7 @@ open class SoneTemplatePage @JvmOverloads constructor( } internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - handleRequest(freenetRequest.toSoneRequest(core), templateContext) + handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext) } open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { @@ -88,9 +91,12 @@ open class SoneTemplatePage @JvmOverloads constructor( private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") - override fun isEnabled(toadletContext: ToadletContext) = when { - requiresLogin && getCurrentSone(toadletContext) == null -> false - core.preferences.requireFullAccess && !toadletContext.isAllowedFullAccess -> false + override fun isEnabled(toadletContext: ToadletContext) = + isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, core, webInterface)) + + open fun isEnabled(soneRequest: SoneRequest) = when { + requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false + core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false else -> true } diff --git a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java index e85c131..6eade58 100644 --- a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java @@ -22,6 +22,7 @@ import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; import freenet.clients.http.ToadletContext; +import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; import com.google.common.base.Charsets; @@ -39,6 +40,7 @@ public class DebugLoadersTest { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + private final BaseL10n l10n = mock(BaseL10n.class); private final StringWriter stringWriter = new StringWriter(); private final TemplateContext templateContext = new TemplateContext(); private Loaders loaders; @@ -68,7 +70,7 @@ public class DebugLoadersTest { Method method = Method.GET; HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); page.handleRequest(request, response); diff --git a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java index 5c71b73..d5e5522 100644 --- a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java @@ -22,6 +22,7 @@ import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; import freenet.clients.http.ToadletContext; +import freenet.l10n.BaseL10n; import freenet.support.api.HTTPRequest; import org.junit.Test; @@ -31,6 +32,7 @@ import org.junit.Test; */ public class DefaultLoadersTest { + private final BaseL10n l10n = mock(BaseL10n.class); private final Loaders loaders = new DefaultLoaders(); private final StringWriter stringWriter = new StringWriter(); private final TemplateContext templateContext = new TemplateContext(); @@ -49,7 +51,7 @@ public class DefaultLoadersTest { Method method = Method.GET; HTTPRequest httpRequest = mock(HTTPRequest.class); ToadletContext toadletContext = mock(ToadletContext.class); - FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext); + FreenetRequest request = new FreenetRequest(uri, method, httpRequest, toadletContext, l10n); OutputStream outputStream = new ByteArrayOutputStream(); Response response = new Response(outputStream); staticPage.handleRequest(request, response); diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt index 1e3caab..391991c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetRequestTest.kt @@ -1,12 +1,15 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.l10n.* import freenet.support.api.* +import net.pterodactylus.sone.test.* import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* import org.mockito.Mockito.* +import org.mockito.Mockito.mock import java.net.* class FreenetRequestTest { @@ -15,7 +18,8 @@ class FreenetRequestTest { private val method = Method.GET private val httpRequest = mock(HTTPRequest::class.java) private val toadletContext = mock(ToadletContext::class.java) - private val request = FreenetRequest(uri, method, httpRequest, toadletContext) + private val l10n = mock() + private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n) @Test fun `uri is retained correctly`() { @@ -37,4 +41,9 @@ class FreenetRequestTest { assertThat(request.toadletContext, equalTo(toadletContext)) } + @Test + fun `l10n is retained correctly`() { + assertThat(request.l10n, equalTo(l10n)) + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt index dc2b4a5..849d4ed 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt @@ -1,9 +1,11 @@ package net.pterodactylus.sone.web.page import freenet.clients.http.* +import freenet.l10n.* import freenet.support.api.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.web.* import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* @@ -17,8 +19,10 @@ class SoneRequestTest { private val method = Method.GET private val httpRequest = Mockito.mock(HTTPRequest::class.java) private val toadletContext = Mockito.mock(ToadletContext::class.java) + private val l10n = mock() private val core = mock() - private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, core) + private val webInterface = mock() + private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, l10n, core, webInterface) @Test fun `freenet request properties are retained correctly`() { @@ -26,6 +30,7 @@ class SoneRequestTest { assertThat(soneRequest.method, equalTo(method)) assertThat(soneRequest.httpRequest, equalTo(httpRequest)) assertThat(soneRequest.toadletContext, equalTo(toadletContext)) + assertThat(soneRequest.l10n, equalTo(l10n)) } @Test @@ -34,14 +39,21 @@ class SoneRequestTest { } @Test + fun `web interface is retained correctly`() { + assertThat(soneRequest.webInterface, sameInstance(webInterface)) + } + + @Test fun `freenet request is wrapped correctly`() { - val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext) - val wrappedSoneRequest = freenetRequest.toSoneRequest(core) + val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext, l10n) + val wrappedSoneRequest = freenetRequest.toSoneRequest(core, webInterface) assertThat(wrappedSoneRequest.uri, equalTo(uri)) assertThat(wrappedSoneRequest.method, equalTo(method)) assertThat(wrappedSoneRequest.httpRequest, equalTo(httpRequest)) assertThat(wrappedSoneRequest.toadletContext, equalTo(toadletContext)) + assertThat(wrappedSoneRequest.l10n, equalTo(l10n)) assertThat(wrappedSoneRequest.core, sameInstance(core)) + assertThat(wrappedSoneRequest.webInterface, sameInstance(webInterface)) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt index 4f72902..d654b09 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest.kt @@ -20,7 +20,7 @@ import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.utils.asList import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface -import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.sone.web.page.* import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.Template @@ -55,6 +55,10 @@ open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePag val page by lazy { pageSupplier(template, webInterface) } val httpRequest = mock() val freenetRequest = mock() + init { + whenever(freenetRequest.l10n).thenReturn(l10n) + whenever(freenetRequest.uri).thenReturn(mock()) + } val templateContext = TemplateContext() val toadletContext = deepMock() val responseContent = ByteArrayOutputStream()