1 package net.pterodactylus.sone.web
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.GET
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Before
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
17 * Unit test for [DeleteReplyPage].
19 class DeleteReplyPageTest : WebPageTest() {
21 private val page = DeleteReplyPage(template, webInterface)
23 private val sone = mock<Sone>()
24 private val reply = mock<PostReply>()
26 override fun getPage() = page
30 whenever(sone.isLocal).thenReturn(true)
31 whenever(reply.sone).thenReturn(sone)
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo("deleteReply.html"))
40 fun `page requires login`() {
41 assertThat(page.requiresLogin(), equalTo(true))
45 fun `get request sets reply ID and return page in template context`() {
47 addHttpRequestParameter("reply", "reply-id")
48 addHttpRequestParameter("returnPage", "return.html")
49 page.processTemplate(freenetRequest, templateContext)
50 assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
51 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
55 fun `post request without any action sets reply ID and return page in template context`() {
57 addPostReply("reply-id", reply)
58 addHttpRequestParameter("reply", "reply-id")
59 addHttpRequestParameter("returnPage", "return.html")
60 page.processTemplate(freenetRequest, templateContext)
61 assertThat(templateContext["reply"], equalTo<Any>("reply-id"))
62 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
66 fun `trying to delete a reply with an invalid ID results in no permission page`() {
68 verifyRedirect("noPermission.html")
72 fun `trying to delete a reply from a non-local sone results in no permission page`() {
74 addHttpRequestParameter("reply", "reply-id")
75 whenever(sone.isLocal).thenReturn(false)
76 addPostReply("reply-id", reply)
77 verifyRedirect("noPermission.html")
81 fun `confirming deletion of reply deletes the reply and redirects to return page`() {
83 addPostReply("reply-id", reply)
84 addHttpRequestParameter("reply", "reply-id")
85 addHttpRequestParameter("returnPage", "return.html")
86 addHttpRequestParameter("confirmDelete", "true")
87 verifyRedirect("return.html") {
88 verify(core).deleteReply(reply)
93 fun `aborting deletion of reply redirects to return page`() {
95 addPostReply("reply-id", reply)
96 addHttpRequestParameter("reply", "reply-id")
97 addHttpRequestParameter("returnPage", "return.html")
98 addHttpRequestParameter("abortDelete", "true")
99 verifyRedirect("return.html") {
100 verify(core, never()).deleteReply(reply)