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