Convert create album page test to use new web page test base
[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: WebPageTest2(::CreateAlbumPage) {
20
21         private val parentAlbum = createAlbum("parent-id")
22         private val newAlbum = createAlbum("album-id")
23
24         @Before
25         fun setupAlbums() {
26                 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
27                 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
28         }
29
30         @Test
31         fun `page returns correct path`() {
32                 assertThat(page.path, equalTo("createAlbum.html"))
33         }
34
35         @Test
36         fun `get request shows template`() {
37                 page.processTemplate(freenetRequest, templateContext)
38         }
39
40         @Test
41         fun `missing name results in attribute being set in template context`() {
42                 setMethod(POST)
43                 page.processTemplate(freenetRequest, templateContext)
44                 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
45         }
46
47         private fun createAlbum(albumId: String) = deepMock<Album>().apply {
48                 whenever(id).thenReturn(albumId)
49                 selfMock<Modifier>().let { modifier ->
50                         whenever(modifier.update()).thenReturn(this@apply)
51                         whenever(this@apply.modify()).thenReturn(modifier)
52                 }
53         }
54
55         @Test
56         fun `title and description are set correctly on the album`() {
57                 setMethod(POST)
58                 addAlbum("parent-id", parentAlbum)
59                 addHttpRequestPart("name", "new name")
60                 addHttpRequestPart("description", "new description")
61                 addHttpRequestPart("parent", "parent-id")
62                 verifyRedirect("imageBrowser.html?album=album-id") {
63                         verify(newAlbum).modify()
64                         verify(newAlbum.modify()).setTitle("new name")
65                         verify(newAlbum.modify()).setDescription("new description")
66                         verify(newAlbum.modify()).update()
67                         verify(core).touchConfiguration()
68                 }
69         }
70
71         @Test
72         fun `root album is used if no parent is specified`() {
73                 setMethod(POST)
74                 addHttpRequestPart("name", "new name")
75                 addHttpRequestPart("description", "new description")
76                 verifyRedirect("imageBrowser.html?album=album-id")
77         }
78
79         @Test
80         fun `empty album title redirects to error page`() {
81                 setMethod(POST)
82                 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
83                 addHttpRequestPart("name", "new name")
84                 addHttpRequestPart("description", "new description")
85                 verifyRedirect("emptyAlbumTitle.html")
86         }
87
88         @Test
89         fun `album description is filtered`() {
90                 setMethod(POST)
91                 addHttpRequestPart("name", "new name")
92                 addHttpRequestPart("description", "new http://localhost:12345/KSK@foo description")
93                 addHttpRequestHeader("Host", "localhost:12345")
94                 verifyRedirect("imageBrowser.html?album=album-id") {
95                         verify(newAlbum.modify()).setDescription("new KSK@foo description")
96                 }
97         }
98
99 }