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