♻️ Refactor WOT connector
[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.*
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.web.Method.*
10 import org.hamcrest.MatcherAssert.*
11 import org.hamcrest.Matchers.*
12 import org.junit.*
13 import org.mockito.Mockito.*
14
15 /**
16  * Unit test for [CreatePostPage].
17  */
18 class CreatePostPageTest : WebPageTest(::CreatePostPage) {
19
20         @Test
21         fun `page returns correct path`() {
22                 assertThat(page.path, equalTo("createPost.html"))
23         }
24
25         @Test
26         fun `page requires login`() {
27                 assertThat(page.requiresLogin(), equalTo(true))
28         }
29
30         @Test
31         fun `return page is set in template context`() {
32                 addHttpRequestPart("returnPage", "return.html")
33                 page.processTemplate(freenetRequest, templateContext)
34                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
35         }
36
37         @Test
38         fun `post is created correctly`() {
39                 setMethod(POST)
40                 addHttpRequestPart("returnPage", "return.html")
41                 addHttpRequestPart("text", "post text")
42                 verifyRedirect("return.html") {
43                         verify(core).createPost(currentSone, null, "post text")
44                 }
45         }
46
47         @Test
48         fun `creating an empty post is denied`() {
49                 setMethod(POST)
50                 addHttpRequestPart("returnPage", "return.html")
51                 addHttpRequestPart("text", "  ")
52                 page.processTemplate(freenetRequest, templateContext)
53                 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
54         }
55
56         @Test
57         fun `a sender can be selected`() {
58                 setMethod(POST)
59                 addHttpRequestPart("returnPage", "return.html")
60                 addHttpRequestPart("text", "post text")
61                 addHttpRequestPart("sender", "sender-id")
62                 val sender = mock<Sone>()
63                 addLocalSone("sender-id", sender)
64                 verifyRedirect("return.html") {
65                         verify(core).createPost(sender, null, "post text")
66                 }
67         }
68
69         @Test
70         fun `a recipient can be selected`() {
71                 setMethod(POST)
72                 addHttpRequestPart("returnPage", "return.html")
73                 addHttpRequestPart("text", "post text")
74                 addHttpRequestPart("recipient", "recipient-id")
75                 val recipient = mock<Sone>()
76                 addSone("recipient-id", recipient)
77                 verifyRedirect("return.html") {
78                         verify(core).createPost(currentSone, recipient, "post text")
79                 }
80         }
81
82         @Test
83         fun `text is filtered correctly`() {
84                 setMethod(POST)
85                 addHttpRequestPart("returnPage", "return.html")
86                 addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text")
87                 addHttpRequestHeader("Host", "localhost:12345")
88                 verifyRedirect("return.html") {
89                         verify(core).createPost(currentSone, null, "post KSK@foo text")
90                 }
91         }
92
93         @Test
94         fun `page can be created by dependency injection`() {
95                 assertThat(baseInjector.getInstance<CreatePostPage>(), notNullValue())
96         }
97
98         @Test
99         fun `page is annotated with correct template path`() {
100                 assertThat(page.templatePath, equalTo("/templates/createPost.html"))
101         }
102
103 }