X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FImageBrowserPageTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FImageBrowserPageTest.kt;h=7a12242513246ce30c81d76e6cd4348b14bea5cb;hp=0000000000000000000000000000000000000000;hb=de7568a82eb4150bf6d2b0553841b7b69f84c968;hpb=9acbc5bdec4ccb752e0856a501568b0bb6161579 diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt new file mode 100644 index 0000000..7a12242 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt @@ -0,0 +1,106 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.pages.ImageBrowserPage +import net.pterodactylus.sone.web.pages.WebPageTest +import net.pterodactylus.util.web.Method.GET +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.equalTo +import org.junit.Test + +/** + * Unit test for [ImageBrowserPage]. + */ +class ImageBrowserPageTest : WebPageTest() { + + private val page = ImageBrowserPage(template, webInterface) + + @Test + fun `get request with album sets album and page in template context`() { + request("", GET) + val album = mock() + addAlbum("album-id", album) + addHttpRequestParameter("album", "album-id") + addHttpRequestParameter("page", "5") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["albumRequested"], equalTo(true)) + assertThat(templateContext["album"], equalTo(album)) + assertThat(templateContext["page"], equalTo("5")) + } + + @Test + fun `get request with image sets image in template context`() { + request("", GET) + val image = mock() + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["imageRequested"], equalTo(true)) + assertThat(templateContext["image"], equalTo(image)) + } + + @Test + fun `get request with sone sets sone in template context`() { + request("", GET) + val sone = mock() + addSone("sone-id", sone) + addHttpRequestParameter("sone", "sone-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["soneRequested"], equalTo(true)) + assertThat(templateContext["sone"], equalTo(sone)) + } + + @Test + fun `get request with mode of gallery sets albums and page in template context`() { + request("", GET) + val firstSone = createSone("first album", "second album") + addSone("sone1", firstSone) + val secondSone = createSone("third album", "fourth album") + addSone("sone2", secondSone) + addHttpRequestParameter("mode", "gallery") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["galleryRequested"], equalTo(true)) + @Suppress("UNCHECKED_CAST") + assertThat(templateContext["albums"] as Iterable, contains( + firstSone.rootAlbum.albums[0], + secondSone.rootAlbum.albums[1], + firstSone.rootAlbum.albums[1], + secondSone.rootAlbum.albums[0] + )) + } + + private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone { + return mock().apply { + val rootAlbum = mock() + val firstAlbum = mock() + val firstImage = mock().run { whenever(isInserted).thenReturn(true); this } + whenever(firstAlbum.images).thenReturn(listOf(firstImage)) + val secondAlbum = mock() + val secondImage = mock().run { whenever(isInserted).thenReturn(true); this } + whenever(secondAlbum.images).thenReturn(listOf(secondImage)) + whenever(firstAlbum.title).thenReturn(firstAlbumTitle) + whenever(secondAlbum.title).thenReturn(secondAlbumTitle) + whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum)) + whenever(this.rootAlbum).thenReturn(rootAlbum) + } + } + + @Test + fun `requesting nothing will show the albums of the current sone`() { + request("", GET) + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["soneRequested"], equalTo(true)) + assertThat(templateContext["sone"], equalTo(currentSone)) + } + + @Test + fun `page is link-excepted`() { + assertThat(page.isLinkExcepted(null), equalTo(true)) + } + +}