Add test for CreateReplyPage
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 24 Oct 2016 16:56:07 +0000 (18:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 24 Oct 2016 17:19:01 +0000 (19:19 +0200)
src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/WebPageTest.java

diff --git a/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java
new file mode 100644 (file)
index 0000000..996f5c0
--- /dev/null
@@ -0,0 +1,107 @@
+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");
+       }
+
+}
index 2717174..bb6282c 100644 (file)
@@ -72,6 +72,7 @@ public abstract class WebPageTest {
                when(core.getUpdateChecker()).thenReturn(updateChecker);
                when(core.getLocalSone(anyString())).thenReturn(null);
                when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
+               when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
        }
 
        @Before