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