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