1 package net.pterodactylus.sone.web.pages
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
12 import org.mockito.Mockito.never
13 import org.mockito.Mockito.verify
16 * Unit test for [DeletePostPage].
18 class DeletePostPageTest : WebPageTest() {
20 private val page = DeletePostPage(template, webInterface)
22 private val post = mock<Post>()
23 private val sone = mock<Sone>()
25 override fun getPage() = page
29 whenever(post.sone).thenReturn(sone)
30 whenever(sone.isLocal).thenReturn(true)
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("deletePost.html"))
39 fun `page requires login`() {
40 assertThat(page.requiresLogin(), equalTo(true))
44 fun `get request with invalid post redirects to no permission page`() {
45 verifyRedirect("noPermission.html")
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"))
59 fun `post request with invalid post redirects to no permission page`() {
61 verifyRedirect("noPermission.html")
65 fun `post request with post from non-local sone redirects to no permission page`() {
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")
75 fun `post request with confirmation deletes post and redirects to return page`() {
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)
87 fun `post request with abort delete does not delete post and redirects to return page`() {
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)
99 fun `post request without delete or abort sets post in template context`() {
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"))