--- /dev/null
+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()
+ }
+
+}
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()))
}
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() {
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 }
}
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) {