Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / UnbookmarkPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.capture
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.contains
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Test
12 import org.mockito.Mockito.any
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.times
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [UnbookmarkPage].
19  */
20 class UnbookmarkPageTest: WebPageTest(::UnbookmarkPage) {
21
22         @Test
23         fun `page returns correct path`() {
24                 assertThat(page.path, equalTo("unbookmark.html"))
25         }
26
27         @Test
28         fun `page does not require login`() {
29                 assertThat(page.requiresLogin(), equalTo(false))
30         }
31
32         @Test
33         fun `page returns correct title`() {
34                 addTranslation("Page.Unbookmark.Title", "unbookmark page title")
35                 assertThat(page.getPageTitle(freenetRequest), equalTo("unbookmark page title"))
36         }
37
38         @Test
39         fun `get request does not redirect`() {
40                 verifyNoRedirect { }
41         }
42
43         @Test
44         fun `get request with all-not-loaded parameter unloads all not loaded posts and redirects to bookmarks`() {
45                 addHttpRequestParameter("post", "allNotLoaded")
46                 val loadedPost1 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
47                 val loadedPost2 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
48                 val notLoadedPost1 = mock<Post>()
49                 val notLoadedPost2 = mock<Post>()
50                 whenever(core.bookmarkedPosts).thenReturn(setOf(loadedPost1, loadedPost2, notLoadedPost1, notLoadedPost2))
51                 verifyRedirect("bookmarks.html") {
52                         val postCaptor = capture<Post>()
53                         verify(core, times(2)).unbookmarkPost(postCaptor.capture())
54                         assertThat(postCaptor.allValues, contains(notLoadedPost1, notLoadedPost2))
55                 }
56         }
57
58         @Test
59         fun `post request does not unbookmark not-present post but redirects to return page`() {
60                 setMethod(POST)
61                 addHttpRequestPart("post", "post-id")
62                 addHttpRequestPart("returnPage", "return.html")
63                 verifyRedirect("return.html") {
64                         verify(core, never()).unbookmarkPost(any())
65                 }
66         }
67
68         @Test
69         fun `post request unbookmarks present post and redirects to return page`() {
70                 setMethod(POST)
71                 addHttpRequestPart("post", "post-id")
72                 addHttpRequestPart("returnPage", "return.html")
73                 val post = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
74                 addPost("post-id", post)
75                 verifyRedirect("return.html") {
76                         verify(core).unbookmarkPost(post)
77                 }
78         }
79
80 }