Remove obsolete loading animation
[Sone.git] / src / test / java / net / pterodactylus / sone / web / CreateReplyPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.util.web.Method.POST
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.equalTo
9 import org.junit.Test
10 import org.mockito.Mockito.verify
11
12 /**
13  * Unit test for [CreateReplyPage].
14  */
15 class CreateReplyPageTest: WebPageTest() {
16
17         private val page = CreateReplyPage(template, webInterface)
18         override fun getPage() = page
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                 request("", POST)
33                 addHttpRequestParameter("returnPage", "return.html")
34                 addHttpRequestParameter("post", "post-id")
35                 addHttpRequestParameter("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                 request("", POST)
45                 addHttpRequestParameter("returnPage", "return.html")
46                 addHttpRequestParameter("post", "post-id")
47                 addHttpRequestParameter("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                 request("", POST)
58                 addHttpRequestParameter("returnPage", "return.html")
59                 addHttpRequestParameter("post", "post-id")
60                 addHttpRequestParameter("text", "new text")
61                 addHttpRequestParameter("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                 request("", POST)
72                 addHttpRequestParameter("returnPage", "return.html")
73                 addHttpRequestParameter("post", "post-id")
74                 addHttpRequestParameter("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                 request("", POST)
85                 addHttpRequestParameter("returnPage", "return.html")
86                 addHttpRequestParameter("post", "post-id")
87                 addHttpRequestParameter("text", "new text")
88                 verifyRedirect("noPermission.html")
89         }
90
91         @Test
92         fun `get request stores parameters in template context`() {
93                 addHttpRequestParameter("returnPage", "return.html")
94                 addHttpRequestParameter("post", "post-id")
95                 addHttpRequestParameter("text", "new text")
96                 page.processTemplate(freenetRequest, templateContext)
97                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
98                 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
99                 assertThat(templateContext["text"], equalTo<Any>("new text"))
100         }
101
102 }