From 02bfd3cfcf4c78c4e7f561ef68dece9becaf8171 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 24 Oct 2016 18:56:07 +0200 Subject: [PATCH] Add test for CreateReplyPage --- .../sone/web/CreateReplyPageTest.java | 107 +++++++++++++++++++++ .../net/pterodactylus/sone/web/WebPageTest.java | 1 + 2 files changed, 108 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/web/CreateReplyPageTest.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 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"); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 2717174..bb6282c 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -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.absent()); + when(core.getPost(anyString())).thenReturn(Optional.absent()); } @Before -- 2.7.4