From: David ‘Bombe’ Roden Date: Tue, 5 Sep 2017 06:04:55 +0000 (+0200) Subject: Add test for dismiss notification ajax page X-Git-Tag: 0.9.7^2~93 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e5224bcc64cbbe5b0396984554d76dc3a5960e80;p=Sone.git Add test for dismiss notification ajax page --- 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 index 0000000..a10c51c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DismissNotificationAjaxPageTest.kt @@ -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() + 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().apply { whenever(isDismissable).thenReturn(true) } + addNotification(notification, "foo") + addRequestParameter("notification", "foo") + assertThat(json.isSuccess, equalTo(true)) + verify(notification).dismiss() + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index 58193a9..e01764d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -97,7 +97,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals mock().apply { whenever(this.createdTime).thenReturn(2000) }, mock().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())) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index cdb68a3..f3558fa 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -64,7 +64,7 @@ abstract class JsonPageTest( private val replies = mutableMapOf() private val newReplies = mutableMapOf() private val linkedElements = mutableMapOf() - private val notifications = mutableListOf() + private val notifications = mutableMapOf() @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) {