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