b79e6df2f08c3e9edcfa44829e7bb3df7d1db123
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / CreatePostAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import com.google.common.base.Optional
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.utils.asOptional
10 import net.pterodactylus.sone.web.baseInjector
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.notNullValue
14 import org.hamcrest.Matchers.nullValue
15 import org.junit.Test
16
17 /**
18  * Unit test for [CreatePostAjaxPage].
19  */
20 class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::CreatePostAjaxPage) {
21
22         @Test
23         fun `missing text parameter returns error`() {
24                 assertThatJsonFailed("text-required")
25         }
26
27         @Test
28         fun `empty text returns error`() {
29                 addRequestParameter("text", "")
30                 assertThatJsonFailed("text-required")
31         }
32
33         @Test
34         fun `whitespace-only text returns error`() {
35                 addRequestParameter("text", "  ")
36                 assertThatJsonFailed("text-required")
37         }
38
39         @Test
40         fun `request with valid data creates post`() {
41                 addRequestParameter("text", "test")
42                 val post = createPost()
43                 whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
44                 assertThatJsonIsSuccessful()
45                 assertThat(json["postId"]?.asText(), equalTo("id"))
46                 assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
47                 assertThat(json["recipient"], nullValue())
48         }
49
50         @Test
51         fun `request with invalid recipient creates post without recipient`() {
52                 addRequestParameter("text", "test")
53                 addRequestParameter("recipient", "invalid")
54                 val post = createPost()
55                 whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
56                 assertThatJsonIsSuccessful()
57                 assertThat(json["postId"]?.asText(), equalTo("id"))
58                 assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
59                 assertThat(json["recipient"], nullValue())
60         }
61
62         @Test
63         fun `request with valid data and recipient creates correct post`() {
64                 addRequestParameter("text", "test")
65                 addRequestParameter("recipient", "valid")
66                 val recipient = mock<Sone>().apply { whenever(id).thenReturn("valid") }
67                 addSone(recipient)
68                 val post = createPost("valid")
69                 whenever(core.createPost(currentSone, recipient, "test")).thenReturn(post)
70                 assertThatJsonIsSuccessful()
71                 assertThat(json["postId"]?.asText(), equalTo("id"))
72                 assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
73                 assertThat(json["recipient"]?.asText(), equalTo("valid"))
74         }
75
76         @Test
77         fun `text is filtered correctly`() {
78                 addRequestParameter("text", "Link http://freenet.test:8888/KSK@foo is filtered")
79                 addRequestHeader("Host", "freenet.test:8888")
80                 val post = createPost()
81                 whenever(core.createPost(currentSone, null, "Link KSK@foo is filtered")).thenReturn(post)
82                 assertThatJsonIsSuccessful()
83                 assertThat(json["postId"]?.asText(), equalTo("id"))
84                 assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
85                 assertThat(json["recipient"], nullValue())
86         }
87
88         private fun createPost(recipientId: String? = null) =
89                         mock<Post>().apply {
90                                 whenever(id).thenReturn("id")
91                                 whenever(sone).thenReturn(currentSone)
92                                 whenever(this.recipientId).thenReturn(recipientId.asOptional())
93                         }
94
95         @Test
96         fun `page can be created by dependency injection`() {
97             assertThat(baseInjector.getInstance<CreatePostAjaxPage>(), notNullValue())
98         }
99
100 }