Add test for edit album ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 6 Sep 2017 05:20:49 +0000 (07:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 6 Sep 2017 05:20:49 +0000 (07:20 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..2d18366
--- /dev/null
@@ -0,0 +1,93 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Album
+import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.data.impl.AlbumImpl
+import net.pterodactylus.sone.test.deepMock
+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
+
+/**
+ * Unit test for [EditAlbumAjaxPage].
+ */
+class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::EditAlbumAjaxPage) {
+
+       private val sone = mock<Sone>()
+       private val localSone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
+       private val album = mock<Album>().apply { whenever(id).thenReturn("album-id") }
+
+       @Test
+       fun `request without album results in invalid-album-id`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-album-id"))
+       }
+
+       @Test
+       fun `request with non-local album results in not-authorized`() {
+               whenever(album.sone).thenReturn(sone)
+               addAlbum(album)
+               addRequestParameter("album", "album-id")
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("not-authorized"))
+       }
+
+       @Test
+       fun `request with moveLeft moves album to the left`() {
+               whenever(album.sone).thenReturn(localSone)
+               val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
+               val parentAlbum = mock<Album>()
+               whenever(parentAlbum.moveAlbumUp(album)).thenReturn(swappedAlbum)
+               whenever(album.parent).thenReturn(parentAlbum)
+               addAlbum(album)
+               addRequestParameter("album", "album-id")
+               addRequestParameter("moveLeft", "true")
+               assertThat(json.isSuccess, equalTo(true))
+               assertThat(json["sourceAlbumId"].asText(), equalTo("album-id"))
+               assertThat(json["destinationAlbumId"].asText(), equalTo("swapped"))
+       }
+
+       @Test
+       fun `request with moveRight moves album to the right`() {
+               whenever(album.sone).thenReturn(localSone)
+               val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
+               val parentAlbum = mock<Album>()
+               whenever(parentAlbum.moveAlbumDown(album)).thenReturn(swappedAlbum)
+               whenever(album.parent).thenReturn(parentAlbum)
+               addAlbum(album)
+               addRequestParameter("album", "album-id")
+               addRequestParameter("moveRight", "true")
+               assertThat(json.isSuccess, equalTo(true))
+               assertThat(json["sourceAlbumId"].asText(), equalTo("album-id"))
+               assertThat(json["destinationAlbumId"].asText(), equalTo("swapped"))
+       }
+
+       @Test
+       fun `request with missing title results in invalid-title`() {
+               whenever(album.sone).thenReturn(localSone)
+               whenever(album.modify()).thenReturn(deepMock())
+               whenever(album.modify().setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
+               addAlbum(album)
+               addRequestParameter("album", "album-id")
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-album-title"))
+       }
+
+       @Test
+       fun `request with title and description sets title and filtered description`() {
+               val album = AlbumImpl(currentSone, "album-id")
+               addAlbum(album)
+               addRequestParameter("album", "album-id")
+               addRequestParameter("title", "new title")
+               addRequestParameter("description", "foo http://127.0.0.1:8888/KSK@foo.html link")
+               addRequestHeader("Host", "127.0.0.1:8888")
+               assertThat(json.isSuccess, equalTo(true))
+               assertThat(json["albumId"].asText(), equalTo("album-id"))
+               assertThat(json["title"].asText(), equalTo("new title"))
+               assertThat(json["description"].asText(), equalTo("foo KSK@foo.html link"))
+       }
+
+}
index cb5ee5a..09e8e85 100644 (file)
@@ -8,6 +8,7 @@ import net.pterodactylus.sone.core.Core
 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.Post
 import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Profile
@@ -69,6 +70,7 @@ abstract class JsonPageTest(
        private val newReplies = mutableMapOf<String, PostReply>()
        private val linkedElements = mutableMapOf<String, LinkedElement>()
        private val notifications = mutableMapOf<String, Notification>()
+       private val albums = mutableMapOf<String, Album>()
 
        @Before
        fun setupWebInterface() {
@@ -89,6 +91,7 @@ abstract class JsonPageTest(
                whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
                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]] }
        }
 
        @Before
@@ -206,6 +209,10 @@ abstract class JsonPageTest(
                linkedElements[link] = LinkedElement(link, failed, loading)
        }
 
+       protected fun addAlbum(album: Album, albumId: String? = null) {
+               albums[albumId ?: album.id] = album
+       }
+
        @Test
        fun `page returns correct path`() {
                assertThat(page.path, equalTo(expectedPath))