1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.web.*
6 import net.pterodactylus.sone.web.page.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
13 * Unit test for [ImageBrowserPage].
15 class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
18 fun `page returns correct path`() {
19 assertThat(page.path, equalTo("imageBrowser.html"))
23 fun `page requires login`() {
24 assertThat(page.requiresLogin(), equalTo(true))
28 fun `page returns correct title`() {
29 addTranslation("Page.ImageBrowser.Title", "image browser page title")
30 assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
34 fun `get request with album sets album and page in template context`() {
35 val album = mock<Album>()
36 addAlbum("album-id", album)
37 addHttpRequestParameter("album", "album-id")
38 addHttpRequestParameter("page", "5")
40 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
41 assertThat(templateContext["album"], equalTo<Any>(album))
42 assertThat(templateContext["page"], equalTo<Any>("5"))
47 fun `get request with image sets image in template context`() {
48 val image = mock<Image>()
49 addImage("image-id", image)
50 addHttpRequestParameter("image", "image-id")
52 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
53 assertThat(templateContext["image"], equalTo<Any>(image))
58 fun `get request with sone sets sone in template context`() {
59 val sone = mock<Sone>()
60 addSone("sone-id", sone)
61 addHttpRequestParameter("sone", "sone-id")
63 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
64 assertThat(templateContext["sone"], equalTo<Any>(sone))
69 fun `get request with mode of gallery sets albums and page in template context`() {
70 val firstSone = createSone("first album", "second album")
71 addSone("sone1", firstSone)
72 val secondSone = createSone("third album", "fourth album")
73 addSone("sone2", secondSone)
74 addHttpRequestParameter("mode", "gallery")
76 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
77 @Suppress("UNCHECKED_CAST")
78 assertThat(templateContext["albums"] as Iterable<Album>, contains(
79 firstSone.rootAlbum.albums[0],
80 secondSone.rootAlbum.albums[1],
81 firstSone.rootAlbum.albums[1],
82 secondSone.rootAlbum.albums[0]
88 fun `get request for gallery can show second page`() {
89 core.preferences.newImagesPerPage = 2
90 val firstSone = createSone("first album", "second album")
91 addSone("sone1", firstSone)
92 val secondSone = createSone("third album", "fourth album")
93 addSone("sone2", secondSone)
94 addHttpRequestParameter("mode", "gallery")
95 addHttpRequestParameter("page", "1")
97 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
98 @Suppress("UNCHECKED_CAST")
99 assertThat(templateContext["albums"] as Iterable<Album>, contains(
100 firstSone.rootAlbum.albums[1],
101 secondSone.rootAlbum.albums[0]
106 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
107 return mock<Sone>().apply {
108 val rootAlbum = mock<Album>()
109 val firstAlbum = mock<Album>()
110 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
111 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
112 val secondAlbum = mock<Album>()
113 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
114 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
115 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
116 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
117 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
118 whenever(this.rootAlbum).thenReturn(rootAlbum)
123 fun `requesting nothing will show the albums of the current sone`() {
125 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
126 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
131 fun `page is link-excepted`() {
132 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
136 fun `page can be created by dependency injection`() {
137 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
141 fun `page is annotated with correct menuname`() {
142 assertThat(page.menuName, equalTo("ImageBrowser"))
146 fun `page is annotated with correct template path`() {
147 assertThat(page.templatePath, equalTo("/templates/imageBrowser.html"))