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(::CreateAlbumPage) {
21 private val parentAlbum = createAlbum("parent-id")
22 private val newAlbum = createAlbum("album-id")
26 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
27 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
31 fun `page returns correct path`() {
32 assertThat(page.path, equalTo("createAlbum.html"))
36 fun `get request shows template`() {
37 page.processTemplate(freenetRequest, templateContext)
41 fun `missing name results in attribute being set in template context`() {
43 page.processTemplate(freenetRequest, templateContext)
44 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
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)
56 fun `title and description are set correctly on the album`() {
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()
72 fun `root album is used if no parent is specified`() {
74 addHttpRequestPart("name", "new name")
75 addHttpRequestPart("description", "new description")
76 verifyRedirect("imageBrowser.html?album=album-id")
80 fun `empty album title redirects to error page`() {
82 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
83 addHttpRequestPart("name", "new name")
84 addHttpRequestPart("description", "new description")
85 verifyRedirect("emptyAlbumTitle.html")
89 fun `album description is filtered`() {
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")