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(parentAlbum.isRoot).thenReturn(true)
35 whenever(album.id).thenReturn("album-id")
36 whenever(album.sone).thenReturn(sone)
37 whenever(album.parent).thenReturn(parentAlbum)
38 whenever(sone.rootAlbum).thenReturn(parentAlbum)
42 fun `page returns correct path`() {
43 assertThat(page.path, equalTo("deleteAlbum.html"))
47 fun `page requires login`() {
48 assertThat(page.requiresLogin(), equalTo(true))
52 fun `get request with invalid album ID results in redirect to invalid page`() {
54 whenever(core.getAlbum(anyString())).thenReturn(null)
55 verifyRedirect("invalid.html")
59 fun `get request with valid album ID sets album in template context`() {
61 val album = mock<Album>()
62 addAlbum("album-id", album)
63 addHttpRequestParameter("album", "album-id")
64 page.processTemplate(freenetRequest, templateContext)
65 assertThat(templateContext["album"], equalTo<Any>(album))
69 fun `post request redirects to invalid page if album is invalid`() {
71 verifyRedirect("invalid.html")
75 fun `post request redirects to no permissions page if album is not local`() {
77 whenever(sone.isLocal).thenReturn(false)
78 addAlbum("album-id", album)
79 addHttpRequestParameter("album", "album-id")
80 verifyRedirect("noPermission.html")
84 fun `post request with abort delete parameter set redirects to album browser`() {
86 addAlbum("album-id", album)
87 addHttpRequestParameter("album", "album-id")
88 addHttpRequestParameter("abortDelete", "true")
89 verifyRedirect("imageBrowser.html?album=album-id")
93 fun `album is deleted and page redirects to sone if parent album is root album`() {
95 addAlbum("album-id", album)
96 addHttpRequestParameter("album", "album-id")
97 verifyRedirect("imageBrowser.html?sone=sone-id") {
98 verify(core).deleteAlbum(album)
103 fun `album is deleted and page redirects to album if parent album is not root album`() {
105 whenever(parentAlbum.isRoot).thenReturn(false)
106 whenever(sone.rootAlbum).thenReturn(mock<Album>())
107 addAlbum("album-id", album)
108 addHttpRequestParameter("album", "album-id")
109 verifyRedirect("imageBrowser.html?album=parent-id") {
110 verify(core).deleteAlbum(album)