From b6dc376162d13042d430c0cdccc7e53c92e4a690 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 6 Sep 2017 07:20:49 +0200 Subject: [PATCH] Add test for edit album ajax page --- .../sone/web/ajax/EditAlbumAjaxPageTest.kt | 93 ++++++++++++++++++++++ .../pterodactylus/sone/web/ajax/JsonPageTest.kt | 7 ++ 2 files changed, 100 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.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 index 0000000..2d18366 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt @@ -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() + private val localSone = mock().apply { whenever(isLocal).thenReturn(true) } + private val album = mock().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().apply { whenever(id).thenReturn("swapped") } + val parentAlbum = mock() + 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().apply { whenever(id).thenReturn("swapped") } + val parentAlbum = mock() + 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")) + } + +} 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 cb5ee5a..09e8e85 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -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() private val linkedElements = mutableMapOf() private val notifications = mutableMapOf() + private val albums = mutableMapOf() @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)) -- 2.7.4