Clean up some imports
[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.util.web.Method.POST
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
12 import org.junit.Before
13 import org.junit.Test
14 import org.mockito.Mockito.verify
15
16 /**
17  * Unit test for [CreateAlbumPage].
18  */
19 class CreateAlbumPageTest: WebPageTest() {
20
21         private val page = CreateAlbumPage(template, webInterface)
22
23         override fun getPage() = page
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 }