From c3407a682f9f81b27d0354e59fe9e3d3d6dffcd6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 9 Sep 2017 18:53:08 +0200 Subject: [PATCH] Add test for get likes ajax page --- .../sone/web/ajax/GetLikesAjaxPageTest.kt | 103 +++++++++++++++++++++ .../pterodactylus/sone/web/ajax/JsonPageTest.kt | 13 +++ 2 files changed, 116 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt new file mode 100644 index 0000000..777d958 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt @@ -0,0 +1,103 @@ +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().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().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().apply { + whenever(id).thenReturn("S$index") + whenever(profile).thenReturn(Profile(this).apply { + firstName = "F$index" + middleName = "M$index" + lastName = "L$index" + }) +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index 60b2627..d5314c5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -29,6 +29,7 @@ import org.hamcrest.MatcherAssert.assertThat 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 @@ -66,8 +67,10 @@ abstract class JsonPageTest( private val localSones = mutableMapOf() private val remoteSones = mutableMapOf() private val posts = mutableMapOf() + private val postLikes = mutableMapOf>() private val newPosts = mutableMapOf() private val replies = mutableMapOf() + private val replyLikes = mutableMapOf>() private val newReplies = mutableMapOf() private val linkedElements = mutableMapOf() private val notifications = mutableMapOf() @@ -92,6 +95,8 @@ abstract class JsonPageTest( 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())).then { postLikes[it[0]] ?: emptySet() } + whenever(core.getLikes(any())).then { replyLikes[it[0]] ?: emptySet() } 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]] } @@ -182,6 +187,14 @@ abstract class JsonPageTest( 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().apply { whenever(this.id).thenReturn(id) -- 2.7.4