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