Convert view Sone page test to new web page test base
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / EditAlbumPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Album.Modifier
5 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.mockBuilder
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.junit.Before
14 import org.junit.Test
15 import org.mockito.Mockito.verify
16
17 /**
18  * Unit test for [EditAlbumPage].
19  */
20 class EditAlbumPageTest: WebPageTest() {
21
22         private val page = EditAlbumPage(template, webInterface)
23
24         private val album = mock<Album>()
25         private val parentAlbum = mock<Album>()
26         private val modifier = mockBuilder<Modifier>()
27         private val sone = mock<Sone>()
28
29         override fun getPage() = page
30
31         @Before
32         fun setup() {
33                 whenever(album.id).thenReturn("album-id")
34                 whenever(album.sone).thenReturn(sone)
35                 whenever(album.parent).thenReturn(parentAlbum)
36                 whenever(album.modify()).thenReturn(modifier)
37                 whenever(modifier.update()).thenReturn(album)
38                 whenever(parentAlbum.id).thenReturn("parent-id")
39                 whenever(sone.isLocal).thenReturn(true)
40                 addHttpRequestHeader("Host", "www.te.st")
41         }
42
43         @Test
44         fun `page returns correct path`() {
45                 assertThat(page.path, equalTo("editAlbum.html"))
46         }
47
48         @Test
49         fun `page requires login`() {
50                 assertThat(page.requiresLogin(), equalTo(true))
51         }
52
53         @Test
54         fun `page returns correct title`() {
55                 whenever(l10n.getString("Page.EditAlbum.Title")).thenReturn("edit album page")
56                 assertThat(page.getPageTitle(freenetRequest), equalTo("edit album page"))
57         }
58
59         @Test
60         fun `get request does not redirect`() {
61                 page.processTemplate(freenetRequest, templateContext)
62         }
63
64         @Test
65         fun `post request with invalid album redirects to invalid page`() {
66                 setMethod(POST)
67                 verifyRedirect("invalid.html")
68         }
69
70         @Test
71         fun `post request with album of non-local sone redirects to no permissions page`() {
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 move left requested moves album to the left and redirects to album browser`() {
81                 setMethod(POST)
82                 addAlbum("album-id", album)
83                 addHttpRequestPart("album", "album-id")
84                 addHttpRequestPart("moveLeft", "true")
85                 verifyRedirect("imageBrowser.html?album=parent-id") {
86                         verify(parentAlbum).moveAlbumUp(album)
87                         verify(core).touchConfiguration()
88                 }
89         }
90
91         @Test
92         fun `post request with move right requested moves album to the left and redirects to album browser`() {
93                 setMethod(POST)
94                 addAlbum("album-id", album)
95                 addHttpRequestPart("album", "album-id")
96                 addHttpRequestPart("moveRight", "true")
97                 verifyRedirect("imageBrowser.html?album=parent-id") {
98                         verify(parentAlbum).moveAlbumDown(album)
99                         verify(core).touchConfiguration()
100                 }
101         }
102
103         @Test
104         fun `post request with empty album title redirects to empty album title page`() {
105                 setMethod(POST)
106                 addAlbum("album-id", album)
107                 addHttpRequestPart("album", "album-id")
108                 whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
109                 verifyRedirect("emptyAlbumTitle.html")
110         }
111
112         @Test
113         fun `post request with non-empty album title and description redirects to album browser`() {
114                 setMethod(POST)
115                 addAlbum("album-id", album)
116                 addHttpRequestPart("album", "album-id")
117                 addHttpRequestPart("title", "title")
118                 addHttpRequestPart("description", "description")
119                 verifyRedirect("imageBrowser.html?album=album-id") {
120                         verify(modifier).setTitle("title")
121                         verify(modifier).setDescription("description")
122                         verify(modifier).update()
123                         verify(core).touchConfiguration()
124                 }
125         }
126
127 }