Add unit test for rescue page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Dec 2016 20:42:51 +0000 (21:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Dec 2016 21:19:36 +0000 (22:19 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/RescuePageTest.kt [new file with mode: 0644]

index 71cc9f2..f7ea06d 100644 (file)
@@ -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 (file)
index 0000000..f57d11c
--- /dev/null
@@ -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<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()
+               }
+       }
+
+}