♻️ Move generic error pages to custom classes
[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.getInstance
9 import net.pterodactylus.sone.test.mock
10 import net.pterodactylus.sone.test.mockBuilder
11 import net.pterodactylus.sone.test.whenever
12 import net.pterodactylus.sone.web.baseInjector
13 import net.pterodactylus.sone.web.page.*
14 import net.pterodactylus.util.web.Method.POST
15 import org.hamcrest.MatcherAssert.assertThat
16 import org.hamcrest.Matchers.equalTo
17 import org.hamcrest.Matchers.notNullValue
18 import org.junit.Test
19 import org.mockito.Mockito.any
20 import org.mockito.Mockito.eq
21 import org.mockito.Mockito.never
22 import org.mockito.Mockito.verify
23
24 /**
25  * Unit test for [UploadImagePage].
26  */
27 class UploadImagePageTest: WebPageTest(::UploadImagePage) {
28
29         private val parentAlbum = mock<Album>().apply {
30                 whenever(id).thenReturn("parent-id")
31                 whenever(sone).thenReturn(currentSone)
32         }
33
34         @Test
35         fun `page returns correct path`() {
36             assertThat(page.path, equalTo("uploadImage.html"))
37         }
38
39         @Test
40         fun `page requires login`() {
41             assertThat(page.requiresLogin(), equalTo(true))
42         }
43
44         @Test
45         fun `page returns correct title`() {
46             addTranslation("Page.UploadImage.Title", "upload image page title")
47                 assertThat(page.getPageTitle(soneRequest), equalTo("upload image page title"))
48         }
49
50         @Test
51         fun `get request does not redirect or upload anything`() {
52                 verifyNoRedirect {
53                         verify(core, never()).createTemporaryImage(any(), any())
54                         verify(core, never()).createImage(any(), any(), any())
55                 }
56         }
57
58         @Test
59         fun `post request without parent results in no permission error page`() {
60                 setMethod(POST)
61                 verifyRedirect("noPermission.html")
62         }
63
64         @Test
65         fun `post request with parent that is not the current sone results in no permission error page`() {
66                 setMethod(POST)
67                 addHttpRequestPart("parent", "parent-id")
68                 whenever(parentAlbum.sone).thenReturn(mock())
69                 addAlbum("parent-id", parentAlbum)
70                 verifyRedirect("noPermission.html")
71         }
72
73         @Test
74         fun `post request with empty name redirects to error page`() {
75                 setMethod(POST)
76                 addAlbum("parent-id", parentAlbum)
77                 addHttpRequestPart("parent", "parent-id")
78                 addHttpRequestPart("title", " ")
79                 verifyRedirect("emptyImageTitle.html")
80         }
81
82         @Test
83         fun `uploading an invalid image results in no redirect and message set in template context`() {
84                 setMethod(POST)
85                 addAlbum("parent-id", parentAlbum)
86                 addHttpRequestPart("parent", "parent-id")
87                 addHttpRequestPart("title", "title")
88                 addUploadedFile("image", "image.png", "image/png", "upload-image-invalid-image.png")
89                 addTranslation("Page.UploadImage.Error.InvalidImage", "upload error - invalid image")
90                 verifyNoRedirect {
91                         verify(core, never()).createTemporaryImage(any(), any())
92                         assertThat(templateContext["messages"] as String, equalTo("upload error - invalid image"))
93                 }
94         }
95
96         @Test
97         fun `uploading a valid image uploads image and redirects to album browser`() {
98                 setMethod(POST)
99                 addAlbum("parent-id", parentAlbum)
100                 addHttpRequestPart("parent", "parent-id")
101                 addHttpRequestPart("title", "Title")
102                 addHttpRequestPart("description", "Description @ http://localhost:8888/KSK@foo")
103                 addHttpRequestHeader("Host", "localhost:8888")
104                 addUploadedFile("image", "upload-image-value-image.png", "image/png", "upload-image-value-image.png")
105                 val temporaryImage = TemporaryImage("temp-image")
106                 val imageModifier = mockBuilder<Modifier>()
107                 val image = mock<Image>().apply {
108                         whenever(modify()).thenReturn(imageModifier)
109                 }
110                 whenever(core.createTemporaryImage(eq("image/png"), any())).thenReturn(temporaryImage)
111                 whenever(core.createImage(currentSone, parentAlbum, temporaryImage)).thenReturn(image)
112                 verifyRedirect("imageBrowser.html?album=parent-id") {
113                         verify(image).modify()
114                         verify(imageModifier).setWidth(2)
115                         verify(imageModifier).setHeight(1)
116                         verify(imageModifier).setTitle("Title")
117                         verify(imageModifier).setDescription("Description @ KSK@foo")
118                         verify(imageModifier).update()
119                 }
120         }
121
122         @Test
123         fun `page can be created by dependency injection`() {
124             assertThat(baseInjector.getInstance<UploadImagePage>(), notNullValue())
125         }
126
127         @Test
128         fun `page is annotated with correct template path`() {
129             assertThat(page.templatePath, equalTo("/templates/invalid.html"))
130         }
131
132 }