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.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
18 * Unit test for [GetLikesAjaxPage].
20 class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = false, pageSupplier = ::GetLikesAjaxPage) {
23 fun `request without parameters results in failing request`() {
24 assertThat(json.isSuccess, equalTo(false))
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")
35 fun `request with missing post id results in invalid-post-id`() {
36 addRequestParameter("type", "post")
37 assertThatJsonFailed("invalid-post-id")
41 fun `request with valid post id results in likes for post`() {
42 val post = mock<Post>().apply { whenever(id).thenReturn("post-id") }
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(
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")
64 fun `request with missing reply id results in invalid-reply-id`() {
65 addRequestParameter("type", "reply")
66 assertThatJsonFailed("invalid-reply-id")
70 fun `request with valid reply id results in likes for reply`() {
71 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
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(
86 fun `request with invalid type results in invalid-type`() {
87 addRequestParameter("type", "invalid")
88 addRequestParameter("invalid", "foo")
89 assertThatJsonFailed("invalid-type")
93 fun `page can be created by dependency injection`() {
94 assertThat(baseInjector.getInstance<GetLikesAjaxPage>(), notNullValue())
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"