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.getInstance
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.web.baseInjector
9 import net.pterodactylus.util.web.Method.POST
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.notNullValue
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(::DeleteAlbumPage) {
23 private val sone = mock<Sone>()
24 private val album = mock<Album>()
25 private val parentAlbum = mock<Album>()
29 whenever(sone.id).thenReturn("sone-id")
30 whenever(sone.isLocal).thenReturn(true)
31 whenever(parentAlbum.id).thenReturn("parent-id")
32 whenever(parentAlbum.isRoot).thenReturn(true)
33 whenever(album.id).thenReturn("album-id")
34 whenever(album.sone).thenReturn(sone)
35 whenever(album.parent).thenReturn(parentAlbum)
36 whenever(sone.rootAlbum).thenReturn(parentAlbum)
40 fun `page returns correct path`() {
41 assertThat(page.path, equalTo("deleteAlbum.html"))
45 fun `page requires login`() {
46 assertThat(page.requiresLogin(), equalTo(true))
50 fun `get request with invalid album ID results in redirect to invalid page`() {
51 whenever(core.getAlbum(anyString())).thenReturn(null)
52 verifyRedirect("invalid.html")
56 fun `get request with valid album ID sets album in template context`() {
57 val album = mock<Album>()
58 addAlbum("album-id", album)
59 addHttpRequestParameter("album", "album-id")
60 page.processTemplate(freenetRequest, templateContext)
61 assertThat(templateContext["album"], equalTo<Any>(album))
65 fun `post request redirects to invalid page if album is invalid`() {
67 verifyRedirect("invalid.html")
71 fun `post request redirects to no permissions page if album is not local`() {
73 whenever(sone.isLocal).thenReturn(false)
74 addAlbum("album-id", album)
75 addHttpRequestPart("album", "album-id")
76 verifyRedirect("noPermission.html")
80 fun `post request with abort delete parameter set redirects to album browser`() {
82 addAlbum("album-id", album)
83 addHttpRequestPart("album", "album-id")
84 addHttpRequestPart("abortDelete", "true")
85 verifyRedirect("imageBrowser.html?album=album-id")
89 fun `album is deleted and page redirects to sone if parent album is root album`() {
91 addAlbum("album-id", album)
92 addHttpRequestPart("album", "album-id")
93 verifyRedirect("imageBrowser.html?sone=sone-id") {
94 verify(core).deleteAlbum(album)
99 fun `album is deleted and page redirects to album if parent album is not root album`() {
101 whenever(parentAlbum.isRoot).thenReturn(false)
102 whenever(sone.rootAlbum).thenReturn(mock<Album>())
103 addAlbum("album-id", album)
104 addHttpRequestPart("album", "album-id")
105 verifyRedirect("imageBrowser.html?album=parent-id") {
106 verify(core).deleteAlbum(album)
111 fun `page can be created by dependency injection`() {
112 assertThat(baseInjector.getInstance<DeleteAlbumPage>(), notNullValue())