1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.web.*
6 import net.pterodactylus.sone.web.page.*
7 import net.pterodactylus.util.web.Method.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
11 import org.mockito.ArgumentMatchers.anyString
12 import org.mockito.Mockito.verify
15 * Unit test for [DeleteAlbumPage].
17 class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
19 private val sone = mock<Sone>()
20 private val album = mock<Album>()
21 private val parentAlbum = mock<Album>()
25 whenever(sone.id).thenReturn("sone-id")
26 whenever(sone.isLocal).thenReturn(true)
27 whenever(parentAlbum.id).thenReturn("parent-id")
28 whenever(parentAlbum.isRoot).thenReturn(true)
29 whenever(album.id).thenReturn("album-id")
30 whenever(album.sone).thenReturn(sone)
31 whenever(album.parent).thenReturn(parentAlbum)
32 whenever(sone.rootAlbum).thenReturn(parentAlbum)
36 fun `page returns correct path`() {
37 assertThat(page.path, equalTo("deleteAlbum.html"))
41 fun `page requires login`() {
42 assertThat(page.requiresLogin(), equalTo(true))
46 fun `get request with invalid album ID results in redirect to invalid page`() {
47 whenever(core.getAlbum(anyString())).thenReturn(null)
48 verifyRedirect("invalid.html")
52 fun `get request with valid album ID sets album in template context`() {
53 val album = mock<Album>()
54 addAlbum("album-id", album)
55 addHttpRequestParameter("album", "album-id")
56 page.processTemplate(freenetRequest, templateContext)
57 assertThat(templateContext["album"], equalTo<Any>(album))
61 fun `post request redirects to invalid page if album is invalid`() {
63 verifyRedirect("invalid.html")
67 fun `post request redirects to no permissions page if album is not local`() {
69 whenever(sone.isLocal).thenReturn(false)
70 addAlbum("album-id", album)
71 addHttpRequestPart("album", "album-id")
72 verifyRedirect("noPermission.html")
76 fun `post request with abort delete parameter set redirects to album browser`() {
78 addAlbum("album-id", album)
79 addHttpRequestPart("album", "album-id")
80 addHttpRequestPart("abortDelete", "true")
81 verifyRedirect("imageBrowser.html?album=album-id")
85 fun `album is deleted and page redirects to sone if parent album is root album`() {
87 addAlbum("album-id", album)
88 addHttpRequestPart("album", "album-id")
89 verifyRedirect("imageBrowser.html?sone=sone-id") {
90 verify(core).deleteAlbum(album)
95 fun `album is deleted and page redirects to album if parent album is not root album`() {
97 whenever(parentAlbum.isRoot).thenReturn(false)
98 whenever(sone.rootAlbum).thenReturn(mock())
99 addAlbum("album-id", album)
100 addHttpRequestPart("album", "album-id")
101 verifyRedirect("imageBrowser.html?album=parent-id") {
102 verify(core).deleteAlbum(album)
107 fun `page can be created by dependency injection`() {
108 assertThat(baseInjector.getInstance<DeleteAlbumPage>(), notNullValue())
112 fun `page is annotated with correct template path`() {
113 assertThat(page.templatePath, equalTo("/templates/deleteAlbum.html"))