package net.pterodactylus.sone.web;
+import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
+import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
}
+ protected SoneTemplatePage getPage() {
+ return null;
+ }
+
protected void unsetCurrentSone() {
when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
}
+ protected void verifyRedirect(String target) throws RedirectException {
+ expectedException.expect(redirectsTo(target));
+ getPage().handleRequest(freenetRequest, templateContext);
+ }
+
+ protected void verifyRedirect(String target, Runnable verification) throws RedirectException {
+ expectedException.expect(redirectsTo(target));
+ try {
+ getPage().handleRequest(freenetRequest, templateContext);
+ fail();
+ } finally {
+ verification.run();
+ }
+ }
+
}
--- /dev/null
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.core.SoneRescuer
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.util.web.Method.GET
+import net.pterodactylus.util.web.Method.POST
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Before
+import org.junit.Test
+import org.mockito.ArgumentMatchers.anyLong
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [RescuePage].
+ */
+class RescuePageTest : WebPageTest() {
+
+ private val page = RescuePage(template, webInterface)
+
+ private val soneRescuer = mock<SoneRescuer>()
+
+ override fun getPage() = page
+
+ @Before
+ fun setupSoneRescuer() {
+ whenever(core.getSoneRescuer(currentSone)).thenReturn(soneRescuer)
+ }
+
+ @Test
+ fun `get request sets rescuer in template context`() {
+ request("", GET)
+ page.handleRequest(freenetRequest, templateContext)
+ assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
+ }
+
+ @Test
+ fun `post request redirects to rescue page`() {
+ request("", POST)
+ verifyRedirect("rescue.html")
+ }
+
+ @Test
+ fun `post request with fetch and invalid edition starts next fetch`() {
+ request("", POST)
+ addHttpRequestParameter("fetch", "true")
+ verifyRedirect("rescue.html") {
+ verify(soneRescuer, never()).setEdition(anyLong())
+ verify(soneRescuer).startNextFetch()
+ }
+ }
+
+ @Test
+ fun `post request with fetch and valid edition sets edition and starts next fetch`() {
+ request("", POST)
+ addHttpRequestParameter("fetch", "true")
+ addHttpRequestParameter("edition", "123")
+ verifyRedirect("rescue.html") {
+ verify(soneRescuer).setEdition(123L)
+ verify(soneRescuer).startNextFetch()
+ }
+ }
+
+}