Add test for DI constructability of EditAlbumAjaxPage
[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
16 /**
17  * Unit test for [ImageBrowserPage].
18  */
19 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
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                 val album = mock<Album>()
40                 addAlbum("album-id", album)
41                 addHttpRequestParameter("album", "album-id")
42                 addHttpRequestParameter("page", "5")
43                 verifyNoRedirect {
44                         assertThat(templateContext["albumRequested"], equalTo<Any>(true))
45                         assertThat(templateContext["album"], equalTo<Any>(album))
46                         assertThat(templateContext["page"], equalTo<Any>("5"))
47                 }
48         }
49
50         @Test
51         fun `get request with image sets image in template context`() {
52                 val image = mock<Image>()
53                 addImage("image-id", image)
54                 addHttpRequestParameter("image", "image-id")
55                 verifyNoRedirect {
56                         assertThat(templateContext["imageRequested"], equalTo<Any>(true))
57                         assertThat(templateContext["image"], equalTo<Any>(image))
58                 }
59         }
60
61         @Test
62         fun `get request with sone sets sone in template context`() {
63                 val sone = mock<Sone>()
64                 addSone("sone-id", sone)
65                 addHttpRequestParameter("sone", "sone-id")
66                 verifyNoRedirect {
67                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
68                         assertThat(templateContext["sone"], equalTo<Any>(sone))
69                 }
70         }
71
72         @Test
73         fun `get request with mode of gallery sets albums and page in template context`() {
74                 val firstSone = createSone("first album", "second album")
75                 addSone("sone1", firstSone)
76                 val secondSone = createSone("third album", "fourth album")
77                 addSone("sone2", secondSone)
78                 addHttpRequestParameter("mode", "gallery")
79                 verifyNoRedirect {
80                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
81                         @Suppress("UNCHECKED_CAST")
82                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
83                                         firstSone.rootAlbum.albums[0],
84                                         secondSone.rootAlbum.albums[1],
85                                         firstSone.rootAlbum.albums[1],
86                                         secondSone.rootAlbum.albums[0]
87                         ))
88                 }
89         }
90
91         @Test
92         fun `get request for gallery can show second page`() {
93                 core.preferences.imagesPerPage = 2
94                 val firstSone = createSone("first album", "second album")
95                 addSone("sone1", firstSone)
96                 val secondSone = createSone("third album", "fourth album")
97                 addSone("sone2", secondSone)
98                 addHttpRequestParameter("mode", "gallery")
99                 addHttpRequestParameter("page", "1")
100                 verifyNoRedirect {
101                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
102                         @Suppress("UNCHECKED_CAST")
103                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
104                                         firstSone.rootAlbum.albums[1],
105                                         secondSone.rootAlbum.albums[0]
106                         ))
107                 }
108         }
109
110         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
111                 return mock<Sone>().apply {
112                         val rootAlbum = mock<Album>()
113                         val firstAlbum = mock<Album>()
114                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
115                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
116                         val secondAlbum = mock<Album>()
117                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
118                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
119                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
120                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
121                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
122                         whenever(this.rootAlbum).thenReturn(rootAlbum)
123                 }
124         }
125
126         @Test
127         fun `requesting nothing will show the albums of the current sone`() {
128                 verifyNoRedirect {
129                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
130                         assertThat(templateContext["sone"], equalTo<Any>(currentSone))
131                 }
132         }
133
134         @Test
135         fun `page is link-excepted`() {
136             assertThat(page.isLinkExcepted(null), equalTo(true))
137         }
138
139         @Test
140         fun `page can be created by dependency injection`() {
141             assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
142         }
143
144 }