Add test for DI constructability of LikeAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / LikeAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
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.never
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [LikeAjaxPage].
18  */
19 class LikeAjaxPageTest : JsonPageTest("like.ajax", pageSupplier = ::LikeAjaxPage) {
20
21         @Test
22         fun `request with invalid type results in invalid-type error`() {
23                 addRequestParameter("type", "invalid")
24                 addRequestParameter("invalid", "invalid-id")
25                 assertThatJsonFailed("invalid-type")
26         }
27
28         @Test
29         fun `request with valid post id results in post being liked by current sone`() {
30                 addRequestParameter("type", "post")
31                 addRequestParameter("post", "post-id")
32                 addPost(mock<Post>().apply { whenever(id).thenReturn("post-id") })
33                 assertThatJsonIsSuccessful()
34                 verify(currentSone).addLikedPostId("post-id")
35                 verify(core).touchConfiguration()
36         }
37
38         @Test
39         fun `request with valid reply id results in reply being liked by current sone`() {
40                 addRequestParameter("type", "reply")
41                 addRequestParameter("reply", "reply-id")
42                 addReply(mock<PostReply>().apply { whenever(id).thenReturn("reply-id") })
43                 assertThatJsonIsSuccessful()
44                 verify(currentSone).addLikedReplyId("reply-id")
45                 verify(core).touchConfiguration()
46         }
47
48         @Test
49         fun `request with invalid post id results in post being liked by current sone`() {
50                 addRequestParameter("type", "post")
51                 addRequestParameter("post", "post-id")
52                 assertThat(json.isSuccess, equalTo(false))
53                 verify(currentSone, never()).addLikedPostId("post-id")
54                 verify(core, never()).touchConfiguration()
55         }
56
57         @Test
58         fun `request with invalid reply id results in reply being liked by current sone`() {
59                 addRequestParameter("type", "reply")
60                 addRequestParameter("reply", "reply-id")
61                 assertThat(json.isSuccess, equalTo(false))
62                 verify(currentSone, never()).addLikedReplyId("reply-id")
63                 verify(core, never()).touchConfiguration()
64         }
65
66         @Test
67         fun `page can be created by dependency injection`() {
68             assertThat(baseInjector.getInstance<LikeAjaxPage>(), notNullValue())
69         }
70
71 }