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.getInstance
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.web.baseInjector
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.web.Method.POST
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.notNullValue
14 import org.mockito.Mockito.verify
17 * Unit test for [CreateReplyPage].
19 class CreateReplyPageTest: WebPageTest(::CreateReplyPage) {
22 fun `page returns correct path`() {
23 assertThat(page.path, equalTo("createReply.html"))
27 fun `page requires login`() {
28 assertThat(page.requiresLogin(), equalTo(true))
32 fun `reply is created correctly`() {
34 addHttpRequestPart("returnPage", "return.html")
35 addHttpRequestPart("post", "post-id")
36 addHttpRequestPart("text", "new text")
37 val post = mock<Post>().apply { addPost("post-id", this) }
38 verifyRedirect("return.html") {
39 verify(core).createReply(currentSone, post, "new text")
44 fun `reply is filtered`() {
46 addHttpRequestPart("returnPage", "return.html")
47 addHttpRequestPart("post", "post-id")
48 addHttpRequestPart("text", "new http://localhost:12345/KSK@foo text")
49 addHttpRequestHeader("Host", "localhost:12345")
50 val post = mock<Post>().apply { addPost("post-id", this) }
51 verifyRedirect("return.html") {
52 verify(core).createReply(currentSone, post, "new KSK@foo text")
57 fun `reply is created with correct sender`() {
59 addHttpRequestPart("returnPage", "return.html")
60 addHttpRequestPart("post", "post-id")
61 addHttpRequestPart("text", "new text")
62 addHttpRequestPart("sender", "sender-id")
63 val sender = mock<Sone>().apply { addLocalSone("sender-id", this) }
64 val post = mock<Post>().apply { addPost("post-id", this) }
65 verifyRedirect("return.html") {
66 verify(core).createReply(sender, post, "new text")
71 fun `empty text sets parameters in template contexty`() {
73 addHttpRequestPart("returnPage", "return.html")
74 addHttpRequestPart("post", "post-id")
75 addHttpRequestPart("text", " ")
76 page.processTemplate(freenetRequest, templateContext)
77 assertThat(templateContext["errorTextEmpty"], equalTo<Any>(true))
78 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
79 assertThat(templateContext["postId"], equalTo<Any>("post-id"))
80 assertThat(templateContext["text"], equalTo<Any>(""))
84 fun `user is redirected to no permissions page if post does not exist`() {
86 addHttpRequestPart("returnPage", "return.html")
87 addHttpRequestPart("post", "post-id")
88 addHttpRequestPart("text", "new text")
89 verifyRedirect("noPermission.html")
93 fun `page can be created by dependency injection`() {
94 assertThat(baseInjector.getInstance<CreateReplyPage>(), notNullValue())
98 fun `page is annotated with correct template path`() {
99 assertThat(page.templatePath, equalTo("/templates/createReply.html"))