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