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