Replace delete post page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / EditAlbumPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.mockBuilder
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.util.web.Method.GET
10 import net.pterodactylus.util.web.Method.POST
11 import org.junit.Before
12 import org.junit.Test
13 import org.mockito.Mockito.verify
14
15 /**
16  * Unit test for [EditAlbumPage].
17  */
18 class EditAlbumPageTest : WebPageTest() {
19
20         private val page = EditAlbumPage(template, webInterface)
21
22         private val album = mock<Album>()
23         private val parentAlbum = mock<Album>()
24         private val modifier = mockBuilder<Album.Modifier>()
25         private val sone = mock<Sone>()
26
27         override fun getPage() = page
28
29         @Before
30         fun setup() {
31                 whenever(album.id).thenReturn("album-id")
32                 whenever(album.sone).thenReturn(sone)
33                 whenever(album.parent).thenReturn(parentAlbum)
34                 whenever(album.modify()).thenReturn(modifier)
35                 whenever(modifier.update()).thenReturn(album)
36                 whenever(parentAlbum.id).thenReturn("parent-id")
37                 whenever(sone.isLocal).thenReturn(true)
38                 addHttpRequestHeader("Host", "www.te.st")
39         }
40
41         @Test
42         fun `get request does not redirect`() {
43                 request("", GET)
44                 page.handleRequest(freenetRequest, templateContext)
45         }
46
47         @Test
48         fun `post request with invalid album redirects to invalid page`() {
49                 request("", POST)
50                 verifyRedirect("invalid.html")
51         }
52
53         @Test
54         fun `post request with album of non-local sone redirects to no permissions page`() {
55                 request("", POST)
56                 whenever(sone.isLocal).thenReturn(false)
57                 addAlbum("album-id", album)
58                 addHttpRequestParameter("album", "album-id")
59                 verifyRedirect("noPermission.html")
60         }
61
62         @Test
63         fun `post request with move left requested moves album to the left and redirects to album browser`() {
64                 request("", POST)
65                 addAlbum("album-id", album)
66                 addHttpRequestParameter("album", "album-id")
67                 addHttpRequestParameter("moveLeft", "true")
68                 verifyRedirect("imageBrowser.html?album=parent-id") {
69                         verify(parentAlbum).moveAlbumUp(album)
70                         verify(core).touchConfiguration()
71                 }
72         }
73
74         @Test
75         fun `post request with move right requested moves album to the left and redirects to album browser`() {
76                 request("", POST)
77                 addAlbum("album-id", album)
78                 addHttpRequestParameter("album", "album-id")
79                 addHttpRequestParameter("moveRight", "true")
80                 verifyRedirect("imageBrowser.html?album=parent-id") {
81                         verify(parentAlbum).moveAlbumDown(album)
82                         verify(core).touchConfiguration()
83                 }
84         }
85
86         @Test
87         fun `post request with empty album title redirects to empty album title page`() {
88                 request("", POST)
89                 addAlbum("album-id", album)
90                 addHttpRequestParameter("album", "album-id")
91                 whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
92                 verifyRedirect("emptyAlbumTitle.html")
93         }
94
95         @Test
96         fun `post request with non-empty album title and description redirects to album browser`() {
97                 request("", POST)
98                 addAlbum("album-id", album)
99                 addHttpRequestParameter("album", "album-id")
100                 addHttpRequestParameter("title", "title")
101                 addHttpRequestParameter("description", "description")
102                 verifyRedirect("imageBrowser.html?album=album-id") {
103                         verify(modifier).setTitle("title")
104                         verify(modifier).setDescription("description")
105                         verify(modifier).update()
106                         verify(core).touchConfiguration()
107                 }
108         }
109
110 }