Add test for DI constructability of GetLikesAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetLikesAjaxPageTest.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.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.getInstance
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.sone.web.baseInjector
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Test
16
17 /**
18  * Unit test for [GetLikesAjaxPage].
19  */
20 class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = false, pageSupplier = ::GetLikesAjaxPage) {
21
22         @Test
23         fun `request without parameters results in failing request`() {
24                 assertThat(json.isSuccess, equalTo(false))
25         }
26
27         @Test
28         fun `request with invalid post id results in invalid-post-id`() {
29                 addRequestParameter("type", "post")
30                 addRequestParameter("post", "invalid")
31                 assertThatJsonFailed("invalid-post-id")
32         }
33
34         @Test
35         fun `request with missing post id results in invalid-post-id`() {
36                 addRequestParameter("type", "post")
37                 assertThatJsonFailed("invalid-post-id")
38         }
39
40         @Test
41         fun `request with valid post id results in likes for post`() {
42                 val post = mock<Post>().apply { whenever(id).thenReturn("post-id") }
43                 addPost(post)
44                 addLikes(post, createSone(2), createSone(1), createSone(3))
45                 addRequestParameter("type", "post")
46                 addRequestParameter("post", "post-id")
47                 assertThatJsonIsSuccessful()
48                 assertThat(json["likes"]?.asInt(), equalTo(3))
49                 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
50                                 "S1" to "F1 M1 L1",
51                                 "S2" to "F2 M2 L2",
52                                 "S3" to "F3 M3 L3"
53                 ))
54         }
55
56         @Test
57         fun `request with invalid reply id results in invalid-reply-id`() {
58                 addRequestParameter("type", "reply")
59                 addRequestParameter("reply", "invalid")
60                 assertThatJsonFailed("invalid-reply-id")
61         }
62
63         @Test
64         fun `request with missing reply id results in invalid-reply-id`() {
65                 addRequestParameter("type", "reply")
66                 assertThatJsonFailed("invalid-reply-id")
67         }
68
69         @Test
70         fun `request with valid reply id results in likes for reply`() {
71                 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
72                 addReply(reply)
73                 addLikes(reply, createSone(2), createSone(1), createSone(3))
74                 addRequestParameter("type", "reply")
75                 addRequestParameter("reply", "reply-id")
76                 assertThatJsonIsSuccessful()
77                 assertThat(json["likes"]?.asInt(), equalTo(3))
78                 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
79                                 "S1" to "F1 M1 L1",
80                                 "S2" to "F2 M2 L2",
81                                 "S3" to "F3 M3 L3"
82                 ))
83         }
84
85         @Test
86         fun `request with invalid type results in invalid-type`() {
87                 addRequestParameter("type", "invalid")
88                 addRequestParameter("invalid", "foo")
89                 assertThatJsonFailed("invalid-type")
90         }
91
92         @Test
93         fun `page can be created by dependency injection`() {
94             assertThat(baseInjector.getInstance<GetLikesAjaxPage>(), notNullValue())
95         }
96         
97 }
98
99 private fun createSone(index: Int) = mock<Sone>().apply {
100         whenever(id).thenReturn("S$index")
101         whenever(profile).thenReturn(Profile(this).apply {
102                 firstName = "F$index"
103                 middleName = "M$index"
104                 lastName = "L$index"
105         })
106 }