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.getInstance
8 import net.pterodactylus.sone.test.selfMock
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.sone.web.baseInjector
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Before
17 import org.mockito.Mockito.verify
20 * Unit test for [CreateAlbumPage].
22 class CreateAlbumPageTest : WebPageTest(::CreateAlbumPage) {
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<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 addHttpRequestPart("name", "new name")
63 addHttpRequestPart("description", "new description")
64 addHttpRequestPart("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 addHttpRequestPart("name", "new name")
78 addHttpRequestPart("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 addHttpRequestPart("name", "new name")
87 addHttpRequestPart("description", "new description")
88 verifyRedirect("emptyAlbumTitle.html")
92 fun `album description is filtered`() {
94 addHttpRequestPart("name", "new name")
95 addHttpRequestPart("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")
103 fun `page can be created by dependency injection`() {
104 assertThat(baseInjector.getInstance<CreateAlbumPage>(), notNullValue())