From 2e605a25761505242149296e70d77e5ea8b13968 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 2 Sep 2017 10:35:36 +0200 Subject: [PATCH] Add test for delete reply ajax page --- .../sone/web/ajax/DeleteReplyAjaxPageTest.kt | 46 ++++++++++++++++++++++ .../pterodactylus/sone/web/ajax/JsonPageTest.kt | 6 +++ 2 files changed, 52 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPageTest.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 index 0000000..873d897 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPageTest.kt @@ -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() + val sone = mock() + 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() + val sone = mock() + 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) + } + +} 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 afa9b17..cdb68a3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -61,6 +61,7 @@ abstract class JsonPageTest( private val remoteSones = mutableMapOf() private val posts = mutableMapOf() private val newPosts = mutableMapOf() + private val replies = mutableMapOf() private val newReplies = mutableMapOf() private val linkedElements = mutableMapOf() private val notifications = mutableListOf() @@ -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().apply { whenever(this.id).thenReturn(id) -- 2.7.4