faf70b8a0a08bc840f7ee89131c8087f6ee471f4
[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.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.baseInjector
10 import net.pterodactylus.sone.web.page.*
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
15 import org.junit.Test
16 import java.net.*
17
18 /**
19  * Unit test for [ImageBrowserPage].
20  */
21 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
22
23         @Test
24         fun `page returns correct path`() {
25             assertThat(page.path, equalTo("imageBrowser.html"))
26         }
27
28         @Test
29         fun `page requires login`() {
30             assertThat(page.requiresLogin(), equalTo(true))
31         }
32
33         @Test
34         fun `page returns correct title`() {
35                 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
36             assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
37         }
38
39         @Test
40         fun `get request with album sets album and page in template context`() {
41                 val album = mock<Album>()
42                 addAlbum("album-id", album)
43                 addHttpRequestParameter("album", "album-id")
44                 addHttpRequestParameter("page", "5")
45                 verifyNoRedirect {
46                         assertThat(templateContext["albumRequested"], equalTo<Any>(true))
47                         assertThat(templateContext["album"], equalTo<Any>(album))
48                         assertThat(templateContext["page"], equalTo<Any>("5"))
49                 }
50         }
51
52         @Test
53         fun `get request with image sets image in template context`() {
54                 val image = mock<Image>()
55                 addImage("image-id", image)
56                 addHttpRequestParameter("image", "image-id")
57                 verifyNoRedirect {
58                         assertThat(templateContext["imageRequested"], equalTo<Any>(true))
59                         assertThat(templateContext["image"], equalTo<Any>(image))
60                 }
61         }
62
63         @Test
64         fun `get request with sone sets sone in template context`() {
65                 val sone = mock<Sone>()
66                 addSone("sone-id", sone)
67                 addHttpRequestParameter("sone", "sone-id")
68                 verifyNoRedirect {
69                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
70                         assertThat(templateContext["sone"], equalTo<Any>(sone))
71                 }
72         }
73
74         @Test
75         fun `get request with mode of gallery sets albums and page in template context`() {
76                 val firstSone = createSone("first album", "second album")
77                 addSone("sone1", firstSone)
78                 val secondSone = createSone("third album", "fourth album")
79                 addSone("sone2", secondSone)
80                 addHttpRequestParameter("mode", "gallery")
81                 verifyNoRedirect {
82                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
83                         @Suppress("UNCHECKED_CAST")
84                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
85                                         firstSone.rootAlbum.albums[0],
86                                         secondSone.rootAlbum.albums[1],
87                                         firstSone.rootAlbum.albums[1],
88                                         secondSone.rootAlbum.albums[0]
89                         ))
90                 }
91         }
92
93         @Test
94         fun `get request for gallery can show second page`() {
95                 core.preferences.newImagesPerPage = 2
96                 val firstSone = createSone("first album", "second album")
97                 addSone("sone1", firstSone)
98                 val secondSone = createSone("third album", "fourth album")
99                 addSone("sone2", secondSone)
100                 addHttpRequestParameter("mode", "gallery")
101                 addHttpRequestParameter("page", "1")
102                 verifyNoRedirect {
103                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
104                         @Suppress("UNCHECKED_CAST")
105                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
106                                         firstSone.rootAlbum.albums[1],
107                                         secondSone.rootAlbum.albums[0]
108                         ))
109                 }
110         }
111
112         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
113                 return mock<Sone>().apply {
114                         val rootAlbum = mock<Album>()
115                         val firstAlbum = mock<Album>()
116                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
117                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
118                         val secondAlbum = mock<Album>()
119                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
120                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
121                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
122                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
123                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
124                         whenever(this.rootAlbum).thenReturn(rootAlbum)
125                 }
126         }
127
128         @Test
129         fun `requesting nothing will show the albums of the current sone`() {
130                 verifyNoRedirect {
131                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
132                         assertThat(templateContext["sone"], equalTo<Any>(currentSone))
133                 }
134         }
135
136         @Test
137         fun `page is link-excepted`() {
138             assertThat(page.isLinkExcepted(URI("")), equalTo(true))
139         }
140
141         @Test
142         fun `page can be created by dependency injection`() {
143             assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
144         }
145
146         @Test
147         fun `page is annotated with correct menuname`() {
148             assertThat(page.menuName, equalTo("ImageBrowser"))
149         }
150
151 }