1 package net.pterodactylus.sone.web.ajax
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.mock
8 import net.pterodactylus.sone.test.whenever
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
15 * Unit test for [GetLikesAjaxPage].
17 class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = false, pageSupplier = ::GetLikesAjaxPage) {
20 fun `request without parameters results in failing request`() {
21 assertThat(json.isSuccess, equalTo(false))
25 fun `request with invalid post id results in invalid-post-id`() {
26 addRequestParameter("type", "post")
27 addRequestParameter("post", "invalid")
28 assertThatJsonFailed("invalid-post-id")
32 fun `request with missing post id results in invalid-post-id`() {
33 addRequestParameter("type", "post")
34 assertThatJsonFailed("invalid-post-id")
38 fun `request with valid post id results in likes for post`() {
39 val post = mock<Post>().apply { whenever(id).thenReturn("post-id") }
41 addLikes(post, createSone(2), createSone(1), createSone(3))
42 addRequestParameter("type", "post")
43 addRequestParameter("post", "post-id")
44 assertThatJsonIsSuccessful()
45 assertThat(json["likes"]?.asInt(), equalTo(3))
46 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
54 fun `request with invalid reply id results in invalid-reply-id`() {
55 addRequestParameter("type", "reply")
56 addRequestParameter("reply", "invalid")
57 assertThatJsonFailed("invalid-reply-id")
61 fun `request with missing reply id results in invalid-reply-id`() {
62 addRequestParameter("type", "reply")
63 assertThatJsonFailed("invalid-reply-id")
67 fun `request with valid reply id results in likes for reply`() {
68 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
70 addLikes(reply, createSone(2), createSone(1), createSone(3))
71 addRequestParameter("type", "reply")
72 addRequestParameter("reply", "reply-id")
73 assertThatJsonIsSuccessful()
74 assertThat(json["likes"]?.asInt(), equalTo(3))
75 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
83 fun `request with invalid type results in invalid-type`() {
84 addRequestParameter("type", "invalid")
85 addRequestParameter("invalid", "foo")
86 assertThatJsonFailed("invalid-type")
91 private fun createSone(index: Int) = mock<Sone>().apply {
92 whenever(id).thenReturn("S$index")
93 whenever(profile).thenReturn(Profile(this).apply {
95 middleName = "M$index"