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.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
17 import org.mockito.Mockito.verify
20 * Unit test for [DeleteImagePage].
22 class DeleteImagePageTest: WebPageTest(::DeleteImagePage) {
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 `page returns correct path`() {
39 assertThat(page.path, equalTo("deleteImage.html"))
43 fun `page requires login`() {
44 assertThat(page.requiresLogin(), equalTo(true))
48 fun `get request with invalid image redirects to invalid page`() {
49 verifyRedirect("invalid.html")
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")
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))
69 fun `post request with abort delete flag set redirects to image browser`() {
71 addImage("image-id", image)
72 addHttpRequestPart("image", "image-id")
73 addHttpRequestPart("abortDelete", "true")
74 verifyRedirect("imageBrowser.html?image=image-id")
78 fun `post request deletes image and redirects to image browser`() {
80 addImage("image-id", image)
81 addHttpRequestPart("image", "image-id")
82 verifyRedirect("imageBrowser.html?album=album-id") {
83 verify(webInterface.core).deleteImage(image)
88 fun `page can be created by dependency injection`() {
89 assertThat(baseInjector.getInstance<DeleteImagePage>(), notNullValue())
93 fun `page is annotated with correct template path`() {
94 assertThat(page.templatePath, equalTo("/templates/deleteImage.html"))