1 package net.pterodactylus.sone.web.pages
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
14 import org.mockito.Mockito.verify
17 * Unit test for [CreateAlbumPage].
19 class CreateAlbumPageTest: WebPageTest() {
21 private val page = CreateAlbumPage(template, webInterface)
23 override fun getPage() = page
25 private val parentAlbum = createAlbum("parent-id")
26 private val newAlbum = createAlbum("album-id")
30 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
31 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo("createAlbum.html"))
40 fun `get request shows template`() {
41 page.processTemplate(freenetRequest, templateContext)
45 fun `missing name results in attribute being set in template context`() {
47 page.processTemplate(freenetRequest, templateContext)
48 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
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)
60 fun `title and description are set correctly on the album`() {
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()
76 fun `root album is used if no parent is specified`() {
78 addHttpRequestPart("name", "new name")
79 addHttpRequestPart("description", "new description")
80 verifyRedirect("imageBrowser.html?album=album-id")
84 fun `empty album title redirects to error page`() {
86 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
87 addHttpRequestPart("name", "new name")
88 addHttpRequestPart("description", "new description")
89 verifyRedirect("emptyAlbumTitle.html")
93 fun `album description is filtered`() {
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")