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.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
15 import org.mockito.Mockito.any
16 import org.mockito.Mockito.eq
17 import org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
21 * Unit test for [UploadImagePage].
23 class UploadImagePageTest : WebPageTest() {
25 private val parentAlbum = mock<Album>().apply {
26 whenever(id).thenReturn("parent-id")
27 whenever(sone).thenReturn(currentSone)
30 override fun getPage() = UploadImagePage(template, webInterface)
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())
40 fun `post request without parent results in no permission error page`() {
42 verifyRedirect("noPermission.html")
46 fun `post request with parent that is not the current sone results in no permission error page`() {
48 addHttpRequestPart("parent", "parent-id")
49 whenever(parentAlbum.sone).thenReturn(mock<Sone>())
50 addAlbum("parent-id", parentAlbum)
51 verifyRedirect("noPermission.html")
55 fun `post request with empty name redirects to error page`() {
57 addAlbum("parent-id", parentAlbum)
58 addHttpRequestPart("parent", "parent-id")
59 addHttpRequestPart("title", " ")
60 verifyRedirect("emptyImageTitle.html")
64 fun `uploading an invalid image results in no redirect and message set in template context`() {
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"))
76 fun `uploading a valid image uploads image and redirects to album browser`() {
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)
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()