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 assertThat(json.isSuccess, equalTo(false))
25 assertThat(json.error, equalTo("invalid-post-id"))
29 fun `request with valid post results in post json`() {
30 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
31 val post = mock<Post>().apply {
32 whenever(id).thenReturn("post-id")
33 whenever(time).thenReturn(1000)
34 whenever(this.sone).thenReturn(sone)
35 whenever(recipientId).thenReturn("recipient-id".asOptional())
36 whenever(text).thenReturn("post text")
38 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
40 addRequestParameter("post", "post-id")
41 assertThat(json.isSuccess, equalTo(true))
42 assertThat(json["post"]["id"].asText(), equalTo("post-id"))
43 assertThat(json["post"]["time"].asLong(), equalTo(1000L))
44 assertThat(json["post"]["sone"].asText(), equalTo("sone-id"))
45 assertThat(json["post"]["recipient"].asText(), equalTo("recipient-id"))
46 assertThat(json["post"]["html"].asText(), equalTo(listOf(
48 freenetRequest.toString(),
50 currentSone.toString(),
51 core.localSones.toString()
52 ).joinToString("\n")))