d4268453e670a8819090851de4aa4b2ca2c70555
[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: WebPageTest2(::ImageBrowserPage) {
17
18         @Test
19         fun `page returns correct path`() {
20             assertThat(page.path, equalTo("imageBrowser.html"))
21         }
22
23         @Test
24         fun `page requires login`() {
25             assertThat(page.requiresLogin(), equalTo(true))
26         }
27
28         @Test
29         fun `page returns correct title`() {
30                 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
31             assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
32         }
33
34         @Test
35         fun `get request with album sets album and page in template context`() {
36                 val album = mock<Album>()
37                 addAlbum("album-id", album)
38                 addHttpRequestParameter("album", "album-id")
39                 addHttpRequestParameter("page", "5")
40                 verifyNoRedirect {
41                         assertThat(templateContext["albumRequested"], equalTo<Any>(true))
42                         assertThat(templateContext["album"], equalTo<Any>(album))
43                         assertThat(templateContext["page"], equalTo<Any>("5"))
44                 }
45         }
46
47         @Test
48         fun `get request with image sets image in template context`() {
49                 val image = mock<Image>()
50                 addImage("image-id", image)
51                 addHttpRequestParameter("image", "image-id")
52                 verifyNoRedirect {
53                         assertThat(templateContext["imageRequested"], equalTo<Any>(true))
54                         assertThat(templateContext["image"], equalTo<Any>(image))
55                 }
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                 verifyNoRedirect {
64                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
65                         assertThat(templateContext["sone"], equalTo<Any>(sone))
66                 }
67         }
68
69         @Test
70         fun `get request with mode of gallery sets albums and page in template context`() {
71                 val firstSone = createSone("first album", "second album")
72                 addSone("sone1", firstSone)
73                 val secondSone = createSone("third album", "fourth album")
74                 addSone("sone2", secondSone)
75                 addHttpRequestParameter("mode", "gallery")
76                 verifyNoRedirect {
77                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
78                         @Suppress("UNCHECKED_CAST")
79                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
80                                         firstSone.rootAlbum.albums[0],
81                                         secondSone.rootAlbum.albums[1],
82                                         firstSone.rootAlbum.albums[1],
83                                         secondSone.rootAlbum.albums[0]
84                         ))
85                 }
86         }
87
88         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
89                 return mock<Sone>().apply {
90                         val rootAlbum = mock<Album>()
91                         val firstAlbum = mock<Album>()
92                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
93                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
94                         val secondAlbum = mock<Album>()
95                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
96                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
97                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
98                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
99                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
100                         whenever(this.rootAlbum).thenReturn(rootAlbum)
101                 }
102         }
103
104         @Test
105         fun `requesting nothing will show the albums of the current sone`() {
106                 verifyNoRedirect {
107                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
108                         assertThat(templateContext["sone"], equalTo<Any>(currentSone))
109                 }
110         }
111
112         @Test
113         fun `page is link-excepted`() {
114             assertThat(page.isLinkExcepted(null), equalTo(true))
115         }
116
117 }