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.getInstance
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.utils.asOptional
9 import net.pterodactylus.sone.utils.asTemplate
10 import net.pterodactylus.sone.web.baseInjector
11 import net.pterodactylus.util.template.ReflectionAccessor
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
18 * Unit test for [GetPostAjaxPage].
20 class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false,
21 pageSupplier = { webInterface ->
22 GetPostAjaxPage(webInterface, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate())
26 fun `request with missing post results in invalid-post-id`() {
27 assertThatJsonFailed("invalid-post-id")
31 fun `request with valid post results in post json`() {
32 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
33 val post = mock<Post>().apply {
34 whenever(id).thenReturn("post-id")
35 whenever(time).thenReturn(1000)
36 whenever(this.sone).thenReturn(sone)
37 whenever(recipientId).thenReturn("recipient-id".asOptional())
38 whenever(text).thenReturn("post text")
40 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
42 addRequestParameter("post", "post-id")
43 assertThatJsonIsSuccessful()
44 assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
45 assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
46 assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
47 assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id"))
48 assertThat(json["post"]!!["html"].asText(), equalTo(listOf(
50 freenetRequest.toString(),
52 currentSone.toString(),
53 core.localSones.toString()
54 ).joinToString("\n")))
58 fun `page can be created dependency injection`() {
59 assertThat(baseInjector.getInstance<GetPostAjaxPage>(), notNullValue())