Replace web page test base with Kotlin version
[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.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(::CreateReplyPage) {
16
17         @Test
18         fun `page returns correct path`() {
19                 assertThat(page.path, equalTo("createReply.html"))
20         }
21
22         @Test
23         fun `page requires login`() {
24                 assertThat(page.requiresLogin(), equalTo(true))
25         }
26
27         @Test
28         fun `reply is created correctly`() {
29                 setMethod(POST)
30                 addHttpRequestPart("returnPage", "return.html")
31                 addHttpRequestPart("post", "post-id")
32                 addHttpRequestPart("text", "new text")
33                 val post = mock<Post>().apply { addPost("post-id", this) }
34                 verifyRedirect("return.html") {
35                         verify(core).createReply(currentSone, post, "new text")
36                 }
37         }
38
39         @Test
40         fun `reply is filtered`() {
41                 setMethod(POST)
42                 addHttpRequestPart("returnPage", "return.html")
43                 addHttpRequestPart("post", "post-id")
44                 addHttpRequestPart("text", "new http://localhost:12345/KSK@foo text")
45                 addHttpRequestHeader("Host", "localhost:12345")
46                 val post = mock<Post>().apply { addPost("post-id", this) }
47                 verifyRedirect("return.html") {
48                         verify(core).createReply(currentSone, post, "new KSK@foo text")
49                 }
50         }
51
52         @Test
53         fun `reply is created with correct sender`() {
54                 setMethod(POST)
55                 addHttpRequestPart("returnPage", "return.html")
56                 addHttpRequestPart("post", "post-id")
57                 addHttpRequestPart("text", "new text")
58                 addHttpRequestPart("sender", "sender-id")
59                 val sender = mock<Sone>().apply { addLocalSone("sender-id", this) }
60                 val post = mock<Post>().apply { addPost("post-id", this) }
61                 verifyRedirect("return.html") {
62                         verify(core).createReply(sender, post, "new text")
63                 }
64         }
65
66         @Test
67         fun `empty text sets parameters in template contexty`() {
68                 setMethod(POST)
69                 addHttpRequestPart("returnPage", "return.html")
70                 addHttpRequestPart("post", "post-id")
71                 addHttpRequestPart("text", "  ")
72                 page.processTemplate(freenetRequest, templateContext)
73                 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
74                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
75                 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
76                 assertThat(templateContext["text"], equalTo<Any>(""))
77         }
78
79         @Test
80         fun `user is redirected to no permissions page if post does not exist`() {
81                 setMethod(POST)
82                 addHttpRequestPart("returnPage", "return.html")
83                 addHttpRequestPart("post", "post-id")
84                 addHttpRequestPart("text", "new text")
85                 verifyRedirect("noPermission.html")
86         }
87
88 }