Convert view Sone page test to new web page test base
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / DeleteImagePageTest.kt
1 package net.pterodactylus.sone.web.pages
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.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Before
12 import org.junit.Test
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [DeleteImagePage].
17  */
18 class DeleteImagePageTest: WebPageTest() {
19
20         private val page = DeleteImagePage(template, webInterface)
21         private val image = mock<Image>()
22         private val sone = mock<Sone>()
23
24         override fun getPage() = page
25
26         @Before
27         fun setupImage() {
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)
34         }
35
36         @Test
37         fun `page returns correct path`() {
38                 assertThat(page.path, equalTo("deleteImage.html"))
39         }
40
41         @Test
42         fun `page requires login`() {
43                 assertThat(page.requiresLogin(), equalTo(true))
44         }
45
46         @Test
47         fun `get request with invalid image redirects to invalid page`() {
48                 verifyRedirect("invalid.html")
49         }
50
51         @Test
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")
57         }
58
59         @Test
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))
65         }
66
67         @Test
68         fun `post request with abort delete flag set redirects to image browser`() {
69                 setMethod(POST)
70                 addImage("image-id", image)
71                 addHttpRequestPart("image", "image-id")
72                 addHttpRequestPart("abortDelete", "true")
73                 verifyRedirect("imageBrowser.html?image=image-id")
74         }
75
76         @Test
77         fun `post request deletes image and redirects to image browser`() {
78                 setMethod(POST)
79                 addImage("image-id", image)
80                 addHttpRequestPart("image", "image-id")
81                 verifyRedirect("imageBrowser.html?album=album-id") {
82                         verify(webInterface.core).deleteImage(image)
83                 }
84         }
85
86 }