1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.PostReply
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Before
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
16 * Unit test for [DeleteReplyPage].
18 class DeleteReplyPageTest : WebPageTest() {
20 private val page = DeleteReplyPage(template, webInterface)
22 private val sone = mock<Sone>()
23 private val reply = mock<PostReply>()
25 override fun getPage() = page
29 whenever(sone.isLocal).thenReturn(true)
30 whenever(reply.sone).thenReturn(sone)
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("deleteReply.html"))
39 fun `page requires login`() {
40 assertThat(page.requiresLogin(), equalTo(true))
44 fun `get request sets reply ID and return page in template context`() {
45 addHttpRequestParameter("reply", "reply-id")
46 addHttpRequestParameter("returnPage", "return.html")
47 page.processTemplate(freenetRequest, templateContext)
48 assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
49 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
53 fun `post request without any action sets reply ID and return page in template context`() {
55 addPostReply("reply-id", reply)
56 addHttpRequestPart("reply", "reply-id")
57 addHttpRequestPart("returnPage", "return.html")
58 page.processTemplate(freenetRequest, templateContext)
59 assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
60 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
64 fun `trying to delete a reply with an invalid ID results in no permission page`() {
66 verifyRedirect("noPermission.html")
70 fun `trying to delete a reply from a non-local sone results in no permission page`() {
72 addHttpRequestPart("reply", "reply-id")
73 whenever(sone.isLocal).thenReturn(false)
74 addPostReply("reply-id", reply)
75 verifyRedirect("noPermission.html")
79 fun `confirming deletion of reply deletes the reply and redirects to return page`() {
81 addPostReply("reply-id", reply)
82 addHttpRequestPart("reply", "reply-id")
83 addHttpRequestPart("returnPage", "return.html")
84 addHttpRequestPart("confirmDelete", "true")
85 verifyRedirect("return.html") {
86 verify(core).deleteReply(reply)
91 fun `aborting deletion of reply redirects to return page`() {
93 addPostReply("reply-id", reply)
94 addHttpRequestPart("reply", "reply-id")
95 addHttpRequestPart("returnPage", "return.html")
96 addHttpRequestPart("abortDelete", "true")
97 verifyRedirect("return.html") {
98 verify(core, never()).deleteReply(reply)