1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
5 import net.pterodactylus.sone.test.deepMock
6 import net.pterodactylus.sone.test.selfMock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.web.Method.POST
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Before
13 import org.mockito.Mockito.verify
16 * Unit test for [CreateAlbumPage].
18 class CreateAlbumPageTest: WebPageTest() {
20 private val page = CreateAlbumPage(template, webInterface)
22 override fun getPage() = page
24 private val parentAlbum = createAlbum("parent-id")
25 private val newAlbum = createAlbum("album-id")
29 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
30 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
34 fun `page returns correct path`() {
35 assertThat(page.path, equalTo("createAlbum.html"))
39 fun `get request shows template`() {
40 page.processTemplate(freenetRequest, templateContext)
44 fun `missing name results in attribute being set in template context`() {
46 page.processTemplate(freenetRequest, templateContext)
47 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
50 private fun createAlbum(albumId: String) = deepMock<Album>().apply {
51 whenever(id).thenReturn(albumId)
52 selfMock<Album.Modifier>().let { modifier ->
53 whenever(modifier.update()).thenReturn(this@apply)
54 whenever(this@apply.modify()).thenReturn(modifier)
59 fun `title and description are set correctly on the album`() {
61 addAlbum("parent-id", parentAlbum)
62 addHttpRequestParameter("name", "new name")
63 addHttpRequestParameter("description", "new description")
64 addHttpRequestParameter("parent", "parent-id")
65 verifyRedirect("imageBrowser.html?album=album-id") {
66 verify(newAlbum).modify()
67 verify(newAlbum.modify()).setTitle("new name")
68 verify(newAlbum.modify()).setDescription("new description")
69 verify(newAlbum.modify()).update()
70 verify(core).touchConfiguration()
75 fun `root album is used if no parent is specified`() {
77 addHttpRequestParameter("name", "new name")
78 addHttpRequestParameter("description", "new description")
79 verifyRedirect("imageBrowser.html?album=album-id")
83 fun `empty album title redirects to error page`() {
85 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
86 addHttpRequestParameter("name", "new name")
87 addHttpRequestParameter("description", "new description")
88 verifyRedirect("emptyAlbumTitle.html")
92 fun `album description is filtered`() {
94 addHttpRequestParameter("name", "new name")
95 addHttpRequestParameter("description", "new http://localhost:12345/KSK@foo description")
96 addHttpRequestHeader("Host", "localhost:12345")
97 verifyRedirect("imageBrowser.html?album=album-id") {
98 verify(newAlbum.modify()).setDescription("new KSK@foo description")