Replace create reply page and test with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 9 Feb 2017 21:15:02 +0000 (22:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 9 Feb 2017 21:15:02 +0000 (22:15 +0100)
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java [deleted file]
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java [deleted file]
src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.kt [new file with mode: 0644]

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 (file)
index 0bd5217..0000000
+++ /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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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> 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 (file)
index 0000000..6e003f0
--- /dev/null
@@ -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 (file)
index 996f5c0..0000000
+++ /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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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.<Boolean>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.<String>get("postId", String.class), is("post-id"));
-               assertThat(templateContext.<String>get("text", String.class), is(text));
-               assertThat(templateContext.<String>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 (file)
index 0000000..dba49a8
--- /dev/null
@@ -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<Post>().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<Post>().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<Sone>().apply { addLocalSone("sender-id", this) }
+               val post = mock<Post>().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<Any>(true))
+               assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
+               assertThat(templateContext["postId"], equalTo<Any>("post-id"))
+               assertThat(templateContext["text"], equalTo<Any>(""))
+       }
+
+       @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<Any>("return.html"))
+               assertThat(templateContext["postId"], equalTo<Any>("post-id"))
+               assertThat(templateContext["text"], equalTo<Any>("new text"))
+       }
+
+}