38ecce46ddc3459272cf4ae1643e96d33bd876ea
[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.*
4 import net.pterodactylus.sone.data.impl.AlbumImpl
5 import net.pterodactylus.sone.data.impl.ImageImpl
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
11 import org.junit.*
12 import java.net.*
13
14 /**
15  * Unit test for [ImageBrowserPage].
16  */
17 class ImageBrowserPageTest : WebPageTest(::ImageBrowserPage) {
18
19         @Test
20         fun `page returns correct path`() {
21                 assertThat(page.path, equalTo("imageBrowser.html"))
22         }
23
24         @Test
25         fun `page requires login`() {
26                 assertThat(page.requiresLogin(), equalTo(true))
27         }
28
29         @Test
30         fun `page returns correct title`() {
31                 addTranslation("Page.ImageBrowser.Title", "image browser page title")
32                 assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
33         }
34
35         @Test
36         fun `get request with album sets album and page in template context`() {
37                 val album = mock<Album>()
38                 addAlbum("album-id", album)
39                 addHttpRequestParameter("album", "album-id")
40                 addHttpRequestParameter("page", "5")
41                 verifyNoRedirect {
42                         assertThat(templateContext["albumRequested"], equalTo<Any>(true))
43                         assertThat(templateContext["album"], equalTo<Any>(album))
44                         assertThat(templateContext["page"], equalTo<Any>("5"))
45                 }
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                 verifyNoRedirect {
54                         assertThat(templateContext["imageRequested"], equalTo<Any>(true))
55                         assertThat(templateContext["image"], equalTo<Any>(image))
56                 }
57         }
58
59         @Test
60         fun `get request with sone sets sone in template context`() {
61                 val sone = mock<Sone>()
62                 addSone("sone-id", sone)
63                 addHttpRequestParameter("sone", "sone-id")
64                 verifyNoRedirect {
65                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
66                         assertThat(templateContext["sone"], equalTo<Any>(sone))
67                 }
68         }
69
70         @Test
71         fun `get request with mode of gallery sets albums and page in template context`() {
72                 val firstSone = createSone("first album", "second album")
73                 addSone("sone1", firstSone)
74                 val secondSone = createSone("third album", "fourth album")
75                 addSone("sone2", secondSone)
76                 addHttpRequestParameter("mode", "gallery")
77                 verifyNoRedirect {
78                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
79                         @Suppress("UNCHECKED_CAST")
80                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
81                                         firstSone.rootAlbum.albums[0],
82                                         secondSone.rootAlbum.albums[1],
83                                         firstSone.rootAlbum.albums[1],
84                                         secondSone.rootAlbum.albums[0]
85                         ))
86                 }
87         }
88
89         @Test
90         fun `get request for gallery can show second page`() {
91                 core.preferences.newImagesPerPage = 2
92                 val firstSone = createSone("first album", "second album")
93                 addSone("sone1", firstSone)
94                 val secondSone = createSone("third album", "fourth album")
95                 addSone("sone2", secondSone)
96                 addHttpRequestParameter("mode", "gallery")
97                 addHttpRequestParameter("page", "1")
98                 verifyNoRedirect {
99                         assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
100                         @Suppress("UNCHECKED_CAST")
101                         assertThat(templateContext["albums"] as Iterable<Album>, contains(
102                                         firstSone.rootAlbum.albums[1],
103                                         secondSone.rootAlbum.albums[0]
104                         ))
105                 }
106         }
107
108         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
109                 return mock<Sone>().apply {
110                         val rootAlbum = AlbumImpl(this)
111                         val firstAlbum = AlbumImpl(this).modify().setTitle(firstAlbumTitle).update()
112                         firstAlbum.addImage(ImageImpl("1").modify().setSone(this).setKey("key").update())
113                         val secondAlbum = AlbumImpl(this).modify().setTitle(secondAlbumTitle).update()
114                         secondAlbum.addImage(ImageImpl("2").modify().setSone(this).setKey("key").update())
115                         rootAlbum.addAlbum(firstAlbum)
116                         rootAlbum.addAlbum(secondAlbum)
117                         whenever(this.rootAlbum).thenReturn(rootAlbum)
118                 }
119         }
120
121         @Test
122         fun `requesting nothing will show the albums of the current sone`() {
123                 verifyNoRedirect {
124                         assertThat(templateContext["soneRequested"], equalTo<Any>(true))
125                         assertThat(templateContext["sone"], equalTo<Any>(currentSone))
126                 }
127         }
128
129         @Test
130         fun `page is link-excepted`() {
131                 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
132         }
133
134         @Test
135         fun `page can be created by dependency injection`() {
136                 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
137         }
138
139         @Test
140         fun `page is annotated with correct menuname`() {
141                 assertThat(page.menuName, equalTo("ImageBrowser"))
142         }
143
144         @Test
145         fun `page is annotated with correct template path`() {
146                 assertThat(page.templatePath, equalTo("/templates/imageBrowser.html"))
147         }
148
149 }