From 49493113267246fe56f51dc5bdeb48f349289ab8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Jan 2017 21:49:19 +0100 Subject: [PATCH] Add unit test for unbookmark page, fix bug in page implementation --- .../net/pterodactylus/sone/web/UnbookmarkPage.java | 2 +- .../pterodactylus/sone/web/UnbookmarkPageTest.kt | 64 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/UnbookmarkPageTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java b/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java index 8edf2da..9cc266e 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java @@ -66,7 +66,7 @@ public class UnbookmarkPage extends SoneTemplatePage { if (id.equals("allNotLoaded")) { Set posts = webInterface.getCore().getBookmarkedPosts(); for (Post post : posts) { - if (post.isLoaded()) { + if (!post.isLoaded()) { webInterface.getCore().unbookmarkPost(post); } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/UnbookmarkPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/UnbookmarkPageTest.kt new file mode 100644 index 0000000..d599f19 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/UnbookmarkPageTest.kt @@ -0,0 +1,64 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.util.web.Method.POST +import org.junit.Test +import org.mockito.Mockito.any +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for [UnbookmarkPage]. + */ +class UnbookmarkPageTest : WebPageTest() { + + private val page = UnbookmarkPage(template, webInterface) + + override fun getPage() = page + + @Test + fun `get request does not redirect`() { + page.handleRequest(freenetRequest, templateContext) + } + + @Test + fun `get request with all-not-loaded parameter unloads all not loaded posts and redirects to bookmarks`() { + addHttpRequestParameter("post", "allNotLoaded") + val loadedPost1 = mock().apply { whenever(isLoaded).thenReturn(true) } + val loadedPost2 = mock().apply { whenever(isLoaded).thenReturn(true) } + val notLoadedPost1 = mock() + val notLoadedPost2 = mock() + whenever(core.bookmarkedPosts).thenReturn(setOf(loadedPost1, loadedPost2, notLoadedPost1, notLoadedPost2)) + verifyRedirect("bookmarks.html") { + verify(core).unbookmarkPost(notLoadedPost1) + verify(core).unbookmarkPost(notLoadedPost2) + verify(core, never()).unbookmarkPost(loadedPost1) + verify(core, never()).unbookmarkPost(loadedPost2) + } + } + + @Test + fun `post request does not unbookmark not-present post but redirects to return page`() { + request("", POST) + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("returnPage", "return.html") + verifyRedirect("return.html") { + verify(core, never()).unbookmarkPost(any()) + } + } + + @Test + fun `post request unbookmarks present post and redirects to return page`() { + request("", POST) + addHttpRequestParameter("post", "post-id") + addHttpRequestParameter("returnPage", "return.html") + val post = mock().apply { whenever(isLoaded).thenReturn(true) } + addPost("post-id", post) + verifyRedirect("return.html") { + verify(core).unbookmarkPost(post) + } + } + +} -- 2.7.4