9a8f2c2a6b4e6c077af0f3c55cbeaf5575cf91d6
[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.mock
6 import net.pterodactylus.sone.test.whenever
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.junit.Test
10 import org.mockito.Mockito.never
11 import org.mockito.Mockito.verify
12
13 /**
14  * Unit test for [LikeAjaxPage].
15  */
16 class LikeAjaxPageTest : JsonPageTest("like.ajax", pageSupplier = ::LikeAjaxPage) {
17
18         @Test
19         fun `request with invalid type results in invalid-type error`() {
20                 addRequestParameter("type", "invalid")
21                 addRequestParameter("invalid", "invalid-id")
22                 assertThat(json.isSuccess, equalTo(false))
23                 assertThat(json.error, equalTo("invalid-type"))
24         }
25
26         @Test
27         fun `request with valid post id results in post being liked by current sone`() {
28                 addRequestParameter("type", "post")
29                 addRequestParameter("post", "post-id")
30                 addPost(mock<Post>().apply { whenever(id).thenReturn("post-id") })
31                 assertThat(json.isSuccess, equalTo(true))
32                 verify(currentSone).addLikedPostId("post-id")
33                 verify(core).touchConfiguration()
34         }
35
36         @Test
37         fun `request with valid reply id results in reply being liked by current sone`() {
38                 addRequestParameter("type", "reply")
39                 addRequestParameter("reply", "reply-id")
40                 addReply(mock<PostReply>().apply { whenever(id).thenReturn("reply-id") })
41                 assertThat(json.isSuccess, equalTo(true))
42                 verify(currentSone).addLikedReplyId("reply-id")
43                 verify(core).touchConfiguration()
44         }
45
46         @Test
47         fun `request with invalid post id results in post being liked by current sone`() {
48                 addRequestParameter("type", "post")
49                 addRequestParameter("post", "post-id")
50                 assertThat(json.isSuccess, equalTo(false))
51                 verify(currentSone, never()).addLikedPostId("post-id")
52                 verify(core, never()).touchConfiguration()
53         }
54
55         @Test
56         fun `request with invalid reply id results in reply being liked by current sone`() {
57                 addRequestParameter("type", "reply")
58                 addRequestParameter("reply", "reply-id")
59                 assertThat(json.isSuccess, equalTo(false))
60                 verify(currentSone, never()).addLikedReplyId("reply-id")
61                 verify(core, never()).touchConfiguration()
62         }
63
64 }