1 package net.pterodactylus.sone.web.pages
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.*
14 import org.mockito.Mockito.*
15 import org.mockito.Mockito.eq
18 * Unit test for [UploadImagePage].
20 class UploadImagePageTest : WebPageTest(::UploadImagePage) {
22 private val parentAlbum = AlbumImpl(currentSone, "parent-id")
25 fun `page returns correct path`() {
26 assertThat(page.path, equalTo("uploadImage.html"))
30 fun `page requires login`() {
31 assertThat(page.requiresLogin(), equalTo(true))
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"))
41 fun `get request does not redirect or upload anything`() {
43 verify(core, never()).createTemporaryImage(any(), any())
44 verify(core, never()).createImage(any(), any(), any())
49 fun `post request without parent results in no permission error page`() {
51 verifyRedirect("noPermission.html")
55 fun `post request with parent that is not the current sone results in no permission error page`() {
57 val remoteAlbum = AlbumImpl(mock(), "parent-id")
58 addAlbum("parent-id", remoteAlbum)
59 addHttpRequestPart("parent", "parent-id")
60 verifyRedirect("noPermission.html")
64 fun `post request with empty name redirects to error page`() {
66 addAlbum("parent-id", parentAlbum)
67 addHttpRequestPart("parent", "parent-id")
68 addHttpRequestPart("title", " ")
69 verifyRedirect("emptyImageTitle.html")
73 fun `uploading an invalid image results in no redirect and message set in template context`() {
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")
81 verify(core, never()).createTemporaryImage(any(), any())
82 assertThat(templateContext["messages"] as String, equalTo("upload error - invalid image"))
87 fun `uploading a valid image uploads image and redirects to album browser`() {
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"))
108 fun `page can be created by dependency injection`() {
109 assertThat(baseInjector.getInstance<UploadImagePage>(), notNullValue())
113 fun `page is annotated with correct template path`() {
114 assertThat(page.templatePath, equalTo("/templates/invalid.html"))