🎨 Remove template context factory from web interface API
[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.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
16 import org.junit.Test
17
18 /**
19  * Unit test for [GetPostAjaxPage].
20  */
21 class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false) {
22
23         private val templateContextFactory = TemplateContextFactory().apply {
24                 addAccessor(Any::class.java, ReflectionAccessor())
25         }
26         override val page: JsonPage by lazy { GetPostAjaxPage(webInterface, templateContextFactory, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate()) }
27
28         @Test
29         fun `request with missing post results in invalid-post-id`() {
30                 assertThatJsonFailed("invalid-post-id")
31         }
32
33         @Test
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")
42                 }
43                 addPost(post)
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(
51                                 core.toString(),
52                                 freenetRequest.toString(),
53                                 "post text",
54                                 currentSone.toString(),
55                                 core.localSones.toString()
56                 ).joinToString("\n")))
57         }
58
59         @Test
60         fun `page can be created dependency injection`() {
61             assertThat(baseInjector.getInstance<GetPostAjaxPage>(), notNullValue())
62         }
63
64 }