1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.data.impl.AlbumImpl
7 import net.pterodactylus.sone.test.deepMock
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
15 * Unit test for [EditAlbumAjaxPage].
17 class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::EditAlbumAjaxPage) {
19 private val sone = mock<Sone>()
20 private val localSone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
21 private val album = mock<Album>().apply { whenever(id).thenReturn("album-id") }
24 fun `request without album results in invalid-album-id`() {
25 assertThatJsonFailed("invalid-album-id")
29 fun `request with non-local album results in not-authorized`() {
30 whenever(album.sone).thenReturn(sone)
32 addRequestParameter("album", "album-id")
33 assertThatJsonFailed("not-authorized")
37 fun `request with moveLeft moves album to the left`() {
38 whenever(album.sone).thenReturn(localSone)
39 val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
40 val parentAlbum = mock<Album>()
41 whenever(parentAlbum.moveAlbumUp(album)).thenReturn(swappedAlbum)
42 whenever(album.parent).thenReturn(parentAlbum)
44 addRequestParameter("album", "album-id")
45 addRequestParameter("moveLeft", "true")
46 assertThatJsonIsSuccessful()
47 assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id"))
48 assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped"))
52 fun `request with moveRight moves album to the right`() {
53 whenever(album.sone).thenReturn(localSone)
54 val swappedAlbum = mock<Album>().apply { whenever(id).thenReturn("swapped") }
55 val parentAlbum = mock<Album>()
56 whenever(parentAlbum.moveAlbumDown(album)).thenReturn(swappedAlbum)
57 whenever(album.parent).thenReturn(parentAlbum)
59 addRequestParameter("album", "album-id")
60 addRequestParameter("moveRight", "true")
61 assertThatJsonIsSuccessful()
62 assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id"))
63 assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped"))
67 fun `request with missing title results in invalid-title`() {
68 whenever(album.sone).thenReturn(localSone)
69 whenever(album.modify()).thenReturn(deepMock())
70 whenever(album.modify().setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
72 addRequestParameter("album", "album-id")
73 assertThatJsonFailed("invalid-album-title")
77 fun `request with title and description sets title and filtered description`() {
78 val album = AlbumImpl(currentSone, "album-id")
80 addRequestParameter("album", "album-id")
81 addRequestParameter("title", "new title")
82 addRequestParameter("description", "foo http://127.0.0.1:8888/KSK@foo.html link")
83 addRequestHeader("Host", "127.0.0.1:8888")
84 assertThatJsonIsSuccessful()
85 assertThat(json["albumId"]?.asText(), equalTo("album-id"))
86 assertThat(json["title"]?.asText(), equalTo("new title"))
87 assertThat(json["description"]?.asText(), equalTo("foo KSK@foo.html link"))