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