Add test for DI constructability of CreateReplyAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / CreateReplyAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.baseInjector
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.notNullValue
13 import org.junit.Test
14
15 /**
16  * Unit test for [CreateReplyAjaxPage].
17  */
18 class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = ::CreateReplyAjaxPage) {
19
20         @Test
21         fun `invalid post ID results in error message`() {
22                 assertThatJsonFailed("invalid-post-id")
23         }
24
25         @Test
26         fun `valid post ID results in created reply`() {
27                 val post = mock<Post>()
28                 addPost(post, "post-id")
29                 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
30                 whenever(core.createReply(currentSone, post, "")).thenReturn(reply)
31             addRequestParameter("post", "post-id")
32                 assertThatJsonIsSuccessful()
33                 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
34                 assertThat(json["sone"]?.asText(), equalTo("soneId"))
35         }
36
37         @Test
38         fun `text is filtered when creating reply`() {
39                 val post = mock<Post>()
40                 addPost(post, "post-id")
41                 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
42                 whenever(core.createReply(currentSone, post, "Text with KSK@foo.bar link")).thenReturn(reply)
43                 addRequestParameter("post", "post-id")
44                 addRequestParameter("text", "Text with http://127.0.0.1:8888/KSK@foo.bar link")
45                 addRequestHeader("Host", "127.0.0.1:8888")
46                 assertThatJsonIsSuccessful()
47                 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
48                 assertThat(json["sone"]?.asText(), equalTo("soneId"))
49         }
50
51         @Test
52         fun `sender can be chosen from local sones`() {
53             val sone = mock<Sone>().apply { whenever(id).thenReturn("local-sone") }
54                 addLocalSone(sone)
55                 val post = mock<Post>()
56                 addPost(post, "post-id")
57                 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
58                 whenever(core.createReply(sone, post, "Text")).thenReturn(reply)
59                 addRequestParameter("post", "post-id")
60                 addRequestParameter("text", "Text")
61                 addRequestParameter("sender", "local-sone")
62                 assertThatJsonIsSuccessful()
63                 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
64                 assertThat(json["sone"]?.asText(), equalTo("local-sone"))
65         }
66
67         @Test
68         fun `page can be created by dependency injection`() {
69             assertThat(baseInjector.getInstance<CreateReplyAjaxPage>(), notNullValue())
70         }
71
72 }