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