Replace delete album page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / DeleteAlbumPageTest.kt
1 package net.pterodactylus.sone.web
2
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
12 import org.junit.Test
13 import org.mockito.ArgumentMatchers.anyString
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [DeleteAlbumPage].
18  */
19 class DeleteAlbumPageTest: WebPageTest() {
20
21         private val page = DeleteAlbumPage(template, webInterface)
22
23         private val sone = mock<Sone>()
24         private val album = mock<Album>()
25         private val parentAlbum = mock<Album>()
26
27         override fun getPage() = page
28
29         @Before
30         fun setupAlbums() {
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)
39         }
40
41         @Test
42         fun `page returns correct path`() {
43                 assertThat(page.path, equalTo("deleteAlbum.html"))
44         }
45
46         @Test
47         fun `page requires login`() {
48                 assertThat(page.requiresLogin(), equalTo(true))
49         }
50
51         @Test
52         fun `get request with invalid album ID results in redirect to invalid page`() {
53                 request("", GET)
54                 whenever(core.getAlbum(anyString())).thenReturn(null)
55                 verifyRedirect("invalid.html")
56         }
57
58         @Test
59         fun `get request with valid album ID sets album in template context`() {
60                 request("", GET)
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))
66         }
67
68         @Test
69         fun `post request redirects to invalid page if album is invalid`() {
70                 request("", POST)
71                 verifyRedirect("invalid.html")
72         }
73
74         @Test
75         fun `post request redirects to no permissions page if album is not local`() {
76                 request("", POST)
77                 whenever(sone.isLocal).thenReturn(false)
78                 addAlbum("album-id", album)
79                 addHttpRequestParameter("album", "album-id")
80                 verifyRedirect("noPermission.html")
81         }
82
83         @Test
84         fun `post request with abort delete parameter set redirects to album browser`() {
85                 request("", POST)
86                 addAlbum("album-id", album)
87                 addHttpRequestParameter("album", "album-id")
88                 addHttpRequestParameter("abortDelete", "true")
89                 verifyRedirect("imageBrowser.html?album=album-id")
90         }
91
92         @Test
93         fun `album is deleted and page redirects to sone if parent album is root album`() {
94                 request("", POST)
95                 addAlbum("album-id", album)
96                 addHttpRequestParameter("album", "album-id")
97                 verifyRedirect("imageBrowser.html?sone=sone-id") {
98                         verify(core).deleteAlbum(album)
99                 }
100         }
101
102         @Test
103         fun `album is deleted and page redirects to album if parent album is not root album`() {
104                 request("", POST)
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)
111                 }
112         }
113
114 }