Add test for DI constructability of GetPostAjaxPage
[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 org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Test
16
17 /**
18  * Unit test for [GetPostAjaxPage].
19  */
20 class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false,
21                 pageSupplier = { webInterface ->
22                         GetPostAjaxPage(webInterface, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate())
23                 }) {
24
25         @Test
26         fun `request with missing post results in invalid-post-id`() {
27                 assertThatJsonFailed("invalid-post-id")
28         }
29
30         @Test
31         fun `request with valid post results in post json`() {
32                 val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
33                 val post = mock<Post>().apply {
34                         whenever(id).thenReturn("post-id")
35                         whenever(time).thenReturn(1000)
36                         whenever(this.sone).thenReturn(sone)
37                         whenever(recipientId).thenReturn("recipient-id".asOptional())
38                         whenever(text).thenReturn("post text")
39                 }
40                 webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
41                 addPost(post)
42                 addRequestParameter("post", "post-id")
43                 assertThatJsonIsSuccessful()
44                 assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
45                 assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
46                 assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
47                 assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id"))
48                 assertThat(json["post"]!!["html"].asText(), equalTo(listOf(
49                                 core.toString(),
50                                 freenetRequest.toString(),
51                                 "post text",
52                                 currentSone.toString(),
53                                 core.localSones.toString()
54                 ).joinToString("\n")))
55         }
56
57         @Test
58         fun `page can be created dependency injection`() {
59             assertThat(baseInjector.getInstance<GetPostAjaxPage>(), notNullValue())
60         }
61
62 }