4cd67c9ba242534d5b88b748f55d76b3b4622912
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / CreateReplyPageTest.kt
1 package net.pterodactylus.sone.web.pages
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.web.baseInjector
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.hamcrest.Matchers.notNullValue
12 import org.junit.Test
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [CreateReplyPage].
17  */
18 class CreateReplyPageTest: WebPageTest(::CreateReplyPage) {
19
20         @Test
21         fun `page returns correct path`() {
22                 assertThat(page.path, equalTo("createReply.html"))
23         }
24
25         @Test
26         fun `page requires login`() {
27                 assertThat(page.requiresLogin(), equalTo(true))
28         }
29
30         @Test
31         fun `reply is created correctly`() {
32                 setMethod(POST)
33                 addHttpRequestPart("returnPage", "return.html")
34                 addHttpRequestPart("post", "post-id")
35                 addHttpRequestPart("text", "new text")
36                 val post = mock<Post>().apply { addPost("post-id", this) }
37                 verifyRedirect("return.html") {
38                         verify(core).createReply(currentSone, post, "new text")
39                 }
40         }
41
42         @Test
43         fun `reply is filtered`() {
44                 setMethod(POST)
45                 addHttpRequestPart("returnPage", "return.html")
46                 addHttpRequestPart("post", "post-id")
47                 addHttpRequestPart("text", "new http://localhost:12345/KSK@foo text")
48                 addHttpRequestHeader("Host", "localhost:12345")
49                 val post = mock<Post>().apply { addPost("post-id", this) }
50                 verifyRedirect("return.html") {
51                         verify(core).createReply(currentSone, post, "new KSK@foo text")
52                 }
53         }
54
55         @Test
56         fun `reply is created with correct sender`() {
57                 setMethod(POST)
58                 addHttpRequestPart("returnPage", "return.html")
59                 addHttpRequestPart("post", "post-id")
60                 addHttpRequestPart("text", "new text")
61                 addHttpRequestPart("sender", "sender-id")
62                 val sender = mock<Sone>().apply { addLocalSone("sender-id", this) }
63                 val post = mock<Post>().apply { addPost("post-id", this) }
64                 verifyRedirect("return.html") {
65                         verify(core).createReply(sender, post, "new text")
66                 }
67         }
68
69         @Test
70         fun `empty text sets parameters in template contexty`() {
71                 setMethod(POST)
72                 addHttpRequestPart("returnPage", "return.html")
73                 addHttpRequestPart("post", "post-id")
74                 addHttpRequestPart("text", "  ")
75                 page.processTemplate(freenetRequest, templateContext)
76                 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
77                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
78                 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
79                 assertThat(templateContext["text"], equalTo<Any>(""))
80         }
81
82         @Test
83         fun `user is redirected to no permissions page if post does not exist`() {
84                 setMethod(POST)
85                 addHttpRequestPart("returnPage", "return.html")
86                 addHttpRequestPart("post", "post-id")
87                 addHttpRequestPart("text", "new text")
88                 verifyRedirect("noPermission.html")
89         }
90
91         @Test
92         fun `page can be created by dependency injection`() {
93             assertThat(baseInjector.getInstance<CreateReplyPage>(), notNullValue())
94         }
95
96 }