Move web pages to their own package
[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.sone.web.pages.ImageBrowserPage
9 import net.pterodactylus.sone.web.pages.WebPageTest
10 import net.pterodactylus.util.web.Method.GET
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Test
15
16 /**
17  * Unit test for [ImageBrowserPage].
18  */
19 class ImageBrowserPageTest : WebPageTest() {
20
21         private val page = ImageBrowserPage(template, webInterface)
22
23         @Test
24         fun `get request with album sets album and page in template context`() {
25                 request("", GET)
26                 val album = mock<Album>()
27                 addAlbum("album-id", album)
28                 addHttpRequestParameter("album", "album-id")
29                 addHttpRequestParameter("page", "5")
30                 page.handleRequest(freenetRequest, templateContext)
31                 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
32                 assertThat(templateContext["album"], equalTo<Any>(album))
33                 assertThat(templateContext["page"], equalTo<Any>("5"))
34         }
35
36         @Test
37         fun `get request with image sets image in template context`() {
38                 request("", GET)
39                 val image = mock<Image>()
40                 addImage("image-id", image)
41                 addHttpRequestParameter("image", "image-id")
42                 page.handleRequest(freenetRequest, templateContext)
43                 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
44                 assertThat(templateContext["image"], equalTo<Any>(image))
45         }
46
47         @Test
48         fun `get request with sone sets sone in template context`() {
49                 request("", GET)
50                 val sone = mock<Sone>()
51                 addSone("sone-id", sone)
52                 addHttpRequestParameter("sone", "sone-id")
53                 page.handleRequest(freenetRequest, templateContext)
54                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
55                 assertThat(templateContext["sone"], equalTo<Any>(sone))
56         }
57
58         @Test
59         fun `get request with mode of gallery sets albums and page in template context`() {
60                 request("", GET)
61                 val firstSone = createSone("first album", "second album")
62                 addSone("sone1", firstSone)
63                 val secondSone = createSone("third album", "fourth album")
64                 addSone("sone2", secondSone)
65                 addHttpRequestParameter("mode", "gallery")
66                 page.handleRequest(freenetRequest, templateContext)
67                 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
68                 @Suppress("UNCHECKED_CAST")
69                 assertThat(templateContext["albums"] as Iterable<Album>, contains(
70                                 firstSone.rootAlbum.albums[0],
71                                 secondSone.rootAlbum.albums[1],
72                                 firstSone.rootAlbum.albums[1],
73                                 secondSone.rootAlbum.albums[0]
74                 ))
75         }
76
77         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
78                 return mock<Sone>().apply {
79                         val rootAlbum = mock<Album>()
80                         val firstAlbum = mock<Album>()
81                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
82                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
83                         val secondAlbum = mock<Album>()
84                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
85                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
86                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
87                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
88                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
89                         whenever(this.rootAlbum).thenReturn(rootAlbum)
90                 }
91         }
92
93         @Test
94         fun `requesting nothing will show the albums of the current sone`() {
95                 request("", GET)
96                 page.handleRequest(freenetRequest, templateContext)
97                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
98                 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
99         }
100
101         @Test
102         fun `page is link-excepted`() {
103             assertThat(page.isLinkExcepted(null), equalTo(true))
104         }
105
106 }