Add test for delete post ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 31 Aug 2017 18:31:32 +0000 (20:31 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Sep 2017 20:45:24 +0000 (22:45 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..026b717
--- /dev/null
@@ -0,0 +1,45 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Post
+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 [DeletePostAjaxPage].
+ */
+class DeletePostAjaxPageTest : JsonPageTest("deletePost.ajax", pageSupplier = ::DeletePostAjaxPage) {
+
+       @Test
+       fun `missing post ID results in invalid id response`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-post-id"))
+       }
+
+       @Test
+       fun `post from non-local sone results in not authorized response`() {
+               val post = mock<Post>()
+               val sone = mock<Sone>()
+               whenever(post.sone).thenReturn(sone)
+               addPost("post-id", post)
+               addRequestParameter("post", "post-id")
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("not-authorized"))
+       }
+
+       @Test
+       fun `post from local sone is deleted`() {
+               val post = mock<Post>()
+               val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
+               whenever(post.sone).thenReturn(sone)
+               addPost("post-id", post)
+               addRequestParameter("post", "post-id")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).deletePost(post)
+       }
+
+}