From c1767cbdd65e2ff62297676653250c0dcd890446 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 28 Nov 2016 21:47:35 +0100 Subject: [PATCH] Add unit test for mark as known page and slightly improve the logic --- .../pterodactylus/sone/web/MarkAsKnownPage.java | 2 +- .../pterodactylus/sone/web/MarkAsKnownPageTest.kt | 76 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java index dc1841b..57ea00f 100644 --- a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java +++ b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java @@ -77,7 +77,7 @@ public class MarkAsKnownPage extends SoneTemplatePage { continue; } webInterface.getCore().markReplyKnown(reply.get()); - } else if (type.equals("sone")) { + } else { Optional sone = webInterface.getCore().getSone(id); if (!sone.isPresent()) { continue; diff --git a/src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt new file mode 100644 index 0000000..e586e67 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt @@ -0,0 +1,76 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.web.WebTestUtils.redirectsTo +import org.junit.Test +import org.mockito.Mockito.verify + +/** + * Unit test for [MarkAsKnownPage]. + */ +class MarkAsKnownPageTest : WebPageTest() { + + private val page = MarkAsKnownPage(template, webInterface) + + @Test + fun `posts can be marked as known`() { + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("type", "post") + addHttpRequestParameter("id", "post1 post2 post3") + val posts = listOf(mock(), mock()) + addPost("post1", posts[0]) + addPost("post3", posts[1]) + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core).markPostKnown(posts[0]) + verify(core).markPostKnown(posts[1]) + } + } + + @Test + fun `replies can be marked as known`() { + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("type", "reply") + addHttpRequestParameter("id", "reply1 reply2 reply3") + val replies = listOf(mock(), mock()) + addPostReply("reply1", replies[0]) + addPostReply("reply3", replies[1]) + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core).markReplyKnown(replies[0]) + verify(core).markReplyKnown(replies[1]) + } + } + + @Test + fun `sones can be marked as known`() { + addHttpRequestParameter("returnPage", "return.html") + addHttpRequestParameter("type", "sone") + addHttpRequestParameter("id", "sone1 sone2 sone3") + val sones = listOf(mock(), mock()) + addSone("sone1", sones[0]) + addSone("sone3", sones[1]) + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core).markSoneKnown(sones[0]) + verify(core).markSoneKnown(sones[1]) + } + } + + @Test + fun `different type redirects to invalid page`() { + addHttpRequestParameter("type", "foo") + expectedException.expect(redirectsTo("invalid.html")) + page.handleRequest(freenetRequest, templateContext) + } + +} -- 2.7.4