Add some tests and fix names of tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / DeletePostPageTest.kt
1 package net.pterodactylus.sone.web
2
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.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
12 import org.junit.Test
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [DeletePostPage].
18  */
19 class DeletePostPageTest : WebPageTest() {
20
21         private val page = DeletePostPage(template, webInterface)
22
23         private val post = mock<Post>()
24         private val sone = mock<Sone>()
25
26         override fun getPage() = page
27
28         @Before
29         fun setupPost() {
30                 whenever(post.sone).thenReturn(sone)
31                 whenever(sone.isLocal).thenReturn(true)
32         }
33
34         @Test
35         fun `page returns correct path`() {
36             assertThat(page.path, equalTo("deletePost.html"))
37         }
38
39         @Test
40         fun `page requires login`() {
41             assertThat(page.requiresLogin(), equalTo(true))
42         }
43
44         @Test
45         fun `get request with invalid post redirects to no permission page`() {
46                 request("", GET)
47                 verifyRedirect("noPermission.html")
48         }
49
50         @Test
51         fun `get request with valid post sets post and return page in template context`() {
52                 request("", GET)
53                 addPost("post-id", post)
54                 addHttpRequestParameter("post", "post-id")
55                 addHttpRequestParameter("returnPage", "return.html")
56                 page.processTemplate(freenetRequest, templateContext)
57                 assertThat(templateContext["post"], equalTo<Any>(post))
58                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
59         }
60
61         @Test
62         fun `post request with invalid post redirects to no permission page`() {
63                 request("", POST)
64                 verifyRedirect("noPermission.html")
65         }
66
67         @Test
68         fun `post request with post from non-local sone redirects to no permission page`() {
69                 request("", POST)
70                 whenever(sone.isLocal).thenReturn(false)
71                 addPost("post-id", post)
72                 addHttpRequestParameter("post", "post-id")
73                 addHttpRequestParameter("returnPage", "return.html")
74                 verifyRedirect("noPermission.html")
75         }
76
77         @Test
78         fun `post request with confirmation deletes post and redirects to return page`() {
79                 request("", POST)
80                 addPost("post-id", post)
81                 addHttpRequestParameter("post", "post-id")
82                 addHttpRequestParameter("returnPage", "return.html")
83                 addHttpRequestParameter("confirmDelete", "true")
84                 verifyRedirect("return.html") {
85                         verify(core).deletePost(post)
86                 }
87         }
88
89         @Test
90         fun `post request with abort delete does not delete post and redirects to return page`() {
91                 request("", POST)
92                 addPost("post-id", post)
93                 addHttpRequestParameter("post", "post-id")
94                 addHttpRequestParameter("returnPage", "return.html")
95                 addHttpRequestParameter("abortDelete", "true")
96                 verifyRedirect("return.html") {
97                         verify(core, never()).deletePost(post)
98                 }
99         }
100
101         @Test
102         fun `post request without delete or abort sets post in template context`() {
103                 request("", POST)
104                 addPost("post-id", post)
105                 addHttpRequestParameter("post", "post-id")
106                 addHttpRequestParameter("returnPage", "return.html")
107                 page.processTemplate(freenetRequest, templateContext)
108                 assertThat(templateContext["post"], equalTo<Any>(post))
109                 assertThat(templateContext["returnPage"], equalTo<Any>("return.html"))
110         }
111
112 }