X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FCreateAlbumPageTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FCreateAlbumPageTest.kt;h=c4f2ee4da333a96e6b7b8e15c0c0ff92730d8391;hp=0000000000000000000000000000000000000000;hb=de7568a82eb4150bf6d2b0553841b7b69f84c968;hpb=9acbc5bdec4ccb752e0856a501568b0bb6161579 diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPageTest.kt new file mode 100644 index 0000000..c4f2ee4 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreateAlbumPageTest.kt @@ -0,0 +1,105 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Album.Modifier +import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty +import net.pterodactylus.sone.test.deepMock +import net.pterodactylus.sone.test.selfMock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.pages.WebPageTest +import net.pterodactylus.sone.web.pages.CreateAlbumPage +import net.pterodactylus.util.web.Method.POST +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import org.mockito.Mockito.verify + +/** + * Unit test for [CreateAlbumPage]. + */ +class CreateAlbumPageTest: WebPageTest() { + + private val page = CreateAlbumPage(template, webInterface) + + override fun getPage() = page + + private val parentAlbum = createAlbum("parent-id") + private val newAlbum = createAlbum("album-id") + + @Before + fun setupAlbums() { + whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum) + whenever(currentSone.rootAlbum).thenReturn(parentAlbum) + } + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("createAlbum.html")) + } + + @Test + fun `get request shows template`() { + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `missing name results in attribute being set in template context`() { + request("", POST) + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["nameMissing"], equalTo(true)) + } + + private fun createAlbum(albumId: String) = deepMock().apply { + whenever(id).thenReturn(albumId) + selfMock().let { modifier -> + whenever(modifier.update()).thenReturn(this@apply) + whenever(this@apply.modify()).thenReturn(modifier) + } + } + + @Test + fun `title and description are set correctly on the album`() { + request("", POST) + addAlbum("parent-id", parentAlbum) + addHttpRequestParameter("name", "new name") + addHttpRequestParameter("description", "new description") + addHttpRequestParameter("parent", "parent-id") + verifyRedirect("imageBrowser.html?album=album-id") { + verify(newAlbum).modify() + verify(newAlbum.modify()).setTitle("new name") + verify(newAlbum.modify()).setDescription("new description") + verify(newAlbum.modify()).update() + verify(core).touchConfiguration() + } + } + + @Test + fun `root album is used if no parent is specified`() { + request("", POST) + addHttpRequestParameter("name", "new name") + addHttpRequestParameter("description", "new description") + verifyRedirect("imageBrowser.html?album=album-id") + } + + @Test + fun `empty album title redirects to error page`() { + request("", POST) + whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java) + addHttpRequestParameter("name", "new name") + addHttpRequestParameter("description", "new description") + verifyRedirect("emptyAlbumTitle.html") + } + + @Test + fun `album description is filtered`() { + request("", POST) + addHttpRequestParameter("name", "new name") + addHttpRequestParameter("description", "new http://localhost:12345/KSK@foo description") + addHttpRequestHeader("Host", "localhost:12345") + verifyRedirect("imageBrowser.html?album=album-id") { + verify(newAlbum.modify()).setDescription("new KSK@foo description") + } + } + +}