From 3734def16a9888d2b904d39ac3a7118f8bc052f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 6 Dec 2016 21:42:51 +0100 Subject: [PATCH] Add unit test for rescue page --- .../net/pterodactylus/sone/web/WebPageTest.java | 22 ++++++++ .../net/pterodactylus/sone/web/RescuePageTest.kt | 66 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/RescuePageTest.kt diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 71cc9f2..f7ea06d 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -1,5 +1,7 @@ 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; @@ -36,6 +38,7 @@ import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; 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; @@ -185,6 +188,10 @@ public abstract class WebPageTest { 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); @@ -253,4 +260,19 @@ public abstract class WebPageTest { 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(); + } + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/RescuePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/RescuePageTest.kt new file mode 100644 index 0000000..f57d11c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/RescuePageTest.kt @@ -0,0 +1,66 @@ +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() + + 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(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() + } + } + +} -- 2.7.4