From: David ‘Bombe’ Roden Date: Mon, 28 Nov 2016 17:43:20 +0000 (+0100) Subject: Convert and complete unit test and fix bug in page X-Git-Tag: 0.9.7^2~379 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fa90abc436a8f44118b601843b7f5a801c29ce79;p=Sone.git Convert and complete unit test and fix bug in page --- diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java b/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java index 7112b73..aa8bc8f 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java @@ -67,7 +67,7 @@ public class DeleteReplyPage extends SoneTemplatePage { throw new RedirectException(returnPage); } } - templateContext.set("reply", reply); + templateContext.set("reply", replyId); templateContext.set("returnPage", returnPage); } diff --git a/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java b/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java deleted file mode 100644 index 88c87a1..0000000 --- a/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.pterodactylus.sone.web; - -import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; -import static net.pterodactylus.util.web.Method.POST; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.data.PostReply; - -import com.google.common.base.Optional; -import org.junit.Test; - -/** - * Unit test for {@link DeleteReplyPage}. - * - * @author David ‘Bombe’ Roden - */ -public class DeleteReplyPageTest extends WebPageTest { - - private final DeleteReplyPage page = new DeleteReplyPage(template, webInterface); - - @Test - public void tryingToDeleteAReplyWithAnInvalidIdResultsInNoPermissionPage() throws Exception { - request("", POST); - when(httpRequest.getPartAsStringFailsafe(eq("reply"), anyInt())).thenReturn("id"); - when(webInterface.getCore().getPostReply("id")).thenReturn(Optional.absent()); - expectedException.expect(redirectsTo("noPermission.html")); - page.processTemplate(freenetRequest, templateContext); - } - -} diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 16610e4..b38121f 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -30,6 +30,7 @@ import net.pterodactylus.sone.core.UpdateChecker; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; import net.pterodactylus.sone.data.TemporaryImage; @@ -210,6 +211,10 @@ public abstract class WebPageTest { when(core.getPost(postId)).thenReturn(Optional.fromNullable(post)); } + protected void addPostReply(String postReplyId, PostReply postReply) { + when(core.getPostReply(postReplyId)).thenReturn(Optional.fromNullable(postReply)); + } + protected void addSone(String soneId, Sone sone) { sones.put(soneId, sone); } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt new file mode 100644 index 0000000..c2649d8 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/DeleteReplyPageTest.kt @@ -0,0 +1,108 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.web.WebTestUtils.redirectsTo +import net.pterodactylus.util.web.Method.POST +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mockito.`when` + +import net.pterodactylus.sone.data.PostReply + +import com.google.common.base.Optional +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.util.web.Method.GET +import org.hamcrest.MatcherAssert +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for [DeleteReplyPage]. + */ +class DeleteReplyPageTest : WebPageTest() { + + private val page = DeleteReplyPage(template, webInterface) + + private val sone = mock() + private val reply = mock() + + @Before + fun setupReply() { + whenever(sone.isLocal).thenReturn(true) + whenever(reply.sone).thenReturn(sone) + } + + @Test + fun `get request sets reply ID and return page in template context`() { + request("", GET) + addHttpRequestParameter("reply", "reply-id") + addHttpRequestParameter("returnPage", "return.html") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["reply"], equalTo("reply-id")) + assertThat(templateContext["returnPage"], equalTo("return.html")) + } + + @Test + fun `post request without any action sets reply ID and return page in template context`() { + request("", POST) + addPostReply("reply-id", reply) + addHttpRequestParameter("reply", "reply-id") + addHttpRequestParameter("returnPage", "return.html") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["reply"], equalTo("reply-id")) + assertThat(templateContext["returnPage"], equalTo("return.html")) + } + + @Test + fun `trying to delete a reply with an invalid ID results in no permission page`() { + request("", POST) + expectedException.expect(redirectsTo("noPermission.html")) + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `trying to delete a reply from a non-local sone results in no permission page`() { + request("", POST) + addHttpRequestParameter("reply", "reply-id") + whenever(sone.isLocal).thenReturn(false) + addPostReply("reply-id", reply) + expectedException.expect(redirectsTo("noPermission.html")) + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `confirming deletion of reply deletes the reply and redirects to return page`() { + request("", POST) + addPostReply("reply-id", reply) + addHttpRequestParameter("reply", "reply-id") + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("confirmDelete", "true") + expectedException.expect(redirectsTo("return.html")) + try { + page.processTemplate(freenetRequest, templateContext) + } finally { + verify(core).deleteReply(reply) + } + } + + @Test + fun `aborting deletion of reply redirects to return page`() { + request("", POST) + addPostReply("reply-id", reply) + addHttpRequestParameter("reply", "reply-id") + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("abortDelete", "true") + expectedException.expect(redirectsTo("return.html")) + try { + page.processTemplate(freenetRequest, templateContext) + } finally { + verify(core, never()).deleteReply(reply) + } + } + +}