♻️ Move generic error pages to custom classes
[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.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.baseInjector
10 import net.pterodactylus.sone.web.page.*
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Before
16 import org.junit.Test
17 import org.mockito.Mockito.verify
18
19 /**
20  * Unit test for [DeleteImagePage].
21  */
22 class DeleteImagePageTest: WebPageTest(::DeleteImagePage) {
23
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 `page returns correct path`() {
39                 assertThat(page.path, equalTo("deleteImage.html"))
40         }
41
42         @Test
43         fun `page requires login`() {
44                 assertThat(page.requiresLogin(), equalTo(true))
45         }
46
47         @Test
48         fun `get request with invalid image redirects to invalid page`() {
49                 verifyRedirect("invalid.html")
50         }
51
52         @Test
53         fun `get request with image from non-local sone redirects to no permissions page`() {
54                 whenever(sone.isLocal).thenReturn(false)
55                 addImage("image-id", image)
56                 addHttpRequestParameter("image", "image-id")
57                 verifyRedirect("noPermission.html")
58         }
59
60         @Test
61         fun `get request with image from local sone sets image in template context`() {
62                 addImage("image-id", image)
63                 addHttpRequestParameter("image", "image-id")
64                 page.processTemplate(freenetRequest, templateContext)
65                 assertThat(templateContext["image"], equalTo<Any>(image))
66         }
67
68         @Test
69         fun `post request with abort delete flag set redirects to image browser`() {
70                 setMethod(POST)
71                 addImage("image-id", image)
72                 addHttpRequestPart("image", "image-id")
73                 addHttpRequestPart("abortDelete", "true")
74                 verifyRedirect("imageBrowser.html?image=image-id")
75         }
76
77         @Test
78         fun `post request deletes image and redirects to image browser`() {
79                 setMethod(POST)
80                 addImage("image-id", image)
81                 addHttpRequestPart("image", "image-id")
82                 verifyRedirect("imageBrowser.html?album=album-id") {
83                         verify(webInterface.core).deleteImage(image)
84                 }
85         }
86
87         @Test
88         fun `page can be created by dependency injection`() {
89             assertThat(baseInjector.getInstance<DeleteImagePage>(), notNullValue())
90         }
91
92         @Test
93         fun `page is annotated with correct template path`() {
94             assertThat(page.templatePath, equalTo("/templates/deleteImage.html"))
95         }
96
97 }