1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.web.Method.GET
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
15 * Unit test for [ImageBrowserPage].
17 class ImageBrowserPageTest : WebPageTest() {
19 private val page = ImageBrowserPage(template, webInterface)
22 fun `get request with album sets album and page in template context`() {
24 val album = mock<Album>()
25 addAlbum("album-id", album)
26 addHttpRequestParameter("album", "album-id")
27 addHttpRequestParameter("page", "5")
28 page.handleRequest(freenetRequest, templateContext)
29 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
30 assertThat(templateContext["album"], equalTo<Any>(album))
31 assertThat(templateContext["page"], equalTo<Any>("5"))
35 fun `get request with image sets image in template context`() {
37 val image = mock<Image>()
38 addImage("image-id", image)
39 addHttpRequestParameter("image", "image-id")
40 page.handleRequest(freenetRequest, templateContext)
41 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
42 assertThat(templateContext["image"], equalTo<Any>(image))
46 fun `get request with sone sets sone in template context`() {
48 val sone = mock<Sone>()
49 addSone("sone-id", sone)
50 addHttpRequestParameter("sone", "sone-id")
51 page.handleRequest(freenetRequest, templateContext)
52 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
53 assertThat(templateContext["sone"], equalTo<Any>(sone))
57 fun `get request with mode of gallery sets albums and page in template context`() {
59 val firstSone = createSone("first album", "second album")
60 addSone("sone1", firstSone)
61 val secondSone = createSone("third album", "fourth album")
62 addSone("sone2", secondSone)
63 addHttpRequestParameter("mode", "gallery")
64 page.handleRequest(freenetRequest, templateContext)
65 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
66 @Suppress("UNCHECKED_CAST")
67 assertThat(templateContext["albums"] as Iterable<Album>, contains(
68 firstSone.rootAlbum.albums[0],
69 secondSone.rootAlbum.albums[1],
70 firstSone.rootAlbum.albums[1],
71 secondSone.rootAlbum.albums[0]
75 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
76 return mock<Sone>().apply {
77 val rootAlbum = mock<Album>()
78 val firstAlbum = mock<Album>()
79 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
80 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
81 val secondAlbum = mock<Album>()
82 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
83 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
84 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
85 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
86 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
87 whenever(this.rootAlbum).thenReturn(rootAlbum)
92 fun `requesting nothing will show the albums of the current sone`() {
94 page.handleRequest(freenetRequest, templateContext)
95 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
96 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
100 fun `page is link-excepted`() {
101 assertThat(page.isLinkExcepted(null), equalTo(true))