1 package net.pterodactylus.sone.web.pages
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
11 import org.mockito.ArgumentMatchers.anyLong
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
16 * Unit test for [RescuePage].
18 class RescuePageTest: WebPageTest(::RescuePage) {
20 private val soneRescuer = mock<SoneRescuer>()
23 fun setupSoneRescuer() {
24 whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
28 fun `page returns correct path`() {
29 assertThat(page.path, equalTo("rescue.html"))
33 fun `page requires login`() {
34 assertThat(page.requiresLogin(), equalTo(true))
38 fun `page returns correct title`() {
39 addTranslation("Page.Rescue.Title", "rescue page title")
40 assertThat(page.getPageTitle(freenetRequest), equalTo("rescue page title"))
44 fun `get request sets rescuer in template context`() {
46 assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
51 fun `post request redirects to rescue page`() {
53 verifyRedirect("rescue.html")
57 fun `post request with fetch and invalid edition starts next fetch`() {
59 addHttpRequestPart("fetch", "true")
60 verifyRedirect("rescue.html") {
61 verify(soneRescuer, never()).setEdition(anyLong())
62 verify(soneRescuer).startNextFetch()
67 fun `post request with fetch and valid edition sets edition and starts next fetch`() {
69 addHttpRequestPart("fetch", "true")
70 addHttpRequestPart("edition", "123")
71 verifyRedirect("rescue.html") {
72 verify(soneRescuer).setEdition(123L)
73 verify(soneRescuer).startNextFetch()
78 fun `post request with negative edition will not set edition`() {
80 addHttpRequestPart("fetch", "true")
81 addHttpRequestPart("edition", "-123")
82 verifyRedirect("rescue.html") {
83 verify(soneRescuer, never()).setEdition(anyLong())
84 verify(soneRescuer).startNextFetch()