1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.data.PostReply
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.asTemplate
9 import net.pterodactylus.sone.web.baseInjector
10 import net.pterodactylus.util.template.ReflectionAccessor
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.notNullValue
17 * Unit test for [GetReplyAjaxPage].
19 class GetReplyAjaxPageTest : JsonPageTest("getReply.ajax", needsFormPassword = false,
20 pageSupplier = { webInterface ->
21 GetReplyAjaxPage(webInterface, "<%core>\n<%request>\n<%reply.text>\n<%currentSone>".asTemplate())
25 fun `request without reply id results in invalid-reply-id`() {
26 assertThatJsonFailed("invalid-reply-id")
30 fun `request with valid reply id results in reply json`() {
31 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
32 val reply = mock<PostReply>().apply {
33 whenever(id).thenReturn("reply-id")
34 whenever(this.sone).thenReturn(sone)
35 whenever(postId).thenReturn("post-id")
36 whenever(time).thenReturn(1000)
37 whenever(text).thenReturn("reply text")
39 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
41 addRequestParameter("reply", "reply-id")
42 assertThatJsonIsSuccessful()
43 assertThat(json["reply"]!!["id"].asText(), equalTo("reply-id"))
44 assertThat(json["reply"]!!["soneId"].asText(), equalTo("sone-id"))
45 assertThat(json["reply"]!!["postId"].asText(), equalTo("post-id"))
46 assertThat(json["reply"]!!["time"].asLong(), equalTo(1000L))
47 assertThat(json["reply"]!!["html"].asText(), equalTo(listOf(
49 freenetRequest.toString(),
51 currentSone.toString()
52 ).joinToString("\n")))
56 fun `page can be created by dependency injection`() {
57 assertThat(baseInjector.getInstance<GetReplyAjaxPage>(), notNullValue())