X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FUploadImagePageTest.kt;h=4c99759e5cb80b8d7fec12497a8520f913f8b5fc;hp=38c5fe140e52cb5e795a12f7dde235758e77552c;hb=2ab560633c24940fa49bdaf569635dd673ad7b19;hpb=05fb821e72072bde52f383bdc5a988da67f66d0c diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt index 38c5fe1..4c99759 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/UploadImagePageTest.kt @@ -1,60 +1,68 @@ package net.pterodactylus.sone.web.pages -import net.pterodactylus.sone.data.Album -import net.pterodactylus.sone.data.Image -import net.pterodactylus.sone.data.Image.Modifier -import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.data.TemporaryImage +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.data.impl.* +import net.pterodactylus.sone.test.getInstance import net.pterodactylus.sone.test.mock -import net.pterodactylus.sone.test.mockBuilder import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.pages.UploadImagePage -import net.pterodactylus.util.web.Method.POST -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.equalTo -import org.junit.Test -import org.mockito.Mockito.any +import net.pterodactylus.sone.web.* +import net.pterodactylus.sone.web.page.* +import net.pterodactylus.util.web.Method.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.mockito.Mockito.* import org.mockito.Mockito.eq -import org.mockito.Mockito.never -import org.mockito.Mockito.verify /** * Unit test for [UploadImagePage]. */ -class UploadImagePageTest : WebPageTest() { +class UploadImagePageTest : WebPageTest(::UploadImagePage) { - private val parentAlbum = mock().apply { - whenever(id).thenReturn("parent-id") - whenever(sone).thenReturn(currentSone) + private val parentAlbum = AlbumImpl(currentSone, "parent-id") + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("uploadImage.html")) } - override fun getPage() = UploadImagePage(template, webInterface) + @Test + fun `page requires login`() { + assertThat(page.requiresLogin(), equalTo(true)) + } + + @Test + fun `page returns correct title`() { + addTranslation("Page.UploadImage.Title", "upload image page title") + assertThat(page.getPageTitle(soneRequest), equalTo("upload image page title")) + } @Test fun `get request does not redirect or upload anything`() { - page.handleRequest(freenetRequest, templateContext) - verify(core, never()).createTemporaryImage(any(), any()) - verify(core, never()).createImage(any(), any(), any()) + verifyNoRedirect { + verify(core, never()).createTemporaryImage(any(), any()) + verify(core, never()).createImage(any(), any(), any()) + } } @Test fun `post request without parent results in no permission error page`() { - request("", POST) + setMethod(POST) verifyRedirect("noPermission.html") } @Test fun `post request with parent that is not the current sone results in no permission error page`() { - request("", POST) + setMethod(POST) + val remoteAlbum = AlbumImpl(mock(), "parent-id") + addAlbum("parent-id", remoteAlbum) addHttpRequestPart("parent", "parent-id") - whenever(parentAlbum.sone).thenReturn(mock()) - addAlbum("parent-id", parentAlbum) verifyRedirect("noPermission.html") } @Test fun `post request with empty name redirects to error page`() { - request("", POST) + setMethod(POST) addAlbum("parent-id", parentAlbum) addHttpRequestPart("parent", "parent-id") addHttpRequestPart("title", " ") @@ -63,39 +71,47 @@ class UploadImagePageTest : WebPageTest() { @Test fun `uploading an invalid image results in no redirect and message set in template context`() { - request("", POST) + setMethod(POST) addAlbum("parent-id", parentAlbum) addHttpRequestPart("parent", "parent-id") addHttpRequestPart("title", "title") - addUploadedFile("image", "image.png", "image/png", "no-image.png") - page.handleRequest(freenetRequest, templateContext) - verify(core, never()).createTemporaryImage(any(), any()) - assertThat(templateContext["messages"] as String?, equalTo("Page.UploadImage.Error.InvalidImage")) + addUploadedFile("image", "image.png", "image/png", "upload-image-invalid-image.png") + addTranslation("Page.UploadImage.Error.InvalidImage", "upload error - invalid image") + verifyNoRedirect { + verify(core, never()).createTemporaryImage(any(), any()) + assertThat(templateContext["messages"] as String, equalTo("upload error - invalid image")) + } } @Test fun `uploading a valid image uploads image and redirects to album browser`() { - request("", POST) + setMethod(POST) addAlbum("parent-id", parentAlbum) addHttpRequestPart("parent", "parent-id") addHttpRequestPart("title", "Title") - addHttpRequestPart("description", "Description") - addUploadedFile("image", "image.png", "image/png", "image.png") + addHttpRequestPart("description", "Description @ http://localhost:8888/KSK@foo") + addHttpRequestHeader("Host", "localhost:8888") + addUploadedFile("image", "upload-image-value-image.png", "image/png", "upload-image-value-image.png") val temporaryImage = TemporaryImage("temp-image") - val imageModifier = mockBuilder() - val image = mock().apply { - whenever(modify()).thenReturn(imageModifier) - } + val image = ImageImpl() whenever(core.createTemporaryImage(eq("image/png"), any())).thenReturn(temporaryImage) whenever(core.createImage(currentSone, parentAlbum, temporaryImage)).thenReturn(image) verifyRedirect("imageBrowser.html?album=parent-id") { - verify(image).modify() - verify(imageModifier).setWidth(2) - verify(imageModifier).setHeight(1) - verify(imageModifier).setTitle("Title") - verify(imageModifier).setDescription("Description") - verify(imageModifier).update() + assertThat(image.width, equalTo(2)) + assertThat(image.height, equalTo(1)) + assertThat(image.title, equalTo("Title")) + assertThat(image.description, equalTo("Description @ KSK@foo")) } } + @Test + fun `page can be created by dependency injection`() { + assertThat(baseInjector.getInstance(), notNullValue()) + } + + @Test + fun `page is annotated with correct template path`() { + assertThat(page.templatePath, equalTo("/templates/invalid.html")) + } + }