1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.data.Album.*
5 import net.pterodactylus.sone.data.Album.Modifier.*
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.web.Method.*
10 import org.hamcrest.MatcherAssert.*
11 import org.hamcrest.Matchers.*
13 import org.mockito.Mockito.*
16 * Unit test for [CreateAlbumPage].
18 class CreateAlbumPageTest : WebPageTest(::CreateAlbumPage) {
20 private val parentAlbum = createAlbum("parent-id")
21 private val newAlbum = createAlbum("album-id")
25 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
26 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
30 fun `page returns correct path`() {
31 assertThat(page.path, equalTo("createAlbum.html"))
35 fun `get request shows template`() {
36 page.processTemplate(freenetRequest, templateContext)
40 fun `missing name results in attribute being set in template context`() {
42 page.processTemplate(freenetRequest, templateContext)
43 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
46 private fun createAlbum(albumId: String) = deepMock<Album>().apply {
47 whenever(id).thenReturn(albumId)
48 selfMock<Modifier>().let { modifier ->
49 whenever(modifier.update()).thenReturn(this@apply)
50 whenever(this@apply.modify()).thenReturn(modifier)
55 fun `title and description are set correctly on the album`() {
57 addAlbum("parent-id", parentAlbum)
58 addHttpRequestPart("name", "new name")
59 addHttpRequestPart("description", "new description")
60 addHttpRequestPart("parent", "parent-id")
61 verifyRedirect("imageBrowser.html?album=album-id") {
62 verify(newAlbum).modify()
63 verify(newAlbum.modify()).setTitle("new name")
64 verify(newAlbum.modify()).setDescription("new description")
65 verify(newAlbum.modify()).update()
66 verify(core).touchConfiguration()
71 fun `root album is used if no parent is specified`() {
73 addHttpRequestPart("name", "new name")
74 addHttpRequestPart("description", "new description")
75 verifyRedirect("imageBrowser.html?album=album-id")
79 fun `empty album title redirects to error page`() {
81 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
82 addHttpRequestPart("name", "new name")
83 addHttpRequestPart("description", "new description")
84 verifyRedirect("emptyAlbumTitle.html")
88 fun `album description is filtered`() {
90 addHttpRequestPart("name", "new name")
91 addHttpRequestPart("description", "new http://localhost:12345/KSK@foo description")
92 addHttpRequestHeader("Host", "localhost:12345")
93 verifyRedirect("imageBrowser.html?album=album-id") {
94 verify(newAlbum.modify()).setDescription("new KSK@foo description")
99 fun `page can be created by dependency injection`() {
100 assertThat(baseInjector.getInstance<CreateAlbumPage>(), notNullValue())
104 fun `page is annotated with correct template path`() {
105 assertThat(page.templatePath, equalTo("/templates/createAlbum.html"))