From: David ‘Bombe’ Roden Date: Thu, 24 Nov 2016 20:29:29 +0000 (+0100) Subject: Add unit test for dismiss notification page X-Git-Tag: 0.9.7^2~396 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8aebcf573e7b5ef8b7b09f1c90c4f12a82920b64;p=Sone.git Add unit test for dismiss notification page --- diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 5a1d589..2825a20 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -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.absent()); when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList()); } @@ -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 index 0000000..a794f4c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/DismissNotificationPageTest.kt @@ -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() + + @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() + } + } + +}