1 package net.pterodactylus.sone.web.ajax
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
18 * Unit test for [CreatePostAjaxPage].
20 class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::CreatePostAjaxPage) {
23 fun `missing text parameter returns error`() {
24 assertThatJsonFailed("text-required")
28 fun `empty text returns error`() {
29 addRequestParameter("text", "")
30 assertThatJsonFailed("text-required")
34 fun `whitespace-only text returns error`() {
35 addRequestParameter("text", " ")
36 assertThatJsonFailed("text-required")
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())
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())
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") }
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"))
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())
88 private fun createPost(recipientId: String? = null) =
90 whenever(id).thenReturn("id")
91 whenever(sone).thenReturn(currentSone)
92 whenever(this.recipientId).thenReturn(recipientId.asOptional())
96 fun `page can be created by dependency injection`() {
97 assertThat(baseInjector.getInstance<CreatePostAjaxPage>(), notNullValue())