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