Convert view Sone page test to new web page test base
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / DeleteReplyPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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
11 import org.junit.Test
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [DeleteReplyPage].
17  */
18 class DeleteReplyPageTest : WebPageTest() {
19
20         private val page = DeleteReplyPage(template, webInterface)
21
22         private val sone = mock<Sone>()
23         private val reply = mock<PostReply>()
24
25         override fun getPage() = page
26
27         @Before
28         fun setupReply() {
29                 whenever(sone.isLocal).thenReturn(true)
30                 whenever(reply.sone).thenReturn(sone)
31         }
32
33         @Test
34         fun `page returns correct path`() {
35             assertThat(page.path, equalTo("deleteReply.html"))
36         }
37
38         @Test
39         fun `page requires login`() {
40             assertThat(page.requiresLogin(), equalTo(true))
41         }
42
43         @Test
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"))
50         }
51
52         @Test
53         fun `post request without any action sets reply ID and return page in template context`() {
54                 setMethod(POST)
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"))
61         }
62
63         @Test
64         fun `trying to delete a reply with an invalid ID results in no permission page`() {
65                 setMethod(POST)
66                 verifyRedirect("noPermission.html")
67         }
68
69         @Test
70         fun `trying to delete a reply from a non-local sone results in no permission page`() {
71                 setMethod(POST)
72                 addHttpRequestPart("reply", "reply-id")
73                 whenever(sone.isLocal).thenReturn(false)
74                 addPostReply("reply-id", reply)
75                 verifyRedirect("noPermission.html")
76         }
77
78         @Test
79         fun `confirming deletion of reply deletes the reply and redirects to return page`() {
80                 setMethod(POST)
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)
87                 }
88         }
89
90         @Test
91         fun `aborting deletion of reply redirects to return page`() {
92                 setMethod(POST)
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)
99                 }
100         }
101
102 }