From: David ‘Bombe’ Roden Date: Thu, 9 Feb 2017 21:15:02 +0000 (+0100) Subject: Replace create reply page and test with Kotlin version X-Git-Tag: 0.9.7^2~299 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=cfa969d6aa8dc49b2d6487c1ccaaeb3058e86ba9 Replace create reply page and test with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java deleted file mode 100644 index 0bd5217..0000000 --- a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Sone - CreateReplyPage.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 post a reply to a post. - * - * @author David ‘Bombe’ Roden - */ -public class CreateReplyPage extends SoneTemplatePage { - - /** - * Creates a new “create reply” page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public CreateReplyPage(Template template, WebInterface webInterface) { - super("createReply.html", template, "Page.CreateReply.Title", webInterface, true); - } - - // - // TEMPLATEPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36); - String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim(); - String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - if (request.getMethod() == Method.POST) { - Optional post = webInterface.getCore().getPost(postId); - if (!post.isPresent()) { - throw new RedirectException("noPermission.html"); - } - if (text.length() > 0) { - String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = getCurrentSone(request.getToadletContext()); - } - text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - webInterface.getCore().createReply(sender, post.get(), text); - throw new RedirectException(returnPage); - } - templateContext.set("errorTextEmpty", true); - } - templateContext.set("postId", postId); - templateContext.set("text", text); - templateContext.set("returnPage", returnPage); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.kt b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.kt new file mode 100644 index 0000000..6e003f0 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.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 post a reply to a post. + */ +class CreateReplyPage(template: Template, webInterface: WebInterface): + SoneTemplatePage("createReply.html", template, "Page.CreateReply.Title", webInterface, true) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + val postId = request.httpRequest.getPartAsStringFailsafe("post", 36).apply { templateContext["postId"] = this } + val text = request.httpRequest.getPartAsStringFailsafe("text", 65536).trim().apply { templateContext["text"] = this } + val returnPage = request.httpRequest.getPartAsStringFailsafe("returnPage", 256).apply { templateContext["returnPage"] = this } + if (request.method == POST) { + if (text == "") { + templateContext["errorTextEmpty"] = true + return + } + val post = webInterface.core.getPost(postId).orNull() ?: throw RedirectException("noPermission.html") + val sender = webInterface.core.getLocalSone(request.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(request.toadletContext) + webInterface.core.createReply(sender, post, TextFilter.filter(request.httpRequest.getHeader("Host"), text)) + throw RedirectException(returnPage) + } + } + +} diff --git a/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java deleted file mode 100644 index 996f5c0..0000000 --- a/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package net.pterodactylus.sone.web; - -import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; -import static net.pterodactylus.util.web.Method.GET; -import static net.pterodactylus.util.web.Method.POST; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; - -import org.junit.Test; - -/** - * Unit test for {@link CreateReplyPageTest}. - * - * @author David ‘Bombe’ Roden - */ -public class CreateReplyPageTest extends WebPageTest { - - private final CreateReplyPage page = new CreateReplyPage(template, webInterface); - - @Test - public void pageReturnsCorrectPath() { - assertThat(page.getPath(), is("createReply.html")); - } - - @Test - public void replyIsCreatedCorrectly() throws Exception { - request("", POST); - addHttpRequestParameter("post", "post-id"); - addHttpRequestParameter("text", "some text"); - addHttpRequestParameter("returnPage", "returnPage.html"); - Post post = mock(Post.class); - addPost("post-id", post); - expectedException.expect(redirectsTo("returnPage.html")); - try { - page.processTemplate(freenetRequest, templateContext); - } finally { - verify(core).createReply(currentSone, post, "some text"); - } - } - - @Test - public void replyIsCreatedWithCorrectSender() throws Exception { - request("", POST); - addHttpRequestParameter("post", "post-id"); - addHttpRequestParameter("text", "some text"); - addHttpRequestParameter("returnPage", "returnPage.html"); - addHttpRequestParameter("sender", "sender-id"); - Sone sender = mock(Sone.class); - addLocalSone("sender-id", sender); - Post post = mock(Post.class); - addPost("post-id", post); - expectedException.expect(redirectsTo("returnPage.html")); - try { - page.processTemplate(freenetRequest, templateContext); - } finally { - verify(core).createReply(sender, post, "some text"); - } - } - - @Test - public void emptyTextSetsVariableInTemplateContext() throws Exception { - request("", POST); - addPost("post-id", mock(Post.class)); - addHttpRequestParameter("post", "post-id"); - addHttpRequestParameter("text", " "); - addHttpRequestParameter("returnPage", "returnPage.html"); - page.processTemplate(freenetRequest, templateContext); - assertThat(templateContext.get("errorTextEmpty", Boolean.class), is(true)); - verifyParametersAreCopied(""); - verify(core, never()).createReply(any(Sone.class), any(Post.class), anyString()); - } - - private void verifyParametersAreCopied(String text) { - assertThat(templateContext.get("postId", String.class), is("post-id")); - assertThat(templateContext.get("text", String.class), is(text)); - assertThat(templateContext.get("returnPage", String.class), is("returnPage.html")); - } - - @Test - public void userIsRedirectIfPostDoesNotExist() throws Exception { - request("", POST); - addHttpRequestParameter("post", "post-id"); - addHttpRequestParameter("text", "some text"); - addHttpRequestParameter("returnPage", "returnPage.html"); - expectedException.expect(redirectsTo("noPermission.html")); - page.processTemplate(freenetRequest, templateContext); - } - - @Test - public void getRequestServesTemplateAndStoresParameters() throws Exception { - request("", GET); - addHttpRequestParameter("post", "post-id"); - addHttpRequestParameter("text", "some text"); - addHttpRequestParameter("returnPage", "returnPage.html"); - page.processTemplate(freenetRequest, templateContext); - verifyParametersAreCopied("some text"); - } - -} diff --git a/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.kt b/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.kt new file mode 100644 index 0000000..dba49a8 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.kt @@ -0,0 +1,102 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.Sone +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 [CreateReplyPage]. + */ +class CreateReplyPageTest: WebPageTest() { + + private val page = CreateReplyPage(template, webInterface) + override fun getPage() = page + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("createReply.html")) + } + + @Test + fun `page requires login`() { + assertThat(page.requiresLogin(), equalTo(true)) + } + + @Test + fun `reply is created correctly`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", "new text") + val post = mock().apply { addPost("post-id", this) } + verifyRedirect("return.html") { + verify(core).createReply(currentSone, post, "new text") + } + } + + @Test + fun `reply is filtered`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", "new http://localhost:12345/KSK@foo text") + addHttpRequestHeader("Host", "localhost:12345") + val post = mock().apply { addPost("post-id", this) } + verifyRedirect("return.html") { + verify(core).createReply(currentSone, post, "new KSK@foo text") + } + } + + @Test + fun `reply is created with correct sender`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", "new text") + addHttpRequestParameter("sender", "sender-id") + val sender = mock().apply { addLocalSone("sender-id", this) } + val post = mock().apply { addPost("post-id", this) } + verifyRedirect("return.html") { + verify(core).createReply(sender, post, "new text") + } + } + + @Test + fun `empty text sets parameters in template contexty`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", " ") + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["errorTextEmpty"], equalTo(true)) + assertThat(templateContext["returnPage"], equalTo("return.html")) + assertThat(templateContext["postId"], equalTo("post-id")) + assertThat(templateContext["text"], equalTo("")) + } + + @Test + fun `user is redirected to no permissions page if post does not exist`() { + request("", POST) + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", "new text") + verifyRedirect("noPermission.html") + } + + @Test + fun `get request stores parameters in template context`() { + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("text", "new text") + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["returnPage"], equalTo("return.html")) + assertThat(templateContext["postId"], equalTo("post-id")) + assertThat(templateContext["text"], equalTo("new text")) + } + +}