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