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 net.pterodactylus.util.template.TemplateContextFactory
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.equalTo
15 import org.hamcrest.Matchers.notNullValue
19 * Unit test for [GetPostAjaxPage].
21 class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false) {
23 private val templateContextFactory = TemplateContextFactory().apply {
24 addAccessor(Any::class.java, ReflectionAccessor())
26 override val page: JsonPage by lazy { GetPostAjaxPage(webInterface, templateContextFactory, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate()) }
29 fun `request with missing post results in invalid-post-id`() {
30 assertThatJsonFailed("invalid-post-id")
34 fun `request with valid post results in post json`() {
35 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
36 val post = mock<Post>().apply {
37 whenever(id).thenReturn("post-id")
38 whenever(time).thenReturn(1000)
39 whenever(this.sone).thenReturn(sone)
40 whenever(recipientId).thenReturn("recipient-id".asOptional())
41 whenever(text).thenReturn("post text")
44 addRequestParameter("post", "post-id")
45 assertThatJsonIsSuccessful()
46 assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
47 assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
48 assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
49 assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id"))
50 assertThat(json["post"]!!["html"].asText(), equalTo(listOf(
52 freenetRequest.toString(),
54 currentSone.toString(),
55 core.localSones.toString()
56 ).joinToString("\n")))
60 fun `page can be created dependency injection`() {
61 assertThat(baseInjector.getInstance<GetPostAjaxPage>(), notNullValue())