--- /dev/null
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Profile
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+/**
+ * Unit test for [GetLikesAjaxPage].
+ */
+class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = false, pageSupplier = ::GetLikesAjaxPage) {
+
+ @Test
+ fun `request without parameters results in failing request`() {
+ assertThat(json.isSuccess, equalTo(false))
+ }
+
+ @Test
+ fun `request with invalid post id results in invalid-post-id`() {
+ addRequestParameter("type", "post")
+ addRequestParameter("post", "invalid")
+ assertThat(json.isSuccess, equalTo(false))
+ assertThat(json.error, equalTo("invalid-post-id"))
+ }
+
+ @Test
+ fun `request with missing post id results in invalid-post-id`() {
+ addRequestParameter("type", "post")
+ assertThat(json.isSuccess, equalTo(false))
+ assertThat(json.error, equalTo("invalid-post-id"))
+ }
+
+ @Test
+ fun `request with valid post id results in likes for post`() {
+ val post = mock<Post>().apply { whenever(id).thenReturn("post-id") }
+ addPost(post)
+ addLikes(post, createSone(2), createSone(1), createSone(3))
+ addRequestParameter("type", "post")
+ addRequestParameter("post", "post-id")
+ assertThat(json.isSuccess, equalTo(true))
+ assertThat(json["likes"].asInt(), equalTo(3))
+ assertThat(json["sones"].toList().map { it["id"].asText() to it["name"].asText() }, contains(
+ "S1" to "F1 M1 L1",
+ "S2" to "F2 M2 L2",
+ "S3" to "F3 M3 L3"
+ ))
+ }
+
+ @Test
+ fun `request with invalid reply id results in invalid-reply-id`() {
+ addRequestParameter("type", "reply")
+ addRequestParameter("reply", "invalid")
+ assertThat(json.isSuccess, equalTo(false))
+ assertThat(json.error, equalTo("invalid-reply-id"))
+ }
+
+ @Test
+ fun `request with missing reply id results in invalid-reply-id`() {
+ addRequestParameter("type", "reply")
+ assertThat(json.isSuccess, equalTo(false))
+ assertThat(json.error, equalTo("invalid-reply-id"))
+ }
+
+ @Test
+ fun `request with valid reply id results in likes for reply`() {
+ val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
+ addReply(reply)
+ addLikes(reply, createSone(2), createSone(1), createSone(3))
+ addRequestParameter("type", "reply")
+ addRequestParameter("reply", "reply-id")
+ assertThat(json.isSuccess, equalTo(true))
+ assertThat(json["likes"].asInt(), equalTo(3))
+ assertThat(json["sones"].toList().map { it["id"].asText() to it["name"].asText() }, contains(
+ "S1" to "F1 M1 L1",
+ "S2" to "F2 M2 L2",
+ "S3" to "F3 M3 L3"
+ ))
+ }
+
+ @Test
+ fun `request with invalid type results in invalid-type`() {
+ addRequestParameter("type", "invalid")
+ addRequestParameter("invalid", "foo")
+ assertThat(json.isSuccess, equalTo(false))
+ assertThat(json.error, equalTo("invalid-type"))
+ }
+
+}
+
+private fun createSone(index: Int) = mock<Sone>().apply {
+ whenever(id).thenReturn("S$index")
+ whenever(profile).thenReturn(Profile(this).apply {
+ firstName = "F$index"
+ middleName = "M$index"
+ lastName = "L$index"
+ })
+}
import org.hamcrest.Matchers.equalTo
import org.junit.Before
import org.junit.Test
+import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
private val localSones = mutableMapOf<String, Sone>()
private val remoteSones = mutableMapOf<String, Sone>()
private val posts = mutableMapOf<String, Post>()
+ private val postLikes = mutableMapOf<Post, Set<Sone>>()
private val newPosts = mutableMapOf<String, Post>()
private val replies = mutableMapOf<String, PostReply>()
+ private val replyLikes = mutableMapOf<PostReply, Set<Sone>>()
private val newReplies = mutableMapOf<String, PostReply>()
private val linkedElements = mutableMapOf<String, LinkedElement>()
private val notifications = mutableMapOf<String, Notification>()
whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
+ whenever(core.getLikes(any<Post>())).then { postLikes[it[0]] ?: emptySet<Sone>() }
+ whenever(core.getLikes(any<PostReply>())).then { replyLikes[it[0]] ?: emptySet<Sone>() }
whenever(core.getPostReply(anyString())).then { replies[it[0]].asOptional() }
whenever(core.getAlbum(anyString())).then { albums[it[0]] }
whenever(core.getImage(anyString())).then { images[it[0]] }
posts[id ?: post.id] = post
}
+ protected fun addLikes(post: Post, vararg sones: Sone) {
+ postLikes[post] = setOf(*sones)
+ }
+
+ protected fun addLikes(reply: PostReply, vararg sones: Sone) {
+ replyLikes[reply] = setOf(*sones)
+ }
+
protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) =
mock<Post>().apply {
whenever(this.id).thenReturn(id)