🔀 Merge “release/v81” into “master”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / ImageBrowserPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
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.*
9 import org.junit.*
10 import java.net.*
11
12 /**
13  * Unit test for [ImageBrowserPage].
14  */
15 class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
16
17         @Test
18         fun `page returns correct path`() {
19                 assertThat(page.path, equalTo("imageBrowser.html"))
20         }
21
22         @Test
23         fun `page requires login`() {
24                 assertThat(page.requiresLogin(), equalTo(true))
25         }
26
27         @Test
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"))
31         }
32
33         @Test
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")
39                 verifyNoRedirect {
40                         assertThat(templateContext["albumRequested"], equalTo<Any>(true))
41                         assertThat(templateContext["album"], equalTo<Any>(album))
42                         assertThat(templateContext["page"], equalTo<Any>("5"))
43                 }
44         }
45
46         @Test
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")
51                 verifyNoRedirect {
52                         assertThat(templateContext["imageRequested"], equalTo<Any>(true))
53                         assertThat(templateContext["image"], equalTo<Any>(image))
54                 }
55         }
56
57         @Test
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")
62                 verifyNoRedirect {
63                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
64                         assertThat(templateContext["sone"], equalTo<Any>(sone))
65                 }
66         }
67
68         @Test
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")
75                 verifyNoRedirect {
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]
83                         ))
84                 }
85         }
86
87         @Test
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")
96                 verifyNoRedirect {
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]
102                         ))
103                 }
104         }
105
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)
119                 }
120         }
121
122         @Test
123         fun `requesting nothing will show the albums of the current sone`() {
124                 verifyNoRedirect {
125                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
126                         assertThat(templateContext["sone"], equalTo<Any>(currentSone))
127                 }
128         }
129
130         @Test
131         fun `page is link-excepted`() {
132                 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
133         }
134
135         @Test
136         fun `page can be created by dependency injection`() {
137                 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
138         }
139
140         @Test
141         fun `page is annotated with correct menuname`() {
142                 assertThat(page.menuName, equalTo("ImageBrowser"))
143         }
144
145         @Test
146         fun `page is annotated with correct template path`() {
147                 assertThat(page.templatePath, equalTo("/templates/imageBrowser.html"))
148         }
149
150 }