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 addHttpRequestParameter("returnPage", "return.html")
36 addHttpRequestParameter("post", "post-id")
37 addHttpRequestParameter("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 addHttpRequestParameter("returnPage", "return.html")
48 addHttpRequestParameter("post", "post-id")
49 addHttpRequestParameter("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 addHttpRequestParameter("returnPage", "return.html")
61 addHttpRequestParameter("post", "post-id")
62 addHttpRequestParameter("text", "new text")
63 addHttpRequestParameter("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 addHttpRequestParameter("returnPage", "return.html")
75 addHttpRequestParameter("post", "post-id")
76 addHttpRequestParameter("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 addHttpRequestParameter("returnPage", "return.html")
88 addHttpRequestParameter("post", "post-id")
89 addHttpRequestParameter("text", "new text")
90 verifyRedirect("noPermission.html")
94 fun `get request stores parameters in template context`() {
95 addHttpRequestParameter("returnPage", "return.html")
96 addHttpRequestParameter("post", "post-id")
97 addHttpRequestParameter("text", "new text")
98 page.processTemplate(freenetRequest, templateContext)
99 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
100 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
101 assertThat(templateContext["text"], equalTo<Any>("new text"))