Replace reply-specific interfaces with Kotlin versions
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / EditAlbumAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
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
12 import org.junit.Test
13
14 /**
15  * Unit test for [EditAlbumAjaxPage].
16  */
17 class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::EditAlbumAjaxPage) {
18
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") }
22
23         @Test
24         fun `request without album results in invalid-album-id`() {
25                 assertThatJsonFailed("invalid-album-id")
26         }
27
28         @Test
29         fun `request with non-local album results in not-authorized`() {
30                 whenever(album.sone).thenReturn(sone)
31                 addAlbum(album)
32                 addRequestParameter("album", "album-id")
33                 assertThatJsonFailed("not-authorized")
34         }
35
36         @Test
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)
43                 addAlbum(album)
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"))
49         }
50
51         @Test
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)
58                 addAlbum(album)
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"))
64         }
65
66         @Test
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)
71                 addAlbum(album)
72                 addRequestParameter("album", "album-id")
73                 assertThatJsonFailed("invalid-album-title")
74         }
75
76         @Test
77         fun `request with title and description sets title and filtered description`() {
78                 val album = AlbumImpl(currentSone, "album-id")
79                 addAlbum(album)
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"))
88         }
89
90 }