--- /dev/null
+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"))
+ }
+
+}
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
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() {
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
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))