Convert view Sone page test to new web page test base
[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() {
19
20         private val page = RescuePage(template, webInterface)
21
22         private val soneRescuer = mock<SoneRescuer>()
23
24         override fun getPage() = page
25
26         @Before
27         fun setupSoneRescuer() {
28                 whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
29         }
30
31         @Test
32         fun `page returns correct path`() {
33                 assertThat(page.path, equalTo("rescue.html"))
34         }
35
36         @Test
37         fun `page requires login`() {
38                 assertThat(page.requiresLogin(), equalTo(true))
39         }
40
41         @Test
42         fun `page returns correct title`() {
43                 addTranslation("Page.Rescue.Title", "rescue page title")
44                 assertThat(page.getPageTitle(freenetRequest), equalTo("rescue page title"))
45         }
46
47         @Test
48         fun `get request sets rescuer in template context`() {
49                 verifyNoRedirect {
50                         assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
51                 }
52         }
53
54         @Test
55         fun `post request redirects to rescue page`() {
56                 setMethod(POST)
57                 verifyRedirect("rescue.html")
58         }
59
60         @Test
61         fun `post request with fetch and invalid edition starts next fetch`() {
62                 setMethod(POST)
63                 addHttpRequestPart("fetch", "true")
64                 verifyRedirect("rescue.html") {
65                         verify(soneRescuer, never()).setEdition(anyLong())
66                         verify(soneRescuer).startNextFetch()
67                 }
68         }
69
70         @Test
71         fun `post request with fetch and valid edition sets edition and starts next fetch`() {
72                 setMethod(POST)
73                 addHttpRequestPart("fetch", "true")
74                 addHttpRequestPart("edition", "123")
75                 verifyRedirect("rescue.html") {
76                         verify(soneRescuer).setEdition(123L)
77                         verify(soneRescuer).startNextFetch()
78                 }
79         }
80
81         @Test
82         fun `post request with negative edition will not set edition`() {
83                 setMethod(POST)
84                 addHttpRequestPart("fetch", "true")
85                 addHttpRequestPart("edition", "-123")
86                 verifyRedirect("rescue.html") {
87                         verify(soneRescuer, never()).setEdition(anyLong())
88                         verify(soneRescuer).startNextFetch()
89                 }
90         }
91
92
93 }