Replace JSON return object with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetPostAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
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
12 import org.junit.Test
13
14 /**
15  * Unit test for [GetPostAjaxPage].
16  */
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())
20                 }) {
21
22         @Test
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"))
26         }
27
28         @Test
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")
37                 }
38                 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
39                 addPost(post)
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(
47                                 core.toString(),
48                                 freenetRequest.toString(),
49                                 "post text",
50                                 currentSone.toString(),
51                                 core.localSones.toString()
52                 ).joinToString("\n")))
53         }
54
55 }