Add test for edit image ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Sep 2017 12:18:53 +0000 (14:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Sep 2017 18:18:13 +0000 (20:18 +0200)
src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

index d3e8dad..c4fd7cb 100644 (file)
@@ -25,3 +25,5 @@ inline fun <reified T: Any?> whenever(methodCall: T) = Mockito.`when`(methodCall
 inline fun <reified T : Any> OngoingStubbing<T>.thenReturnMock(): OngoingStubbing<T> = this.thenReturn(mock<T>())
 
 operator fun <T> InvocationOnMock.get(index: Int): T = getArgument(index)
+
+inline fun <reified T> argumentCaptor(): ArgumentCaptor<T> = ArgumentCaptor.forClass<T, T>(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 (file)
index 0000000..6a3d89c
--- /dev/null
@@ -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<ParserFilter>()
+       private val shortenFilter = mock<ShortenFilter>()
+       private val renderFilter = mock<RenderFilter>()
+       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<Image>()
+               val sone = mock<Sone>()
+               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<Image>().apply { whenever(id).thenReturn("image-id") }
+               val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
+               whenever(image.sone).thenReturn(sone)
+               val swapped = mock<Image>().apply { whenever(id).thenReturn("swapped") }
+               val album = mock<Album>()
+               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<Image>().apply { whenever(id).thenReturn("image-id") }
+               val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
+               whenever(image.sone).thenReturn(sone)
+               val swapped = mock<Image>().apply { whenever(id).thenReturn("swapped") }
+               val album = mock<Album>()
+               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<Image>().apply { whenever(id).thenReturn("image-id") }
+               val sone = mock<Sone>().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<Sone>().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<MutableMap<String, Any?>>()
+               verify(parserFilter).format(any(), any(), parameterCaptor.capture())
+               assertThat(parameterCaptor.value["sone"], equalTo<Any>(sone))
+       }
+
+}
index 09e8e85..4dddbe5 100644 (file)
@@ -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<String, LinkedElement>()
        private val notifications = mutableMapOf<String, Notification>()
        private val albums = mutableMapOf<String, Album>()
+       private val images = mutableMapOf<String, Image>()
 
        @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))