1 package net.pterodactylus.sone.web
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.sone.web.WebTestUtils.redirectsTo
9 import net.pterodactylus.util.web.Method.GET
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.junit.Before
15 import org.mockito.Mockito.verify
16 import kotlin.test.fail
19 * Unit test for [DeleteImagePage].
21 class DeleteImagePageTest : WebPageTest() {
23 private val page = DeleteImagePage(template, webInterface)
24 private val image = mock<Image>()
25 private val sone = mock<Sone>()
29 val album = mock<Album>()
30 whenever(album.id).thenReturn("album-id")
31 whenever(image.id).thenReturn("image-id")
32 whenever(image.sone).thenReturn(sone)
33 whenever(image.album).thenReturn(album)
34 whenever(sone.isLocal).thenReturn(true)
38 fun `get request with invalid image redirects to invalid page`() {
40 expectedException.expect(redirectsTo("invalid.html"))
41 page.processTemplate(freenetRequest, templateContext)
45 fun `get request with image from non-local sone redirects to no permissions page`() {
47 whenever(sone.isLocal).thenReturn(false)
48 addImage("image-id", image)
49 addHttpRequestParameter("image", "image-id")
50 expectedException.expect(redirectsTo("noPermission.html"))
51 page.processTemplate(freenetRequest, templateContext)
55 fun `get request with image from local sone sets image in template context`() {
57 addImage("image-id", image)
58 addHttpRequestParameter("image", "image-id")
59 page.processTemplate(freenetRequest, templateContext)
60 assertThat(templateContext["image"], equalTo<Any>(image))
64 fun `post request with abort delete flag set redirects to image browser`() {
66 addImage("image-id", image)
67 addHttpRequestParameter("image", "image-id")
68 addHttpRequestParameter("abortDelete", "true")
69 expectedException.expect(redirectsTo("imageBrowser.html?image=image-id"))
70 page.processTemplate(freenetRequest, templateContext)
74 fun `post request deletes image and redirects to image browser`() {
76 addImage("image-id", image)
77 addHttpRequestParameter("image", "image-id")
78 expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"))
80 page.processTemplate(freenetRequest, templateContext)
82 } catch (e: Exception) {
83 verify(webInterface.core).deleteImage(image)