Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / FollowSonePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
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.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.junit.Test
10 import org.mockito.ArgumentMatchers
11 import org.mockito.ArgumentMatchers.any
12 import org.mockito.ArgumentMatchers.anyString
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [FollowSonePage].
18  */
19 class FollowSonePageTest: WebPageTest(::FollowSonePage) {
20
21         @Test
22         fun `page returns correct path`() {
23             assertThat(page.path, equalTo("followSone.html"))
24         }
25
26         @Test
27         fun `page requires login`() {
28             assertThat(page.requiresLogin(), equalTo(true))
29         }
30
31         @Test
32         fun `page returns correct title`() {
33             whenever(l10n.getString("Page.FollowSone.Title")).thenReturn("follow sone page title")
34                 assertThat(page.getPageTitle(freenetRequest), equalTo("follow sone page title"))
35         }
36
37         @Test
38         fun `get request does not redirect`() {
39                 page.processTemplate(freenetRequest, templateContext)
40         }
41
42         @Test
43         fun `a single sone can be followed`() {
44                 setMethod(POST)
45                 val sone = mock<Sone>()
46                 addSone("sone-id", sone)
47                 addHttpRequestPart("sone", "sone-id")
48                 addHttpRequestPart("returnPage", "return.html")
49                 verifyRedirect("return.html") {
50                         verify(core).followSone(currentSone, "sone-id")
51                         verify(core).markSoneKnown(sone)
52                 }
53         }
54
55         @Test
56         fun `multiple sones can be followed`() {
57                 setMethod(POST)
58                 val firstSone = mock<Sone>()
59                 addSone("sone-id1", firstSone)
60                 val secondSone = mock<Sone>()
61                 addSone("sone-id2", secondSone)
62                 addHttpRequestPart("sone", "sone-id1,sone-id2")
63                 addHttpRequestPart("returnPage", "return.html")
64                 verifyRedirect("return.html") {
65                         verify(core).followSone(currentSone, "sone-id1")
66                         verify(core).followSone(currentSone, "sone-id2")
67                         verify(core).markSoneKnown(firstSone)
68                         verify(core).markSoneKnown(secondSone)
69                 }
70         }
71
72         @Test
73         fun `a non-existing sone is not followed`() {
74                 setMethod(POST)
75                 addHttpRequestPart("sone", "sone-id")
76                 addHttpRequestPart("returnPage", "return.html")
77                 verifyRedirect("return.html") {
78                         verify(core, never()).followSone(ArgumentMatchers.eq(currentSone), anyString())
79                         verify(core, never()).markSoneKnown(any<Sone>())
80                 }
81         }
82
83 }