1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.util.web.Method.GET
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
13 import org.mockito.ArgumentMatchers.anyString
14 import org.mockito.Mockito.verify
17 * Unit test for [DeleteAlbumPage].
19 class DeleteAlbumPageTest : WebPageTest() {
21 private val page = DeleteAlbumPage(template, webInterface)
23 private val sone = mock<Sone>()
24 private val album = mock<Album>()
25 private val parentAlbum = mock<Album>()
27 override fun getPage() = page
31 whenever(sone.id).thenReturn("sone-id")
32 whenever(sone.isLocal).thenReturn(true)
33 whenever(parentAlbum.id).thenReturn("parent-id")
34 whenever(album.id).thenReturn("album-id")
35 whenever(album.sone).thenReturn(sone)
36 whenever(album.parent).thenReturn(parentAlbum)
37 whenever(sone.rootAlbum).thenReturn(parentAlbum)
41 fun `get request with invalid album ID results in redirect to invalid page`() {
43 whenever(core.getAlbum(anyString())).thenReturn(null)
44 verifyRedirect("invalid.html")
48 fun `get request with valid album ID sets album in template context`() {
50 val album = mock<Album>()
51 addAlbum("album-id", album)
52 addHttpRequestParameter("album", "album-id")
53 page.handleRequest(freenetRequest, templateContext)
54 assertThat(templateContext["album"], equalTo<Any>(album))
58 fun `post request redirects to invalid page if album is invalid`() {
60 verifyRedirect("invalid.html")
64 fun `post request redirects to no permissions page if album is not local`() {
66 whenever(sone.isLocal).thenReturn(false)
67 addAlbum("album-id", album)
68 addHttpRequestParameter("album", "album-id")
69 verifyRedirect("noPermission.html")
73 fun `post request with abort delete parameter set redirects to album browser`() {
75 addAlbum("album-id", album)
76 addHttpRequestParameter("album", "album-id")
77 addHttpRequestParameter("abortDelete", "true")
78 verifyRedirect("imageBrowser.html?album=album-id")
82 fun `album is deleted and page redirects to sone if parent album is root album`() {
84 addAlbum("album-id", album)
85 addHttpRequestParameter("album", "album-id")
86 verifyRedirect("imageBrowser.html?sone=sone-id") {
87 verify(core).deleteAlbum(album)
92 fun `album is deleted and page redirects to album if parent album is not root album`() {
94 whenever(sone.rootAlbum).thenReturn(mock<Album>())
95 addAlbum("album-id", album)
96 addHttpRequestParameter("album", "album-id")
97 verifyRedirect("imageBrowser.html?album=parent-id") {
98 verify(core).deleteAlbum(album)