Use new redirect verification
[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(album.id).thenReturn("album-id")
35                 whenever(album.sone).thenReturn(sone)
36                 whenever(album.parent).thenReturn(parentAlbum)
37                 whenever(sone.rootAlbum).thenReturn(parentAlbum)
38         }
39
40         @Test
41         fun `get request with invalid album ID results in redirect to invalid page`() {
42                 request("", GET)
43                 whenever(core.getAlbum(anyString())).thenReturn(null)
44                 verifyRedirect("invalid.html")
45         }
46
47         @Test
48         fun `get request with valid album ID sets album in template context`() {
49                 request("", GET)
50                 val album = mock<Album>()
51                 addAlbum("album-id", album)
52                 addHttpRequestParameter("album", "album-id")
53                 page.handleRequest(freenetRequest, templateContext)
54                 assertThat(templateContext["album"], equalTo<Any>(album))
55         }
56
57         @Test
58         fun `post request redirects to invalid page if album is invalid`() {
59                 request("", POST)
60                 verifyRedirect("invalid.html")
61         }
62
63         @Test
64         fun `post request redirects to no permissions page if album is not local`() {
65                 request("", POST)
66                 whenever(sone.isLocal).thenReturn(false)
67                 addAlbum("album-id", album)
68                 addHttpRequestParameter("album", "album-id")
69                 verifyRedirect("noPermission.html")
70         }
71
72         @Test
73         fun `post request with abort delete parameter set redirects to album browser`() {
74                 request("", POST)
75                 addAlbum("album-id", album)
76                 addHttpRequestParameter("album", "album-id")
77                 addHttpRequestParameter("abortDelete", "true")
78                 verifyRedirect("imageBrowser.html?album=album-id")
79         }
80
81         @Test
82         fun `album is deleted and page redirects to sone if parent album is root album`() {
83                 request("", POST)
84                 addAlbum("album-id", album)
85                 addHttpRequestParameter("album", "album-id")
86                 verifyRedirect("imageBrowser.html?sone=sone-id") {
87                         verify(core).deleteAlbum(album)
88                 }
89         }
90
91         @Test
92         fun `album is deleted and page redirects to album if parent album is not root album`() {
93                 request("", POST)
94                 whenever(sone.rootAlbum).thenReturn(mock<Album>())
95                 addAlbum("album-id", album)
96                 addHttpRequestParameter("album", "album-id")
97                 verifyRedirect("imageBrowser.html?album=parent-id") {
98                         verify(core).deleteAlbum(album)
99                 }
100         }
101
102 }