From: David ‘Bombe’ Roden Date: Thu, 7 Sep 2017 12:18:53 +0000 (+0200) Subject: Add test for edit image ajax page X-Git-Tag: 0.9.7^2~85 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=51b58fad3a00b2d9cae45345ac066e89233ba0b5;p=Sone.git Add test for edit image ajax page --- diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt index d3e8dad..c4fd7cb 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt @@ -25,3 +25,5 @@ inline fun whenever(methodCall: T) = Mockito.`when`(methodCall inline fun OngoingStubbing.thenReturnMock(): OngoingStubbing = this.thenReturn(mock()) operator fun InvocationOnMock.get(index: Int): T = getArgument(index) + +inline fun argumentCaptor(): ArgumentCaptor = ArgumentCaptor.forClass(T::class.java)!! diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt new file mode 100644 index 0000000..6a3d89c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt @@ -0,0 +1,119 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.data.impl.ImageImpl +import net.pterodactylus.sone.template.ParserFilter +import net.pterodactylus.sone.template.RenderFilter +import net.pterodactylus.sone.template.ShortenFilter +import net.pterodactylus.sone.test.argumentCaptor +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mockito.verify + +/** + * Unit test for [EditImageAjaxPage]. + */ +class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") { + + private val parserFilter = mock() + private val shortenFilter = mock() + private val renderFilter = mock() + override val page: JsonPage get() = EditImageAjaxPage(webInterface, parserFilter, shortenFilter, renderFilter) + + @Test + fun `request without image results in invalid-image-id`() { + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("invalid-image-id")) + } + + @Test + fun `request with non-local image results in not-authorized`() { + val image = mock() + val sone = mock() + whenever(image.sone).thenReturn(sone) + addImage(image, "image-id") + addRequestParameter("image", "image-id") + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("not-authorized")) + } + + @Test + fun `moving an image to the left returns the correct values`() { + val image = mock().apply { whenever(id).thenReturn("image-id") } + val sone = mock().apply { whenever(isLocal).thenReturn(true) } + whenever(image.sone).thenReturn(sone) + val swapped = mock().apply { whenever(id).thenReturn("swapped") } + val album = mock() + whenever(album.moveImageUp(image)).thenReturn(swapped) + whenever(image.album).thenReturn(album) + addImage(image) + addRequestParameter("image", "image-id") + addRequestParameter("moveLeft", "true") + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["sourceImageId"].asText(), equalTo("image-id")) + assertThat(json["destinationImageId"].asText(), equalTo("swapped")) + verify(core).touchConfiguration() + } + + @Test + fun `moving an image to the right returns the correct values`() { + val image = mock().apply { whenever(id).thenReturn("image-id") } + val sone = mock().apply { whenever(isLocal).thenReturn(true) } + whenever(image.sone).thenReturn(sone) + val swapped = mock().apply { whenever(id).thenReturn("swapped") } + val album = mock() + whenever(album.moveImageDown(image)).thenReturn(swapped) + whenever(image.album).thenReturn(album) + addImage(image) + addRequestParameter("image", "image-id") + addRequestParameter("moveRight", "true") + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["sourceImageId"].asText(), equalTo("image-id")) + assertThat(json["destinationImageId"].asText(), equalTo("swapped")) + verify(core).touchConfiguration() + } + + @Test + fun `request with empty title results in invalid-image-title`() { + val image = mock().apply { whenever(id).thenReturn("image-id") } + val sone = mock().apply { whenever(isLocal).thenReturn(true) } + whenever(image.sone).thenReturn(sone) + addImage(image) + addRequestParameter("image", "image-id") + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("invalid-image-title")) + } + + @Test + fun `request with title and description returns correct values`() { + val sone = mock().apply { whenever(isLocal).thenReturn(true) } + val image = ImageImpl("image-id").modify().setSone(sone).update() + val parsed = Object() + val shortened = Object() + val rendered = "rendered description" + whenever(parserFilter.format(any(), eq("some KSK@foo link"), any())).thenReturn(parsed) + whenever(shortenFilter.format(any(), eq(parsed), any())).thenReturn(shortened) + whenever(renderFilter.format(any(), eq(shortened), any())).thenReturn(rendered) + addImage(image) + addRequestParameter("image", "image-id") + addRequestParameter("title", "some title") + addRequestParameter("description", "some http://127.0.0.1:8888/KSK@foo link") + addRequestHeader("Host", "127.0.0.1:8888") + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["title"].asText(), equalTo("some title")) + assertThat(json["description"].asText(), equalTo("some KSK@foo link")) + assertThat(json["parsedDescription"].asText(), equalTo("rendered description")) + verify(core).touchConfiguration() + val parameterCaptor = argumentCaptor>() + verify(parserFilter).format(any(), any(), parameterCaptor.capture()) + assertThat(parameterCaptor.value["sone"], equalTo(sone)) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index 09e8e85..4dddbe5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -9,6 +9,7 @@ import net.pterodactylus.sone.core.ElementLoader import net.pterodactylus.sone.core.LinkedElement import net.pterodactylus.sone.core.Preferences import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Profile @@ -71,6 +72,7 @@ abstract class JsonPageTest( private val linkedElements = mutableMapOf() private val notifications = mutableMapOf() private val albums = mutableMapOf() + private val images = mutableMapOf() @Before fun setupWebInterface() { @@ -92,6 +94,8 @@ abstract class JsonPageTest( whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() } whenever(core.getPostReply(anyString())).then { replies[it[0]].asOptional() } whenever(core.getAlbum(anyString())).then { albums[it[0]] } + whenever(core.getImage(anyString())).then { images[it[0]] } + whenever(core.getImage(anyString(), anyBoolean())).then { images[it[0]] } } @Before @@ -213,6 +217,10 @@ abstract class JsonPageTest( albums[albumId ?: album.id] = album } + protected fun addImage(image: Image, imageId: String? = null) { + images[imageId ?: image.id] = image + } + @Test fun `page returns correct path`() { assertThat(page.path, equalTo(expectedPath))