1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.web.pages.WebPageTest
7 import net.pterodactylus.sone.web.pages.CreateReplyPage
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
12 import org.mockito.Mockito.verify
15 * Unit test for [CreateReplyPage].
17 class CreateReplyPageTest: WebPageTest() {
19 private val page = CreateReplyPage(template, webInterface)
20 override fun getPage() = page
23 fun `page returns correct path`() {
24 assertThat(page.path, equalTo("createReply.html"))
28 fun `page requires login`() {
29 assertThat(page.requiresLogin(), equalTo(true))
33 fun `reply is created correctly`() {
35 addHttpRequestPart("returnPage", "return.html")
36 addHttpRequestPart("post", "post-id")
37 addHttpRequestPart("text", "new text")
38 val post = mock<Post>().apply { addPost("post-id", this) }
39 verifyRedirect("return.html") {
40 verify(core).createReply(currentSone, post, "new text")
45 fun `reply is filtered`() {
47 addHttpRequestPart("returnPage", "return.html")
48 addHttpRequestPart("post", "post-id")
49 addHttpRequestPart("text", "new http://localhost:12345/KSK@foo text")
50 addHttpRequestHeader("Host", "localhost:12345")
51 val post = mock<Post>().apply { addPost("post-id", this) }
52 verifyRedirect("return.html") {
53 verify(core).createReply(currentSone, post, "new KSK@foo text")
58 fun `reply is created with correct sender`() {
60 addHttpRequestPart("returnPage", "return.html")
61 addHttpRequestPart("post", "post-id")
62 addHttpRequestPart("text", "new text")
63 addHttpRequestPart("sender", "sender-id")
64 val sender = mock<Sone>().apply { addLocalSone("sender-id", this) }
65 val post = mock<Post>().apply { addPost("post-id", this) }
66 verifyRedirect("return.html") {
67 verify(core).createReply(sender, post, "new text")
72 fun `empty text sets parameters in template contexty`() {
74 addHttpRequestPart("returnPage", "return.html")
75 addHttpRequestPart("post", "post-id")
76 addHttpRequestPart("text", " ")
77 page.processTemplate(freenetRequest, templateContext)
78 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
79 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
80 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
81 assertThat(templateContext["text"], equalTo<Any>(""))
85 fun `user is redirected to no permissions page if post does not exist`() {
87 addHttpRequestPart("returnPage", "return.html")
88 addHttpRequestPart("post", "post-id")
89 addHttpRequestPart("text", "new text")
90 verifyRedirect("noPermission.html")