1 package net.pterodactylus.sone.web.pages
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 `page returns correct path`() {
23 assertThat(page.path, equalTo("imageBrowser.html"))
27 fun `page requires login`() {
28 assertThat(page.requiresLogin(), equalTo(true))
32 fun `page returns correct title`() {
33 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
34 assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
38 fun `get request with album sets album and page in template context`() {
40 val album = mock<Album>()
41 addAlbum("album-id", album)
42 addHttpRequestParameter("album", "album-id")
43 addHttpRequestParameter("page", "5")
44 page.processTemplate(freenetRequest, templateContext)
45 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
46 assertThat(templateContext["album"], equalTo<Any>(album))
47 assertThat(templateContext["page"], equalTo<Any>("5"))
51 fun `get request with image sets image in template context`() {
53 val image = mock<Image>()
54 addImage("image-id", image)
55 addHttpRequestParameter("image", "image-id")
56 page.processTemplate(freenetRequest, templateContext)
57 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
58 assertThat(templateContext["image"], equalTo<Any>(image))
62 fun `get request with sone sets sone in template context`() {
64 val sone = mock<Sone>()
65 addSone("sone-id", sone)
66 addHttpRequestParameter("sone", "sone-id")
67 page.processTemplate(freenetRequest, templateContext)
68 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
69 assertThat(templateContext["sone"], equalTo<Any>(sone))
73 fun `get request with mode of gallery sets albums and page in template context`() {
75 val firstSone = createSone("first album", "second album")
76 addSone("sone1", firstSone)
77 val secondSone = createSone("third album", "fourth album")
78 addSone("sone2", secondSone)
79 addHttpRequestParameter("mode", "gallery")
80 page.processTemplate(freenetRequest, templateContext)
81 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
82 @Suppress("UNCHECKED_CAST")
83 assertThat(templateContext["albums"] as Iterable<Album>, contains(
84 firstSone.rootAlbum.albums[0],
85 secondSone.rootAlbum.albums[1],
86 firstSone.rootAlbum.albums[1],
87 secondSone.rootAlbum.albums[0]
91 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
92 return mock<Sone>().apply {
93 val rootAlbum = mock<Album>()
94 val firstAlbum = mock<Album>()
95 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
96 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
97 val secondAlbum = mock<Album>()
98 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
99 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
100 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
101 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
102 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
103 whenever(this.rootAlbum).thenReturn(rootAlbum)
108 fun `requesting nothing will show the albums of the current sone`() {
110 page.processTemplate(freenetRequest, templateContext)
111 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
112 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
116 fun `page is link-excepted`() {
117 assertThat(page.isLinkExcepted(null), equalTo(true))