Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / MarkAsKnownPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Test
11 import org.mockito.Mockito.verify
12
13 /**
14  * Unit test for [MarkAsKnownPage].
15  */
16 class MarkAsKnownPageTest: WebPageTest(::MarkAsKnownPage) {
17
18         @Test
19         fun `page returns correct path`() {
20                 assertThat(page.path, equalTo("markAsKnown.html"))
21         }
22
23         @Test
24         fun `page does not require login`() {
25                 assertThat(page.requiresLogin(), equalTo(false))
26         }
27
28         @Test
29         fun `page returns correct title`() {
30                 addTranslation("Page.MarkAsKnown.Title", "mark as known page title")
31                 assertThat(page.getPageTitle(freenetRequest), equalTo("mark as known page title"))
32         }
33
34         @Test
35         fun `posts can be marked as known`() {
36                 setMethod(POST)
37                 addHttpRequestPart("returnPage", "return.html")
38                 addHttpRequestPart("type", "post")
39                 addHttpRequestPart("id", "post1 post2 post3")
40                 val posts = listOf(mock<Post>(), mock<Post>())
41                 addPost("post1", posts[0])
42                 addPost("post3", posts[1])
43                 verifyRedirect("return.html") {
44                         verify(core).markPostKnown(posts[0])
45                         verify(core).markPostKnown(posts[1])
46                 }
47         }
48
49         @Test
50         fun `replies can be marked as known`() {
51                 setMethod(POST)
52                 addHttpRequestPart("returnPage", "return.html")
53                 addHttpRequestPart("type", "reply")
54                 addHttpRequestPart("id", "reply1 reply2 reply3")
55                 val replies = listOf(mock<PostReply>(), mock<PostReply>())
56                 addPostReply("reply1", replies[0])
57                 addPostReply("reply3", replies[1])
58                 verifyRedirect("return.html") {
59                         verify(core).markReplyKnown(replies[0])
60                         verify(core).markReplyKnown(replies[1])
61                 }
62         }
63
64         @Test
65         fun `sones can be marked as known`() {
66                 setMethod(POST)
67                 addHttpRequestPart("returnPage", "return.html")
68                 addHttpRequestPart("type", "sone")
69                 addHttpRequestPart("id", "sone1 sone2 sone3")
70                 val sones = listOf(mock<Sone>(), mock<Sone>())
71                 addSone("sone1", sones[0])
72                 addSone("sone3", sones[1])
73                 verifyRedirect("return.html") {
74                         verify(core).markSoneKnown(sones[0])
75                         verify(core).markSoneKnown(sones[1])
76                 }
77         }
78
79         @Test
80         fun `different type redirects to invalid page`() {
81                 setMethod(POST)
82                 addHttpRequestPart("type", "foo")
83                 verifyRedirect("invalid.html")
84         }
85
86 }