🔀 Merge branch 'release/v82'
[Sone.git] / 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                 assertThatJsonFailed("invalid-post-id")
25         }
26
27         @Test
28         fun `request with valid post results in post json`() {
29                 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
30                 val post = mock<Post>().apply {
31                         whenever(id).thenReturn("post-id")
32                         whenever(time).thenReturn(1000)
33                         whenever(this.sone).thenReturn(sone)
34                         whenever(recipientId).thenReturn("recipient-id".asOptional())
35                         whenever(text).thenReturn("post text")
36                 }
37                 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
38                 addPost(post)
39                 addRequestParameter("post", "post-id")
40                 assertThatJsonIsSuccessful()
41                 assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
42                 assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
43                 assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
44                 assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id"))
45                 assertThat(json["post"]!!["html"].asText(), equalTo(listOf(
46                                 core.toString(),
47                                 freenetRequest.toString(),
48                                 "post text",
49                                 currentSone.toString(),
50                                 core.localSones.toString()
51                 ).joinToString("\n")))
52         }
53
54 }