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