1 package net.pterodactylus.sone.web.pages
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
19 import org.mockito.Mockito.any
20 import org.mockito.Mockito.eq
21 import org.mockito.Mockito.never
22 import org.mockito.Mockito.verify
25 * Unit test for [UploadImagePage].
27 class UploadImagePageTest: WebPageTest(::UploadImagePage) {
29 private val parentAlbum = mock<Album>().apply {
30 whenever(id).thenReturn("parent-id")
31 whenever(sone).thenReturn(currentSone)
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo("uploadImage.html"))
40 fun `page requires login`() {
41 assertThat(page.requiresLogin(), equalTo(true))
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"))
51 fun `get request does not redirect or upload anything`() {
53 verify(core, never()).createTemporaryImage(any(), any())
54 verify(core, never()).createImage(any(), any(), any())
59 fun `post request without parent results in no permission error page`() {
61 verifyRedirect("noPermission.html")
65 fun `post request with parent that is not the current sone results in no permission error page`() {
67 addHttpRequestPart("parent", "parent-id")
68 whenever(parentAlbum.sone).thenReturn(mock())
69 addAlbum("parent-id", parentAlbum)
70 verifyRedirect("noPermission.html")
74 fun `post request with empty name redirects to error page`() {
76 addAlbum("parent-id", parentAlbum)
77 addHttpRequestPart("parent", "parent-id")
78 addHttpRequestPart("title", " ")
79 verifyRedirect("emptyImageTitle.html")
83 fun `uploading an invalid image results in no redirect and message set in template context`() {
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")
91 verify(core, never()).createTemporaryImage(any(), any())
92 assertThat(templateContext["messages"] as String, equalTo("upload error - invalid image"))
97 fun `uploading a valid image uploads image and redirects to album browser`() {
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)
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()
123 fun `page can be created by dependency injection`() {
124 assertThat(baseInjector.getInstance<UploadImagePage>(), notNullValue())
128 fun `page is annotated with correct template path`() {
129 assertThat(page.templatePath, equalTo("/templates/invalid.html"))