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(::CreateReplyAjaxPage) {
18 fun `page returns correct path`() {
19 assertThat(page.path, equalTo("createReply.ajax"))
23 fun `page needs form password`() {
24 assertThat(page.needsFormPassword(), equalTo(true))
28 fun `page requires login`() {
29 assertThat(page.requiresLogin(), equalTo(true))
33 fun `invalid post ID results in error message`() {
34 assertThat(json.isSuccess, equalTo(false))
35 assertThat(json.error, equalTo("invalid-post-id"))
39 fun `valid post ID results in created reply`() {
40 val post = mock<Post>()
41 addPost("post-id", post)
42 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
43 whenever(core.createReply(currentSone, post, "")).thenReturn(reply)
44 addRequestParameter("post", "post-id")
45 assertThat(json.isSuccess, equalTo(true))
46 assertThat(json["reply"].asText(), equalTo("reply-id"))
47 assertThat(json["sone"].asText(), equalTo("soneId"))
51 fun `text is filtered when creating reply`() {
52 val post = mock<Post>()
53 addPost("post-id", post)
54 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
55 whenever(core.createReply(currentSone, post, "Text with KSK@foo.bar link")).thenReturn(reply)
56 addRequestParameter("post", "post-id")
57 addRequestParameter("text", "Text with http://127.0.0.1:8888/KSK@foo.bar link")
58 addRequestHeader("Host", "127.0.0.1:8888")
59 assertThat(json.isSuccess, equalTo(true))
60 assertThat(json["reply"].asText(), equalTo("reply-id"))
61 assertThat(json["sone"].asText(), equalTo("soneId"))
65 fun `sender can be chosen from local sones`() {
66 val sone = mock<Sone>().apply { whenever(id).thenReturn("local-sone") }
67 addLocalSone("local-sone", sone)
68 val post = mock<Post>()
69 addPost("post-id", post)
70 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
71 whenever(core.createReply(sone, post, "Text")).thenReturn(reply)
72 addRequestParameter("post", "post-id")
73 addRequestParameter("text", "Text")
74 addRequestParameter("sender", "local-sone")
75 assertThat(json.isSuccess, equalTo(true))
76 assertThat(json["reply"].asText(), equalTo("reply-id"))
77 assertThat(json["sone"].asText(), equalTo("local-sone"))