Add test for dismiss notification ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Sep 2017 06:04:55 +0000 (08:04 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Sep 2017 06:04:55 +0000 (08:04 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/DismissNotificationAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DismissNotificationAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DismissNotificationAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..a10c51c
--- /dev/null
@@ -0,0 +1,40 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.util.notify.Notification
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [DismissNotificationAjaxPage].
+ */
+class DismissNotificationAjaxPageTest : JsonPageTest("dismissNotification.ajax", requiresLogin = false, pageSupplier = ::DismissNotificationAjaxPage) {
+
+       @Test
+       fun `request without notification returns invalid-notification-id`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-notification-id"))
+       }
+
+       @Test
+       fun `request to dismiss non-dismissable notification results in not-dismissable`() {
+               val notification = mock<Notification>()
+               addNotification(notification, "foo")
+               addRequestParameter("notification", "foo")
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("not-dismissable"))
+       }
+
+       @Test
+       fun `request to dismiss dismissable notification dismisses notification`() {
+               val notification = mock<Notification>().apply { whenever(isDismissable).thenReturn(true) }
+               addNotification(notification, "foo")
+               addRequestParameter("notification", "foo")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(notification).dismiss()
+       }
+
+}
index 58193a9..e01764d 100644 (file)
@@ -97,7 +97,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals
                                mock<Notification>().apply { whenever(this.createdTime).thenReturn(2000) },
                                mock<Notification>().apply { whenever(this.createdTime).thenReturn(1000) }
                )
-               addNotification(*notifications.toTypedArray())
+               notifications.forEachIndexed { index, notification -> addNotification(notification, "notification$index")}
                assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode()))
        }
 
index cdb68a3..f3558fa 100644 (file)
@@ -64,7 +64,7 @@ abstract class JsonPageTest(
        private val replies = mutableMapOf<String, PostReply>()
        private val newReplies = mutableMapOf<String, PostReply>()
        private val linkedElements = mutableMapOf<String, LinkedElement>()
-       private val notifications = mutableListOf<Notification>()
+       private val notifications = mutableMapOf<String, Notification>()
 
        @Before
        fun setupWebInterface() {
@@ -72,7 +72,8 @@ abstract class JsonPageTest(
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.core).thenReturn(core)
-               whenever(webInterface.getNotifications(currentSone)).thenAnswer { notifications }
+               whenever(webInterface.getNotifications(currentSone)).thenAnswer { notifications.values }
+               whenever(webInterface.getNotification(anyString())).then { notifications[it[0]].asOptional() }
                whenever(webInterface.getNewPosts(currentSone)).thenAnswer { newPosts.values }
                whenever(webInterface.getNewReplies(currentSone)).thenAnswer { newReplies.values }
        }
@@ -153,8 +154,8 @@ abstract class JsonPageTest(
                requestParts += key to value
        }
 
-       protected fun addNotification(vararg notifications: Notification) {
-               this.notifications += notifications
+       protected fun addNotification(notification: Notification, notificationId: String? = null) {
+               notifications[notificationId ?: notification.id] = notification
        }
 
        protected fun addSone(sone: Sone) {