1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
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.verify
16 * Unit test for [DeleteImagePage].
18 class DeleteImagePageTest: WebPageTest() {
20 private val page = DeleteImagePage(template, webInterface)
21 private val image = mock<Image>()
22 private val sone = mock<Sone>()
24 override fun getPage() = page
28 val album = mock<Album>()
29 whenever(album.id).thenReturn("album-id")
30 whenever(image.id).thenReturn("image-id")
31 whenever(image.sone).thenReturn(sone)
32 whenever(image.album).thenReturn(album)
33 whenever(sone.isLocal).thenReturn(true)
37 fun `page returns correct path`() {
38 assertThat(page.path, equalTo("deleteImage.html"))
42 fun `page requires login`() {
43 assertThat(page.requiresLogin(), equalTo(true))
47 fun `get request with invalid image redirects to invalid page`() {
48 verifyRedirect("invalid.html")
52 fun `get request with image from non-local sone redirects to no permissions page`() {
53 whenever(sone.isLocal).thenReturn(false)
54 addImage("image-id", image)
55 addHttpRequestParameter("image", "image-id")
56 verifyRedirect("noPermission.html")
60 fun `get request with image from local sone sets image in template context`() {
61 addImage("image-id", image)
62 addHttpRequestParameter("image", "image-id")
63 page.processTemplate(freenetRequest, templateContext)
64 assertThat(templateContext["image"], equalTo<Any>(image))
68 fun `post request with abort delete flag set redirects to image browser`() {
70 addImage("image-id", image)
71 addHttpRequestPart("image", "image-id")
72 addHttpRequestPart("abortDelete", "true")
73 verifyRedirect("imageBrowser.html?image=image-id")
77 fun `post request deletes image and redirects to image browser`() {
79 addImage("image-id", image)
80 addHttpRequestPart("image", "image-id")
81 verifyRedirect("imageBrowser.html?album=album-id") {
82 verify(webInterface.core).deleteImage(image)