Add unit test for dismiss notification page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 20:29:29 +0000 (21:29 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Nov 2016 20:29:29 +0000 (21:29 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/DismissNotificationPageTest.kt [new file with mode: 0644]

index 5a1d589..2825a20 100644 (file)
@@ -103,6 +103,7 @@ public abstract class WebPageTest {
        public final void setupWebInterface() {
                when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
                when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
+               when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
                when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
        }
 
@@ -164,4 +165,8 @@ public abstract class WebPageTest {
                when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
        }
 
+       protected void addNotification(String notificationId, Notification notification) {
+               when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
+       }
+
 }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DismissNotificationPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DismissNotificationPageTest.kt
new file mode 100644 (file)
index 0000000..a794f4c
--- /dev/null
@@ -0,0 +1,60 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.web.WebTestUtils.redirectsTo
+import net.pterodactylus.util.notify.Notification
+import net.pterodactylus.util.web.Method.GET
+import org.junit.Test
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+import kotlin.test.fail
+
+/**
+ * Unit test for [DismissNotificationPage].
+ */
+class DismissNotificationPageTest : WebPageTest() {
+
+       private val page = DismissNotificationPage(template, webInterface)
+       private val notification = mock<Notification>()
+
+       @Test
+       fun `get request with invalid notification ID redirects to return page`() {
+               request("", GET)
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `get request with dismissible notification dismisses the notification and redirects to return page`() {
+               request("", GET)
+               addNotification("notification-id", notification)
+               addHttpRequestParameter("notification", "notification-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+                       fail()
+               } finally {
+                       verify(notification, never()).dismiss()
+               }
+       }
+
+       @Test
+       fun `get request with non dismissible notification redirects to return page`() {
+               request("", GET)
+               whenever(notification.isDismissable).thenReturn(true)
+               addNotification("notification-id", notification)
+               addHttpRequestParameter("notification", "notification-id")
+               addHttpRequestParameter("returnPage", "return.html")
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+                       fail()
+               } finally {
+                       verify(notification).dismiss()
+               }
+       }
+
+}