1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
13 * Unit test for [CreateReplyAjaxPage].
15 class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = ::CreateReplyAjaxPage) {
18 fun `invalid post ID results in error message`() {
19 assertThatJsonFailed("invalid-post-id")
23 fun `valid post ID results in created reply`() {
24 val post = mock<Post>()
25 addPost(post, "post-id")
26 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
27 whenever(core.createReply(currentSone, post, "")).thenReturn(reply)
28 addRequestParameter("post", "post-id")
29 assertThatJsonIsSuccessful()
30 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
31 assertThat(json["sone"]?.asText(), equalTo("soneId"))
35 fun `text is filtered when creating reply`() {
36 val post = mock<Post>()
37 addPost(post, "post-id")
38 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
39 whenever(core.createReply(currentSone, post, "Text with KSK@foo.bar link")).thenReturn(reply)
40 addRequestParameter("post", "post-id")
41 addRequestParameter("text", "Text with http://127.0.0.1:8888/KSK@foo.bar link")
42 addRequestHeader("Host", "127.0.0.1:8888")
43 assertThatJsonIsSuccessful()
44 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
45 assertThat(json["sone"]?.asText(), equalTo("soneId"))
49 fun `sender can be chosen from local sones`() {
50 val sone = mock<Sone>().apply { whenever(id).thenReturn("local-sone") }
52 val post = mock<Post>()
53 addPost(post, "post-id")
54 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
55 whenever(core.createReply(sone, post, "Text")).thenReturn(reply)
56 addRequestParameter("post", "post-id")
57 addRequestParameter("text", "Text")
58 addRequestParameter("sender", "local-sone")
59 assertThatJsonIsSuccessful()
60 assertThat(json["reply"]?.asText(), equalTo("reply-id"))
61 assertThat(json["sone"]?.asText(), equalTo("local-sone"))