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.sone.web.pages.WebPageTest
10 import net.pterodactylus.sone.web.pages.CreateAlbumPage
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Before
16 import org.mockito.Mockito.verify
19 * Unit test for [CreateAlbumPage].
21 class CreateAlbumPageTest: WebPageTest() {
23 private val page = CreateAlbumPage(template, webInterface)
25 override fun getPage() = page
27 private val parentAlbum = createAlbum("parent-id")
28 private val newAlbum = createAlbum("album-id")
32 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
33 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
37 fun `page returns correct path`() {
38 assertThat(page.path, equalTo("createAlbum.html"))
42 fun `get request shows template`() {
43 page.processTemplate(freenetRequest, templateContext)
47 fun `missing name results in attribute being set in template context`() {
49 page.processTemplate(freenetRequest, templateContext)
50 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
53 private fun createAlbum(albumId: String) = deepMock<Album>().apply {
54 whenever(id).thenReturn(albumId)
55 selfMock<Modifier>().let { modifier ->
56 whenever(modifier.update()).thenReturn(this@apply)
57 whenever(this@apply.modify()).thenReturn(modifier)
62 fun `title and description are set correctly on the album`() {
64 addAlbum("parent-id", parentAlbum)
65 addHttpRequestParameter("name", "new name")
66 addHttpRequestParameter("description", "new description")
67 addHttpRequestParameter("parent", "parent-id")
68 verifyRedirect("imageBrowser.html?album=album-id") {
69 verify(newAlbum).modify()
70 verify(newAlbum.modify()).setTitle("new name")
71 verify(newAlbum.modify()).setDescription("new description")
72 verify(newAlbum.modify()).update()
73 verify(core).touchConfiguration()
78 fun `root album is used if no parent is specified`() {
80 addHttpRequestParameter("name", "new name")
81 addHttpRequestParameter("description", "new description")
82 verifyRedirect("imageBrowser.html?album=album-id")
86 fun `empty album title redirects to error page`() {
88 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
89 addHttpRequestParameter("name", "new name")
90 addHttpRequestParameter("description", "new description")
91 verifyRedirect("emptyAlbumTitle.html")
95 fun `album description is filtered`() {
97 addHttpRequestParameter("name", "new name")
98 addHttpRequestParameter("description", "new http://localhost:12345/KSK@foo description")
99 addHttpRequestHeader("Host", "localhost:12345")
100 verifyRedirect("imageBrowser.html?album=album-id") {
101 verify(newAlbum.modify()).setDescription("new KSK@foo description")