9851530b829dbb87d6c83ce93a0d1467f7116601
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / CreateAlbumPageTest.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.test.deepMock
7 import net.pterodactylus.sone.test.selfMock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.pages.WebPageTest
10 import net.pterodactylus.sone.web.pages.CreateAlbumPage
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Before
15 import org.junit.Test
16 import org.mockito.Mockito.verify
17
18 /**
19  * Unit test for [CreateAlbumPage].
20  */
21 class CreateAlbumPageTest: WebPageTest() {
22
23         private val page = CreateAlbumPage(template, webInterface)
24
25         override fun getPage() = page
26
27         private val parentAlbum = createAlbum("parent-id")
28         private val newAlbum = createAlbum("album-id")
29
30         @Before
31         fun setupAlbums() {
32                 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
33                 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
34         }
35
36         @Test
37         fun `page returns correct path`() {
38                 assertThat(page.path, equalTo("createAlbum.html"))
39         }
40
41         @Test
42         fun `get request shows template`() {
43                 page.processTemplate(freenetRequest, templateContext)
44         }
45
46         @Test
47         fun `missing name results in attribute being set in template context`() {
48                 request("", POST)
49                 page.processTemplate(freenetRequest, templateContext)
50                 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
51         }
52
53         private fun createAlbum(albumId: String) = deepMock<Album>().apply {
54                 whenever(id).thenReturn(albumId)
55                 selfMock<Modifier>().let { modifier ->
56                         whenever(modifier.update()).thenReturn(this@apply)
57                         whenever(this@apply.modify()).thenReturn(modifier)
58                 }
59         }
60
61         @Test
62         fun `title and description are set correctly on the album`() {
63                 request("", POST)
64                 addAlbum("parent-id", parentAlbum)
65                 addHttpRequestPart("name", "new name")
66                 addHttpRequestPart("description", "new description")
67                 addHttpRequestPart("parent", "parent-id")
68                 verifyRedirect("imageBrowser.html?album=album-id") {
69                         verify(newAlbum).modify()
70                         verify(newAlbum.modify()).setTitle("new name")
71                         verify(newAlbum.modify()).setDescription("new description")
72                         verify(newAlbum.modify()).update()
73                         verify(core).touchConfiguration()
74                 }
75         }
76
77         @Test
78         fun `root album is used if no parent is specified`() {
79                 request("", POST)
80                 addHttpRequestPart("name", "new name")
81                 addHttpRequestPart("description", "new description")
82                 verifyRedirect("imageBrowser.html?album=album-id")
83         }
84
85         @Test
86         fun `empty album title redirects to error page`() {
87                 request("", POST)
88                 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
89                 addHttpRequestPart("name", "new name")
90                 addHttpRequestPart("description", "new description")
91                 verifyRedirect("emptyAlbumTitle.html")
92         }
93
94         @Test
95         fun `album description is filtered`() {
96                 request("", POST)
97                 addHttpRequestPart("name", "new name")
98                 addHttpRequestPart("description", "new http://localhost:12345/KSK@foo description")
99                 addHttpRequestHeader("Host", "localhost:12345")
100                 verifyRedirect("imageBrowser.html?album=album-id") {
101                         verify(newAlbum.modify()).setDescription("new KSK@foo description")
102                 }
103         }
104
105 }