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