Clean up some imports
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / UploadImagePageTest.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.Image.Modifier
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.data.TemporaryImage
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.mockBuilder
10 import net.pterodactylus.sone.test.whenever
11 import net.pterodactylus.util.web.Method.POST
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.equalTo
14 import org.junit.Test
15 import org.mockito.Mockito.any
16 import org.mockito.Mockito.eq
17 import org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
19
20 /**
21  * Unit test for [UploadImagePage].
22  */
23 class UploadImagePageTest : WebPageTest() {
24
25         private val parentAlbum = mock<Album>().apply {
26                 whenever(id).thenReturn("parent-id")
27                 whenever(sone).thenReturn(currentSone)
28         }
29
30         override fun getPage() = UploadImagePage(template, webInterface)
31
32         @Test
33         fun `get request does not redirect or upload anything`() {
34                 page.handleRequest(freenetRequest, templateContext)
35                 verify(core, never()).createTemporaryImage(any(), any())
36                 verify(core, never()).createImage(any(), any(), any())
37         }
38
39         @Test
40         fun `post request without parent results in no permission error page`() {
41                 setMethod(POST)
42                 verifyRedirect("noPermission.html")
43         }
44
45         @Test
46         fun `post request with parent that is not the current sone results in no permission error page`() {
47                 setMethod(POST)
48                 addHttpRequestPart("parent", "parent-id")
49                 whenever(parentAlbum.sone).thenReturn(mock<Sone>())
50                 addAlbum("parent-id", parentAlbum)
51                 verifyRedirect("noPermission.html")
52         }
53
54         @Test
55         fun `post request with empty name redirects to error page`() {
56                 setMethod(POST)
57                 addAlbum("parent-id", parentAlbum)
58                 addHttpRequestPart("parent", "parent-id")
59                 addHttpRequestPart("title", " ")
60                 verifyRedirect("emptyImageTitle.html")
61         }
62
63         @Test
64         fun `uploading an invalid image results in no redirect and message set in template context`() {
65                 setMethod(POST)
66                 addAlbum("parent-id", parentAlbum)
67                 addHttpRequestPart("parent", "parent-id")
68                 addHttpRequestPart("title", "title")
69                 addUploadedFile("image", "image.png", "image/png", "no-image.png")
70                 page.handleRequest(freenetRequest, templateContext)
71                 verify(core, never()).createTemporaryImage(any(), any())
72                 assertThat(templateContext["messages"] as String?, equalTo<String>("Page.UploadImage.Error.InvalidImage"))
73         }
74
75         @Test
76         fun `uploading a valid image uploads image and redirects to album browser`() {
77                 setMethod(POST)
78                 addAlbum("parent-id", parentAlbum)
79                 addHttpRequestPart("parent", "parent-id")
80                 addHttpRequestPart("title", "Title")
81                 addHttpRequestPart("description", "Description")
82                 addUploadedFile("image", "image.png", "image/png", "image.png")
83                 val temporaryImage = TemporaryImage("temp-image")
84                 val imageModifier = mockBuilder<Modifier>()
85                 val image = mock<Image>().apply {
86                         whenever(modify()).thenReturn(imageModifier)
87                 }
88                 whenever(core.createTemporaryImage(eq("image/png"), any())).thenReturn(temporaryImage)
89                 whenever(core.createImage(currentSone, parentAlbum, temporaryImage)).thenReturn(image)
90                 verifyRedirect("imageBrowser.html?album=parent-id") {
91                         verify(image).modify()
92                         verify(imageModifier).setWidth(2)
93                         verify(imageModifier).setHeight(1)
94                         verify(imageModifier).setTitle("Title")
95                         verify(imageModifier).setDescription("Description")
96                         verify(imageModifier).update()
97                 }
98         }
99
100 }