1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.sone.utils.asOptional
8 import net.pterodactylus.sone.utils.asTemplate
9 import net.pterodactylus.util.template.ReflectionAccessor
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
15 * Unit test for [GetPostAjaxPage].
17 class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false,
18 pageSupplier = { webInterface ->
19 GetPostAjaxPage(webInterface, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate())
23 fun `request with missing post results in invalid-post-id`() {
24 assertThatJsonFailed("invalid-post-id")
28 fun `request with valid post results in post json`() {
29 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
30 val post = mock<Post>().apply {
31 whenever(id).thenReturn("post-id")
32 whenever(time).thenReturn(1000)
33 whenever(this.sone).thenReturn(sone)
34 whenever(recipientId).thenReturn("recipient-id".asOptional())
35 whenever(text).thenReturn("post text")
37 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
39 addRequestParameter("post", "post-id")
40 assertThatJsonIsSuccessful()
41 assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
42 assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
43 assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
44 assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id"))
45 assertThat(json["post"]!!["html"].asText(), equalTo(listOf(
47 freenetRequest.toString(),
49 currentSone.toString(),
50 core.localSones.toString()
51 ).joinToString("\n")))