6db74c0763d953a86ed6032d05871bc645334ea4
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / DeletePostAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.getInstance
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.web.baseInjector
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Test
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [DeletePostAjaxPage].
17  */
18 class DeletePostAjaxPageTest : JsonPageTest("deletePost.ajax", pageSupplier = ::DeletePostAjaxPage) {
19
20         @Test
21         fun `missing post ID results in invalid id response`() {
22                 assertThatJsonFailed("invalid-post-id")
23         }
24
25         @Test
26         fun `post from non-local sone results in not authorized response`() {
27                 val post = mock<Post>()
28                 val sone = mock<Sone>()
29                 whenever(post.sone).thenReturn(sone)
30                 addPost(post, "post-id")
31                 addRequestParameter("post", "post-id")
32                 assertThatJsonFailed("not-authorized")
33         }
34
35         @Test
36         fun `post from local sone is deleted`() {
37                 val post = mock<Post>()
38                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
39                 whenever(post.sone).thenReturn(sone)
40                 addPost(post, "post-id")
41                 addRequestParameter("post", "post-id")
42                 assertThatJsonIsSuccessful()
43                 verify(core).deletePost(post)
44         }
45
46         @Test
47         fun `page can be created by dependency injection`() {
48             assertThat(baseInjector.getInstance<DeletePostAjaxPage>(), notNullValue())
49         }
50
51 }