1 package net.pterodactylus.sone.web.pages
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
14 import org.mockito.ArgumentMatchers.anyLong
15 import org.mockito.Mockito.never
16 import org.mockito.Mockito.verify
19 * Unit test for [RescuePage].
21 class RescuePageTest: WebPageTest(::RescuePage) {
23 private val soneRescuer = mock<SoneRescuer>()
26 fun setupSoneRescuer() {
27 whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
31 fun `page returns correct path`() {
32 assertThat(page.path, equalTo("rescue.html"))
36 fun `page requires login`() {
37 assertThat(page.requiresLogin(), equalTo(true))
41 fun `page returns correct title`() {
42 addTranslation("Page.Rescue.Title", "rescue page title")
43 assertThat(page.getPageTitle(freenetRequest), equalTo("rescue page title"))
47 fun `get request sets rescuer in template context`() {
49 assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
54 fun `post request redirects to rescue page`() {
56 verifyRedirect("rescue.html")
60 fun `post request with fetch and invalid edition starts next fetch`() {
62 addHttpRequestPart("fetch", "true")
63 verifyRedirect("rescue.html") {
64 verify(soneRescuer, never()).setEdition(anyLong())
65 verify(soneRescuer).startNextFetch()
70 fun `post request with fetch and valid edition sets edition and starts next fetch`() {
72 addHttpRequestPart("fetch", "true")
73 addHttpRequestPart("edition", "123")
74 verifyRedirect("rescue.html") {
75 verify(soneRescuer).setEdition(123L)
76 verify(soneRescuer).startNextFetch()
81 fun `post request with negative edition will not set edition`() {
83 addHttpRequestPart("fetch", "true")
84 addHttpRequestPart("edition", "-123")
85 verifyRedirect("rescue.html") {
86 verify(soneRescuer, never()).setEdition(anyLong())
87 verify(soneRescuer).startNextFetch()
92 fun `page can be created by dependency injection`() {
93 assertThat(baseInjector.getInstance<RescuePage>(), notNullValue())