Add test for delete reply ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 2 Sep 2017 08:35:36 +0000 (10:35 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 2 Sep 2017 08:35:36 +0000 (10:35 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..873d897
--- /dev/null
@@ -0,0 +1,46 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [DeleteReplyAjaxPage].
+ */
+class DeleteReplyAjaxPageTest : JsonPageTest("deleteReply.ajax", pageSupplier = ::DeleteReplyAjaxPage) {
+
+       @Test
+       fun `request with missing reply results in invalid id`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-reply-id"))
+       }
+
+       @Test
+       fun `request with non-local reply id results in not authorized`() {
+               val reply = mock<PostReply>()
+               val sone = mock<Sone>()
+               whenever(reply.sone).thenReturn(sone)
+               addReply("reply-id", reply)
+               addRequestParameter("reply", "reply-id")
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("not-authorized"))
+       }
+
+       @Test
+       fun `request with local reply id deletes reply`() {
+               val reply = mock<PostReply>()
+               val sone = mock<Sone>()
+               whenever(sone.isLocal).thenReturn(true)
+               whenever(reply.sone).thenReturn(sone)
+               addReply("reply-id", reply)
+               addRequestParameter("reply", "reply-id")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).deleteReply(reply)
+       }
+
+}
index afa9b17..cdb68a3 100644 (file)
@@ -61,6 +61,7 @@ abstract class JsonPageTest(
        private val remoteSones = mutableMapOf<String, Sone>()
        private val posts = mutableMapOf<String, Post>()
        private val newPosts = mutableMapOf<String, Post>()
+       private val replies = mutableMapOf<String, PostReply>()
        private val newReplies = mutableMapOf<String, PostReply>()
        private val linkedElements = mutableMapOf<String, LinkedElement>()
        private val notifications = mutableListOf<Notification>()
@@ -81,6 +82,7 @@ abstract class JsonPageTest(
                whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
                whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
                whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
+               whenever(core.getPostReply(anyString())).then { replies[it[0]].asOptional() }
        }
 
        @Before
@@ -176,6 +178,10 @@ abstract class JsonPageTest(
                                whenever(this.recipientId).thenReturn(recipientId.asOptional())
                        }.also { newPosts[id] = it }
 
+       protected fun addReply(id: String, reply: PostReply) {
+               replies[id] = reply
+       }
+
        protected fun addNewReply(id: String, soneId: String, postId: String, postSoneId: String) {
                newReplies[id] = mock<PostReply>().apply {
                        whenever(this.id).thenReturn(id)