X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FCreateReplyPageTest.java;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FCreateReplyPageTest.java;h=996f5c05c406c5357f83c677dac0d1db086c5687;hb=02bfd3cfcf4c78c4e7f561ef68dece9becaf8171;hp=0000000000000000000000000000000000000000;hpb=3b2da19b74cfda3b66d98fcec4639ab58207adee;p=Sone.git
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
index 0000000..996f5c0
--- /dev/null
+++ b/src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.java
@@ -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 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");
+ }
+
+}