Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / CreatePostPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import com.google.common.base.Optional.absent
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.utils.asOptional
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Test
11 import org.mockito.Mockito.verify
12
13 /**
14  * Unit test for [CreatePostPage].
15  */
16 class CreatePostPageTest: WebPageTest(::CreatePostPage) {
17
18         @Test
19         fun `page returns correct path`() {
20                 assertThat(page.path, equalTo("createPost.html"))
21         }
22
23         @Test
24         fun `page requires login`() {
25                 assertThat(page.requiresLogin(), equalTo(true))
26         }
27
28         @Test
29         fun `return page is set in template context`() {
30                 addHttpRequestPart("returnPage", "return.html")
31                 page.processTemplate(freenetRequest, templateContext)
32                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
33         }
34
35         @Test
36         fun `post is created correctly`() {
37                 setMethod(POST)
38                 addHttpRequestPart("returnPage", "return.html")
39                 addHttpRequestPart("text", "post text")
40                 verifyRedirect("return.html") {
41                         verify(core).createPost(currentSone, absent(), "post text")
42                 }
43         }
44
45         @Test
46         fun `creating an empty post is denied`() {
47                 setMethod(POST)
48                 addHttpRequestPart("returnPage", "return.html")
49                 addHttpRequestPart("text", "  ")
50                 page.processTemplate(freenetRequest, templateContext)
51                 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
52         }
53
54         @Test
55         fun `a sender can be selected`() {
56                 setMethod(POST)
57                 addHttpRequestPart("returnPage", "return.html")
58                 addHttpRequestPart("text", "post text")
59                 addHttpRequestPart("sender", "sender-id")
60                 val sender = mock<Sone>()
61                 addLocalSone("sender-id", sender)
62                 verifyRedirect("return.html") {
63                         verify(core).createPost(sender, absent(), "post text")
64                 }
65         }
66
67         @Test
68         fun `a recipient can be selected`() {
69                 setMethod(POST)
70                 addHttpRequestPart("returnPage", "return.html")
71                 addHttpRequestPart("text", "post text")
72                 addHttpRequestPart("recipient", "recipient-id")
73                 val recipient = mock<Sone>()
74                 addSone("recipient-id", recipient)
75                 verifyRedirect("return.html") {
76                         verify(core).createPost(currentSone, recipient.asOptional(), "post text")
77                 }
78         }
79
80         @Test
81         fun `text is filtered correctly`() {
82                 setMethod(POST)
83                 addHttpRequestPart("returnPage", "return.html")
84                 addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text")
85                 addHttpRequestHeader("Host", "localhost:12345")
86                 verifyRedirect("return.html") {
87                         verify(core).createPost(currentSone, absent(), "post KSK@foo text")
88                 }
89         }
90
91 }