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() {
20 private val page = RescuePage(template, webInterface)
22 private val soneRescuer = mock<SoneRescuer>()
24 override fun getPage() = page
27 fun setupSoneRescuer() {
28 whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
32 fun `page returns correct path`() {
33 assertThat(page.path, equalTo("rescue.html"))
37 fun `page requires login`() {
38 assertThat(page.requiresLogin(), equalTo(true))
42 fun `page returns correct title`() {
43 addTranslation("Page.Rescue.Title", "rescue page title")
44 assertThat(page.getPageTitle(freenetRequest), equalTo("rescue page title"))
48 fun `get request sets rescuer in template context`() {
50 assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
55 fun `post request redirects to rescue page`() {
57 verifyRedirect("rescue.html")
61 fun `post request with fetch and invalid edition starts next fetch`() {
63 addHttpRequestPart("fetch", "true")
64 verifyRedirect("rescue.html") {
65 verify(soneRescuer, never()).setEdition(anyLong())
66 verify(soneRescuer).startNextFetch()
71 fun `post request with fetch and valid edition sets edition and starts next fetch`() {
73 addHttpRequestPart("fetch", "true")
74 addHttpRequestPart("edition", "123")
75 verifyRedirect("rescue.html") {
76 verify(soneRescuer).setEdition(123L)
77 verify(soneRescuer).startNextFetch()
82 fun `post request with negative edition will not set edition`() {
84 addHttpRequestPart("fetch", "true")
85 addHttpRequestPart("edition", "-123")
86 verifyRedirect("rescue.html") {
87 verify(soneRescuer, never()).setEdition(anyLong())
88 verify(soneRescuer).startNextFetch()