Add unit test for distrust page
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / DeleteImagePageTest.kt
1 package net.pterodactylus.sone.web
2
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
14 import org.junit.Test
15 import org.mockito.Mockito.verify
16 import kotlin.test.fail
17
18 /**
19  * Unit test for [DeleteImagePage].
20  */
21 class DeleteImagePageTest : WebPageTest() {
22
23         private val page = DeleteImagePage(template, webInterface)
24         private val image = mock<Image>()
25         private val sone = mock<Sone>()
26
27         @Before
28         fun setupImage() {
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)
35         }
36
37         @Test
38         fun `get request with invalid image redirects to invalid page`() {
39                 request("", GET)
40                 expectedException.expect(redirectsTo("invalid.html"))
41                 page.processTemplate(freenetRequest, templateContext)
42         }
43
44         @Test
45         fun `get request with image from non-local sone redirects to no permissions page`() {
46                 request("", GET)
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)
52         }
53
54         @Test
55         fun `get request with image from local sone sets image in template context`() {
56                 request("", GET)
57                 addImage("image-id", image)
58                 addHttpRequestParameter("image", "image-id")
59                 page.processTemplate(freenetRequest, templateContext)
60                 assertThat(templateContext["image"], equalTo<Any>(image))
61         }
62
63         @Test
64         fun `post request with abort delete flag set redirects to image browser`() {
65                 request("", POST)
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)
71         }
72
73         @Test
74         fun `post request deletes image and redirects to image browser`() {
75                 request("", POST)
76                 addImage("image-id", image)
77                 addHttpRequestParameter("image", "image-id")
78                 expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"))
79                 try {
80                         page.processTemplate(freenetRequest, templateContext)
81                         fail()
82                 } catch (e: Exception) {
83                         verify(webInterface.core).deleteImage(image)
84                         throw e
85                 }
86         }
87
88 }