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