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