1 package net.pterodactylus.sone.web
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
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.verify
17 * Unit test for [DeletePostPage].
19 class DeletePostPageTest : WebPageTest() {
21 private val page = DeletePostPage(template, webInterface)
23 private val post = mock<Post>()
24 private val sone = mock<Sone>()
26 override fun getPage() = page
30 whenever(post.sone).thenReturn(sone)
31 whenever(sone.isLocal).thenReturn(true)
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo("deletePost.html"))
40 fun `page requires login`() {
41 assertThat(page.requiresLogin(), equalTo(true))
45 fun `get request with invalid post redirects to no permission page`() {
47 verifyRedirect("noPermission.html")
51 fun `get request with valid post sets post and return page in template context`() {
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"))
62 fun `post request with invalid post redirects to no permission page`() {
64 verifyRedirect("noPermission.html")
68 fun `post request with post from non-local sone redirects to no permission page`() {
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")
78 fun `post request with confirmation deletes post and redirects to return page`() {
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)
90 fun `post request with abort delete does not delete post and redirects to return page`() {
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)
102 fun `post request without delete or abort sets post in template context`() {
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"))