Replace delete post page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / UnbookmarkPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.util.web.Method.POST
7 import org.junit.Test
8 import org.mockito.Mockito.any
9 import org.mockito.Mockito.never
10 import org.mockito.Mockito.verify
11
12 /**
13  * Unit test for [UnbookmarkPage].
14  */
15 class UnbookmarkPageTest : WebPageTest() {
16
17         private val page = UnbookmarkPage(template, webInterface)
18
19         override fun getPage() = page
20
21         @Test
22         fun `get request does not redirect`() {
23                 page.handleRequest(freenetRequest, templateContext)
24         }
25
26         @Test
27         fun `get request with all-not-loaded parameter unloads all not loaded posts and redirects to bookmarks`() {
28                 addHttpRequestParameter("post", "allNotLoaded")
29                 val loadedPost1 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
30                 val loadedPost2 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
31                 val notLoadedPost1 = mock<Post>()
32                 val notLoadedPost2 = mock<Post>()
33                 whenever(core.bookmarkedPosts).thenReturn(setOf(loadedPost1, loadedPost2, notLoadedPost1, notLoadedPost2))
34                 verifyRedirect("bookmarks.html") {
35                         verify(core).unbookmarkPost(notLoadedPost1)
36                         verify(core).unbookmarkPost(notLoadedPost2)
37                         verify(core, never()).unbookmarkPost(loadedPost1)
38                         verify(core, never()).unbookmarkPost(loadedPost2)
39                 }
40         }
41
42         @Test
43         fun `post request does not unbookmark not-present post but redirects to return page`() {
44                 request("", POST)
45                 addHttpRequestParameter("post", "post-id")
46                 addHttpRequestParameter("returnPage", "return.html")
47                 verifyRedirect("return.html") {
48                         verify(core, never()).unbookmarkPost(any())
49                 }
50         }
51
52         @Test
53         fun `post request unbookmarks present post and redirects to return page`() {
54                 request("", POST)
55                 addHttpRequestParameter("post", "post-id")
56                 addHttpRequestParameter("returnPage", "return.html")
57                 val post = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
58                 addPost("post-id", post)
59                 verifyRedirect("return.html") {
60                         verify(core).unbookmarkPost(post)
61                 }
62         }
63
64 }