Replace image browser page with Kotlin version
[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.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.web.Method.GET
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
12 import org.junit.Test
13
14 /**
15  * Unit test for [ImageBrowserPage].
16  */
17 class ImageBrowserPageTest : WebPageTest() {
18
19         private val page = ImageBrowserPage(template, webInterface)
20
21         @Test
22         fun `page returns correct path`() {
23             assertThat(page.path, equalTo("imageBrowser.html"))
24         }
25
26         @Test
27         fun `page requires login`() {
28             assertThat(page.requiresLogin(), equalTo(true))
29         }
30
31         @Test
32         fun `page returns correct title`() {
33                 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
34             assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
35         }
36
37         @Test
38         fun `get request with album sets album and page in template context`() {
39                 request("", GET)
40                 val album = mock<Album>()
41                 addAlbum("album-id", album)
42                 addHttpRequestParameter("album", "album-id")
43                 addHttpRequestParameter("page", "5")
44                 page.processTemplate(freenetRequest, templateContext)
45                 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
46                 assertThat(templateContext["album"], equalTo<Any>(album))
47                 assertThat(templateContext["page"], equalTo<Any>("5"))
48         }
49
50         @Test
51         fun `get request with image sets image in template context`() {
52                 request("", GET)
53                 val image = mock<Image>()
54                 addImage("image-id", image)
55                 addHttpRequestParameter("image", "image-id")
56                 page.processTemplate(freenetRequest, templateContext)
57                 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
58                 assertThat(templateContext["image"], equalTo<Any>(image))
59         }
60
61         @Test
62         fun `get request with sone sets sone in template context`() {
63                 request("", GET)
64                 val sone = mock<Sone>()
65                 addSone("sone-id", sone)
66                 addHttpRequestParameter("sone", "sone-id")
67                 page.processTemplate(freenetRequest, templateContext)
68                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
69                 assertThat(templateContext["sone"], equalTo<Any>(sone))
70         }
71
72         @Test
73         fun `get request with mode of gallery sets albums and page in template context`() {
74                 request("", GET)
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                 page.processTemplate(freenetRequest, templateContext)
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         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
92                 return mock<Sone>().apply {
93                         val rootAlbum = mock<Album>()
94                         val firstAlbum = mock<Album>()
95                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
96                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
97                         val secondAlbum = mock<Album>()
98                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
99                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
100                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
101                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
102                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
103                         whenever(this.rootAlbum).thenReturn(rootAlbum)
104                 }
105         }
106
107         @Test
108         fun `requesting nothing will show the albums of the current sone`() {
109                 request("", GET)
110                 page.processTemplate(freenetRequest, templateContext)
111                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
112                 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
113         }
114
115         @Test
116         fun `page is link-excepted`() {
117             assertThat(page.isLinkExcepted(null), equalTo(true))
118         }
119
120 }