X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FCreatePostPageTest.java;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FCreatePostPageTest.java;h=2a91086a3fc867a56b4c2ccd8ce6e9128bf3b1bc;hb=58ce723ebf56266792363b5a29c6349842a926aa;hp=0000000000000000000000000000000000000000;hpb=e3cfdd1ce056e81092f3dfd53cc7110999bd0540;p=Sone.git 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"); + } + } + +}