55b3d7030b0eb0ed8dd36d10c310e1faead9401c
[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.*
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.*
10 import org.junit.*
11 import org.mockito.ArgumentMatchers.anyString
12 import org.mockito.Mockito.verify
13
14 /**
15  * Unit test for [DeleteAlbumPage].
16  */
17 class DeleteAlbumPageTest : WebPageTest(::DeleteAlbumPage) {
18
19         private val sone = mock<Sone>()
20         private val album = mock<Album>()
21         private val parentAlbum = mock<Album>()
22
23         @Before
24         fun setupAlbums() {
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)
33         }
34
35         @Test
36         fun `page returns correct path`() {
37                 assertThat(page.path, equalTo("deleteAlbum.html"))
38         }
39
40         @Test
41         fun `page requires login`() {
42                 assertThat(page.requiresLogin(), equalTo(true))
43         }
44
45         @Test
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")
49         }
50
51         @Test
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))
58         }
59
60         @Test
61         fun `post request redirects to invalid page if album is invalid`() {
62                 setMethod(POST)
63                 verifyRedirect("invalid.html")
64         }
65
66         @Test
67         fun `post request redirects to no permissions page if album is not local`() {
68                 setMethod(POST)
69                 whenever(sone.isLocal).thenReturn(false)
70                 addAlbum("album-id", album)
71                 addHttpRequestPart("album", "album-id")
72                 verifyRedirect("noPermission.html")
73         }
74
75         @Test
76         fun `post request with abort delete parameter set redirects to album browser`() {
77                 setMethod(POST)
78                 addAlbum("album-id", album)
79                 addHttpRequestPart("album", "album-id")
80                 addHttpRequestPart("abortDelete", "true")
81                 verifyRedirect("imageBrowser.html?album=album-id")
82         }
83
84         @Test
85         fun `album is deleted and page redirects to sone if parent album is root album`() {
86                 setMethod(POST)
87                 addAlbum("album-id", album)
88                 addHttpRequestPart("album", "album-id")
89                 verifyRedirect("imageBrowser.html?sone=sone-id") {
90                         verify(core).deleteAlbum(album)
91                 }
92         }
93
94         @Test
95         fun `album is deleted and page redirects to album if parent album is not root album`() {
96                 setMethod(POST)
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)
103                 }
104         }
105
106         @Test
107         fun `page can be created by dependency injection`() {
108                 assertThat(baseInjector.getInstance<DeleteAlbumPage>(), notNullValue())
109         }
110
111         @Test
112         fun `page is annotated with correct template path`() {
113                 assertThat(page.templatePath, equalTo("/templates/deleteAlbum.html"))
114         }
115
116 }