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