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.sone.web.pages.WebPageTest
8 import net.pterodactylus.sone.web.pages.DeleteAlbumPage
9 import net.pterodactylus.util.web.Method.GET
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.junit.Before
15 import org.mockito.ArgumentMatchers.anyString
16 import org.mockito.Mockito.verify
19 * Unit test for [DeleteAlbumPage].
21 class DeleteAlbumPageTest: WebPageTest() {
23 private val page = DeleteAlbumPage(template, webInterface)
25 private val sone = mock<Sone>()
26 private val album = mock<Album>()
27 private val parentAlbum = mock<Album>()
29 override fun getPage() = page
33 whenever(sone.id).thenReturn("sone-id")
34 whenever(sone.isLocal).thenReturn(true)
35 whenever(parentAlbum.id).thenReturn("parent-id")
36 whenever(parentAlbum.isRoot).thenReturn(true)
37 whenever(album.id).thenReturn("album-id")
38 whenever(album.sone).thenReturn(sone)
39 whenever(album.parent).thenReturn(parentAlbum)
40 whenever(sone.rootAlbum).thenReturn(parentAlbum)
44 fun `page returns correct path`() {
45 assertThat(page.path, equalTo("deleteAlbum.html"))
49 fun `page requires login`() {
50 assertThat(page.requiresLogin(), equalTo(true))
54 fun `get request with invalid album ID results in redirect to invalid page`() {
56 whenever(core.getAlbum(anyString())).thenReturn(null)
57 verifyRedirect("invalid.html")
61 fun `get request with valid album ID sets album in template context`() {
63 val album = mock<Album>()
64 addAlbum("album-id", album)
65 addHttpRequestParameter("album", "album-id")
66 page.processTemplate(freenetRequest, templateContext)
67 assertThat(templateContext["album"], equalTo<Any>(album))
71 fun `post request redirects to invalid page if album is invalid`() {
73 verifyRedirect("invalid.html")
77 fun `post request redirects to no permissions page if album is not local`() {
79 whenever(sone.isLocal).thenReturn(false)
80 addAlbum("album-id", album)
81 addHttpRequestParameter("album", "album-id")
82 verifyRedirect("noPermission.html")
86 fun `post request with abort delete parameter set redirects to album browser`() {
88 addAlbum("album-id", album)
89 addHttpRequestParameter("album", "album-id")
90 addHttpRequestParameter("abortDelete", "true")
91 verifyRedirect("imageBrowser.html?album=album-id")
95 fun `album is deleted and page redirects to sone if parent album is root album`() {
97 addAlbum("album-id", album)
98 addHttpRequestParameter("album", "album-id")
99 verifyRedirect("imageBrowser.html?sone=sone-id") {
100 verify(core).deleteAlbum(album)
105 fun `album is deleted and page redirects to album if parent album is not root album`() {
107 whenever(parentAlbum.isRoot).thenReturn(false)
108 whenever(sone.rootAlbum).thenReturn(mock<Album>())
109 addAlbum("album-id", album)
110 addHttpRequestParameter("album", "album-id")
111 verifyRedirect("imageBrowser.html?album=parent-id") {
112 verify(core).deleteAlbum(album)