1 package net.pterodactylus.sone.web.pages
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.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Before
12 import org.mockito.ArgumentMatchers.anyString
13 import org.mockito.Mockito.verify
16 * Unit test for [DeleteAlbumPage].
18 class DeleteAlbumPageTest: WebPageTest(::DeleteAlbumPage) {
20 private val sone = mock<Sone>()
21 private val album = mock<Album>()
22 private val parentAlbum = mock<Album>()
26 whenever(sone.id).thenReturn("sone-id")
27 whenever(sone.isLocal).thenReturn(true)
28 whenever(parentAlbum.id).thenReturn("parent-id")
29 whenever(parentAlbum.isRoot).thenReturn(true)
30 whenever(album.id).thenReturn("album-id")
31 whenever(album.sone).thenReturn(sone)
32 whenever(album.parent).thenReturn(parentAlbum)
33 whenever(sone.rootAlbum).thenReturn(parentAlbum)
37 fun `page returns correct path`() {
38 assertThat(page.path, equalTo("deleteAlbum.html"))
42 fun `page requires login`() {
43 assertThat(page.requiresLogin(), equalTo(true))
47 fun `get request with invalid album ID results in redirect to invalid page`() {
48 whenever(core.getAlbum(anyString())).thenReturn(null)
49 verifyRedirect("invalid.html")
53 fun `get request with valid album ID sets album in template context`() {
54 val album = mock<Album>()
55 addAlbum("album-id", album)
56 addHttpRequestParameter("album", "album-id")
57 page.processTemplate(freenetRequest, templateContext)
58 assertThat(templateContext["album"], equalTo<Any>(album))
62 fun `post request redirects to invalid page if album is invalid`() {
64 verifyRedirect("invalid.html")
68 fun `post request redirects to no permissions page if album is not local`() {
70 whenever(sone.isLocal).thenReturn(false)
71 addAlbum("album-id", album)
72 addHttpRequestPart("album", "album-id")
73 verifyRedirect("noPermission.html")
77 fun `post request with abort delete parameter set redirects to album browser`() {
79 addAlbum("album-id", album)
80 addHttpRequestPart("album", "album-id")
81 addHttpRequestPart("abortDelete", "true")
82 verifyRedirect("imageBrowser.html?album=album-id")
86 fun `album is deleted and page redirects to sone if parent album is root album`() {
88 addAlbum("album-id", album)
89 addHttpRequestPart("album", "album-id")
90 verifyRedirect("imageBrowser.html?sone=sone-id") {
91 verify(core).deleteAlbum(album)
96 fun `album is deleted and page redirects to album if parent album is not root album`() {
98 whenever(parentAlbum.isRoot).thenReturn(false)
99 whenever(sone.rootAlbum).thenReturn(mock<Album>())
100 addAlbum("album-id", album)
101 addHttpRequestPart("album", "album-id")
102 verifyRedirect("imageBrowser.html?album=parent-id") {
103 verify(core).deleteAlbum(album)