1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.data.impl.AlbumImpl
5 import net.pterodactylus.sone.data.impl.ImageImpl
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
15 * Unit test for [ImageBrowserPage].
17 class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
20 fun `page returns correct path`() {
21 assertThat(page.path, equalTo("imageBrowser.html"))
25 fun `page requires login`() {
26 assertThat(page.requiresLogin(), equalTo(true))
30 fun `page returns correct title`() {
31 addTranslation("Page.ImageBrowser.Title", "image browser page title")
32 assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
36 fun `get request with album sets album and page in template context`() {
37 val album = AlbumImpl(currentSone, "album-id")
38 addAlbum("album-id", album)
39 addHttpRequestParameter("album", "album-id")
40 addHttpRequestParameter("page", "5")
42 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
43 assertThat(templateContext["album"], equalTo<Any>(album))
44 assertThat(templateContext["page"], equalTo<Any>("5"))
49 fun `get request with image sets image in template context`() {
50 val image = ImageImpl()
51 addImage("image-id", image)
52 addHttpRequestParameter("image", "image-id")
54 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
55 assertThat(templateContext["image"], equalTo<Any>(image))
60 fun `get request with sone sets sone in template context`() {
61 val sone = mock<Sone>()
62 addSone("sone-id", sone)
63 addHttpRequestParameter("sone", "sone-id")
65 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
66 assertThat(templateContext["sone"], equalTo<Any>(sone))
71 fun `get request with mode of gallery sets albums and page in template context`() {
72 val firstSone = createSone("first album", "second album")
73 addSone("sone1", firstSone)
74 val secondSone = createSone("third album", "fourth album")
75 addSone("sone2", secondSone)
76 addHttpRequestParameter("mode", "gallery")
78 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
79 @Suppress("UNCHECKED_CAST")
80 assertThat(templateContext["albums"] as Iterable<Album>, contains(
81 firstSone.rootAlbum.albums[0],
82 secondSone.rootAlbum.albums[1],
83 firstSone.rootAlbum.albums[1],
84 secondSone.rootAlbum.albums[0]
90 fun `get request for gallery can show second page`() {
91 core.preferences.newImagesPerPage = 2
92 val firstSone = createSone("first album", "second album")
93 addSone("sone1", firstSone)
94 val secondSone = createSone("third album", "fourth album")
95 addSone("sone2", secondSone)
96 addHttpRequestParameter("mode", "gallery")
97 addHttpRequestParameter("page", "1")
99 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
100 @Suppress("UNCHECKED_CAST")
101 assertThat(templateContext["albums"] as Iterable<Album>, contains(
102 firstSone.rootAlbum.albums[1],
103 secondSone.rootAlbum.albums[0]
108 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
109 return mock<Sone>().apply {
110 val rootAlbum = AlbumImpl(this)
111 val firstAlbum = AlbumImpl(this).modify().setTitle(firstAlbumTitle).update()
112 firstAlbum.addImage(ImageImpl("1").modify().setSone(this).setKey("key").update())
113 val secondAlbum = AlbumImpl(this).modify().setTitle(secondAlbumTitle).update()
114 secondAlbum.addImage(ImageImpl("2").modify().setSone(this).setKey("key").update())
115 rootAlbum.addAlbum(firstAlbum)
116 rootAlbum.addAlbum(secondAlbum)
117 whenever(this.rootAlbum).thenReturn(rootAlbum)
122 fun `requesting nothing will show the albums of the current sone`() {
124 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
125 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
130 fun `page is link-excepted`() {
131 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
135 fun `page can be created by dependency injection`() {
136 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
140 fun `page is annotated with correct menuname`() {
141 assertThat(page.menuName, equalTo("ImageBrowser"))
145 fun `page is annotated with correct template path`() {
146 assertThat(page.templatePath, equalTo("/templates/imageBrowser.html"))