From 0136315a9aeb2e583326721e103b1690d0e42fc3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 9 Feb 2017 19:43:51 +0100 Subject: [PATCH] Replace create post page and test with Kotlin version --- .../net/pterodactylus/sone/web/CreatePostPage.java | 79 ------------------ .../net/pterodactylus/sone/web/CreatePostPage.kt | 31 +++++++ .../pterodactylus/sone/web/CreatePostPageTest.java | 89 -------------------- .../pterodactylus/sone/web/CreatePostPageTest.kt | 95 ++++++++++++++++++++++ 4 files changed, 126 insertions(+), 168 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/CreatePostPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/CreatePostPage.kt delete mode 100644 src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/CreatePostPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java deleted file mode 100644 index ed478de..0000000 --- a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Sone - CreatePostPage.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 . - */ - -package net.pterodactylus.sone.web; - -import com.google.common.base.Optional; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.text.TextFilter; -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; - -/** - * This page lets the user create a new {@link Post}. - * - * @author David ‘Bombe’ Roden - */ -public class CreatePostPage extends SoneTemplatePage { - - /** - * Creates a new “create post” page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public CreatePostPage(Template template, WebInterface webInterface) { - super("createPost.html", template, "Page.CreatePost.Title", webInterface, true); - } - - // - // TEMPLATEPATH METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - if (request.getMethod() == Method.POST) { - String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim(); - if (text.length() != 0) { - String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43); - String recipientId = request.getHttpRequest().getPartAsStringFailsafe("recipient", 43); - Sone currentSone = getCurrentSone(request.getToadletContext()); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = currentSone; - } - Optional recipient = webInterface.getCore().getSone(recipientId); - text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - webInterface.getCore().createPost(sender, recipient, text); - throw new RedirectException(returnPage); - } - templateContext.set("errorTextEmpty", true); - } - templateContext.set("returnPage", returnPage); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/CreatePostPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/CreatePostPage.kt new file mode 100644 index 0000000..8287e13 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/CreatePostPage.kt @@ -0,0 +1,31 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.text.TextFilter +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 + +/** + * This page lets the user create a new [Post]. + */ +class CreatePostPage(template: Template, webInterface: WebInterface): + SoneTemplatePage("createPost.html", template, "Page.CreatePost.Title", webInterface, true) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + val returnPage = request.httpRequest.getPartAsStringFailsafe("returnPage", 256) + templateContext["returnPage"] = returnPage + if (request.method == POST) { + val text = request.httpRequest.getPartAsStringFailsafe("text", 65536).trim() + if (text == "") { + templateContext["errorTextEmpty"] = true + return + } + val sender = webInterface.core.getLocalSone(request.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(request.toadletContext) + val recipient = webInterface.core.getSone(request.httpRequest.getPartAsStringFailsafe("recipient", 43)) + webInterface.core.createPost(sender, recipient, TextFilter.filter(request.httpRequest.getHeader("Host"), text)) + throw RedirectException(returnPage) + } + } + +} diff --git a/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java deleted file mode 100644 index 2a91086..0000000 --- a/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java +++ /dev/null @@ -1,89 +0,0 @@ -package net.pterodactylus.sone.web; - -import static net.pterodactylus.util.web.Method.POST; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import net.pterodactylus.sone.data.Sone; - -import com.google.common.base.Optional; -import org.junit.Test; - -/** - * Unit test for {@link CreatePostPage}. - * - * @author David ‘Bombe’ Roden - */ -public class CreatePostPageTest extends WebPageTest { - - private final CreatePostPage page = new CreatePostPage(template, webInterface); - - @Test - public void pageReturnsCorrectPath() { - assertThat(page.getPath(), is("createPost.html")); - } - - @Test - public void returnPageIsSetInTemplateContext() throws Exception { - addHttpRequestParameter("returnPage", "returnPage.html"); - page.processTemplate(freenetRequest, templateContext); - assertThat(templateContext.get("returnPage"), is((Object) "returnPage.html")); - } - - @Test - public void postIsCreatedCorrectly() throws Exception { - addHttpRequestParameter("returnPage", "returnPage.html"); - addHttpRequestParameter("text", "post text"); - request("", POST); - expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); - try { - page.processTemplate(freenetRequest, templateContext); - } finally { - verify(core).createPost(currentSone, Optional.absent(), "post text"); - } - } - - @Test - public void creatingAnEmptyPostIsDenied() throws Exception { - addHttpRequestParameter("returnPage", "returnPage.html"); - addHttpRequestParameter("text", " "); - request("", POST); - page.processTemplate(freenetRequest, templateContext); - assertThat(templateContext.get("errorTextEmpty"), is((Object) true)); - } - - @Test - public void aSenderCanBeSelected() throws Exception { - addHttpRequestParameter("returnPage", "returnPage.html"); - addHttpRequestParameter("text", "post text"); - addHttpRequestParameter("sender", "sender-id"); - Sone sender = mock(Sone.class); - addLocalSone("sender-id", sender); - request("", POST); - expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); - try { - page.processTemplate(freenetRequest, templateContext); - } finally { - verify(core).createPost(sender, Optional.absent(), "post text"); - } - } - - @Test - public void aRecipientCanBeSelected() throws Exception { - addHttpRequestParameter("returnPage", "returnPage.html"); - addHttpRequestParameter("text", "post text"); - addHttpRequestParameter("recipient", "recipient-id"); - Sone recipient = mock(Sone.class); - addSone("recipient-id", recipient); - request("", POST); - expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); - try { - page.processTemplate(freenetRequest, templateContext); - } finally { - verify(core).createPost(currentSone, Optional.of(recipient), "post text"); - } - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/CreatePostPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/CreatePostPageTest.kt new file mode 100644 index 0000000..f1a120e --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/CreatePostPageTest.kt @@ -0,0 +1,95 @@ +package net.pterodactylus.sone.web + +import com.google.common.base.Optional.absent +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.asOptional +import net.pterodactylus.sone.test.mock +import net.pterodactylus.util.web.Method.POST +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import org.mockito.Mockito.verify + +/** + * Unit test for [CreatePostPage]. + */ +class CreatePostPageTest: WebPageTest() { + + private val page = CreatePostPage(template, webInterface) + + override fun getPage() = page + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("createPost.html")) + } + + @Test + fun `page requires login`() { + assertThat(page.requiresLogin(), equalTo(true)) + } + + @Test + fun `return page is set in template context`() { + addHttpRequestParameter("returnPage", "return.html") + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["returnPage"], equalTo("return.html")) + } + + @Test + fun `post is created correctly`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("text", "post text") + verifyRedirect("return.html") { + verify(core).createPost(currentSone, absent(), "post text") + } + } + + @Test + fun `creating an empty post is denied`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("text", " ") + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["errorTextEmpty"], equalTo(true)) + } + + @Test + fun `a sender can be selected`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("text", "post text") + addHttpRequestParameter("sender", "sender-id") + val sender = mock() + addLocalSone("sender-id", sender) + verifyRedirect("return.html") { + verify(core).createPost(sender, absent(), "post text") + } + } + + @Test + fun `a recipient can be selected`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("text", "post text") + addHttpRequestParameter("recipient", "recipient-id") + val recipient = mock() + addSone("recipient-id", recipient) + verifyRedirect("return.html") { + verify(core).createPost(currentSone, recipient.asOptional(), "post text") + } + } + + @Test + fun `text is filtered correctly`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("text", "post http://localhost:12345/KSK@foo text") + addHttpRequestHeader("Host", "localhost:12345") + verifyRedirect("return.html") { + verify(core).createPost(currentSone, absent(), "post KSK@foo text") + } + } + +} -- 2.7.4