From: David ‘Bombe’ Roden Date: Thu, 20 Oct 2016 09:05:19 +0000 (+0200) Subject: Add test for CreatePostPage X-Git-Tag: 0.9.6^2~8 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=58ce723ebf56266792363b5a29c6349842a926aa;p=Sone.git Add test for CreatePostPage --- diff --git a/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java new file mode 100644 index 0000000..2a91086 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java @@ -0,0 +1,89 @@ +package net.pterodactylus.sone.web; + +import static net.pterodactylus.util.web.Method.POST; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import net.pterodactylus.sone.data.Sone; + +import com.google.common.base.Optional; +import org.junit.Test; + +/** + * Unit test for {@link CreatePostPage}. + * + * @author David ‘Bombe’ Roden + */ +public class CreatePostPageTest extends WebPageTest { + + private final CreatePostPage page = new CreatePostPage(template, webInterface); + + @Test + public void pageReturnsCorrectPath() { + assertThat(page.getPath(), is("createPost.html")); + } + + @Test + public void returnPageIsSetInTemplateContext() throws Exception { + addHttpRequestParameter("returnPage", "returnPage.html"); + page.processTemplate(freenetRequest, templateContext); + assertThat(templateContext.get("returnPage"), is((Object) "returnPage.html")); + } + + @Test + public void postIsCreatedCorrectly() throws Exception { + addHttpRequestParameter("returnPage", "returnPage.html"); + addHttpRequestParameter("text", "post text"); + request("", POST); + expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(core).createPost(currentSone, Optional.absent(), "post text"); + } + } + + @Test + public void creatingAnEmptyPostIsDenied() throws Exception { + addHttpRequestParameter("returnPage", "returnPage.html"); + addHttpRequestParameter("text", " "); + request("", POST); + page.processTemplate(freenetRequest, templateContext); + assertThat(templateContext.get("errorTextEmpty"), is((Object) true)); + } + + @Test + public void aSenderCanBeSelected() throws Exception { + addHttpRequestParameter("returnPage", "returnPage.html"); + addHttpRequestParameter("text", "post text"); + addHttpRequestParameter("sender", "sender-id"); + Sone sender = mock(Sone.class); + addLocalSone("sender-id", sender); + request("", POST); + expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(core).createPost(sender, Optional.absent(), "post text"); + } + } + + @Test + public void aRecipientCanBeSelected() throws Exception { + addHttpRequestParameter("returnPage", "returnPage.html"); + addHttpRequestParameter("text", "post text"); + addHttpRequestParameter("recipient", "recipient-id"); + Sone recipient = mock(Sone.class); + addSone("recipient-id", recipient); + request("", POST); + expectedException.expect(WebTestUtils.redirectsTo("returnPage.html")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(core).createPost(currentSone, Optional.of(recipient), "post text"); + } + } + +} diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index f464eac..2717174 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -70,6 +70,8 @@ public abstract class WebPageTest { public final void setupCore() { UpdateChecker updateChecker = mock(UpdateChecker.class); when(core.getUpdateChecker()).thenReturn(updateChecker); + when(core.getLocalSone(anyString())).thenReturn(null); + when(core.getSone(anyString())).thenReturn(Optional.absent()); } @Before @@ -102,4 +104,12 @@ public abstract class WebPageTest { when(core.getPost(postId)).thenReturn(Optional.fromNullable(post)); } + protected void addSone(String soneId, Sone sone) { + when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone)); + } + + protected void addLocalSone(String soneId, Sone sone) { + when(core.getLocalSone(eq(soneId))).thenReturn(sone); + } + }