Add unit test for unbookmark page, fix bug in page implementation
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Jan 2017 20:49:19 +0000 (21:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Jan 2017 20:49:19 +0000 (21:49 +0100)
src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java
src/test/kotlin/net/pterodactylus/sone/web/UnbookmarkPageTest.kt [new file with mode: 0644]

index 8edf2da..9cc266e 100644 (file)
@@ -66,7 +66,7 @@ public class UnbookmarkPage extends SoneTemplatePage {
                if (id.equals("allNotLoaded")) {
                        Set<Post> 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 (file)
index 0000000..d599f19
--- /dev/null
@@ -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<Post>().apply { whenever(isLoaded).thenReturn(true) }
+               val loadedPost2 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
+               val notLoadedPost1 = mock<Post>()
+               val notLoadedPost2 = mock<Post>()
+               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<Post>().apply { whenever(isLoaded).thenReturn(true) }
+               addPost("post-id", post)
+               verifyRedirect("return.html") {
+                       verify(core).unbookmarkPost(post)
+               }
+       }
+
+}