Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / RescuePageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.core.SoneRescuer
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.Before
10 import org.junit.Test
11 import org.mockito.ArgumentMatchers.anyLong
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [RescuePage].
17  */
18 class RescuePageTest: WebPageTest(::RescuePage) {
19
20         private val soneRescuer = mock<SoneRescuer>()
21
22         @Before
23         fun setupSoneRescuer() {
24                 whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
25         }
26
27         @Test
28         fun `page returns correct path`() {
29                 assertThat(page.path, equalTo("rescue.html"))
30         }
31
32         @Test
33         fun `page requires login`() {
34                 assertThat(page.requiresLogin(), equalTo(true))
35         }
36
37         @Test
38         fun `page returns correct title`() {
39                 addTranslation("Page.Rescue.Title", "rescue page title")
40                 assertThat(page.getPageTitle(freenetRequest), equalTo("rescue page title"))
41         }
42
43         @Test
44         fun `get request sets rescuer in template context`() {
45                 verifyNoRedirect {
46                         assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
47                 }
48         }
49
50         @Test
51         fun `post request redirects to rescue page`() {
52                 setMethod(POST)
53                 verifyRedirect("rescue.html")
54         }
55
56         @Test
57         fun `post request with fetch and invalid edition starts next fetch`() {
58                 setMethod(POST)
59                 addHttpRequestPart("fetch", "true")
60                 verifyRedirect("rescue.html") {
61                         verify(soneRescuer, never()).setEdition(anyLong())
62                         verify(soneRescuer).startNextFetch()
63                 }
64         }
65
66         @Test
67         fun `post request with fetch and valid edition sets edition and starts next fetch`() {
68                 setMethod(POST)
69                 addHttpRequestPart("fetch", "true")
70                 addHttpRequestPart("edition", "123")
71                 verifyRedirect("rescue.html") {
72                         verify(soneRescuer).setEdition(123L)
73                         verify(soneRescuer).startNextFetch()
74                 }
75         }
76
77         @Test
78         fun `post request with negative edition will not set edition`() {
79                 setMethod(POST)
80                 addHttpRequestPart("fetch", "true")
81                 addHttpRequestPart("edition", "-123")
82                 verifyRedirect("rescue.html") {
83                         verify(soneRescuer, never()).setEdition(anyLong())
84                         verify(soneRescuer).startNextFetch()
85                 }
86         }
87
88 }