1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Album.Modifier
5 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.mockBuilder
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.util.web.Method.POST
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.equalTo
13 import org.junit.Before
15 import org.mockito.Mockito.verify
18 * Unit test for [EditAlbumPage].
20 class EditAlbumPageTest: WebPageTest(::EditAlbumPage) {
22 private val album = mock<Album>()
23 private val parentAlbum = mock<Album>()
24 private val modifier = mockBuilder<Modifier>()
25 private val sone = mock<Sone>()
29 whenever(album.id).thenReturn("album-id")
30 whenever(album.sone).thenReturn(sone)
31 whenever(album.parent).thenReturn(parentAlbum)
32 whenever(album.modify()).thenReturn(modifier)
33 whenever(modifier.update()).thenReturn(album)
34 whenever(parentAlbum.id).thenReturn("parent-id")
35 whenever(sone.isLocal).thenReturn(true)
36 addHttpRequestHeader("Host", "www.te.st")
40 fun `page returns correct path`() {
41 assertThat(page.path, equalTo("editAlbum.html"))
45 fun `page requires login`() {
46 assertThat(page.requiresLogin(), equalTo(true))
50 fun `page returns correct title`() {
51 whenever(l10n.getString("Page.EditAlbum.Title")).thenReturn("edit album page")
52 assertThat(page.getPageTitle(freenetRequest), equalTo("edit album page"))
56 fun `get request does not redirect`() {
57 page.processTemplate(freenetRequest, templateContext)
61 fun `post request with invalid album redirects to invalid page`() {
63 verifyRedirect("invalid.html")
67 fun `post request with album of non-local sone redirects to no permissions page`() {
69 whenever(sone.isLocal).thenReturn(false)
70 addAlbum("album-id", album)
71 addHttpRequestPart("album", "album-id")
72 verifyRedirect("noPermission.html")
76 fun `post request with move left requested moves album to the left and redirects to album browser`() {
78 addAlbum("album-id", album)
79 addHttpRequestPart("album", "album-id")
80 addHttpRequestPart("moveLeft", "true")
81 verifyRedirect("imageBrowser.html?album=parent-id") {
82 verify(parentAlbum).moveAlbumUp(album)
83 verify(core).touchConfiguration()
88 fun `post request with move right requested moves album to the left and redirects to album browser`() {
90 addAlbum("album-id", album)
91 addHttpRequestPart("album", "album-id")
92 addHttpRequestPart("moveRight", "true")
93 verifyRedirect("imageBrowser.html?album=parent-id") {
94 verify(parentAlbum).moveAlbumDown(album)
95 verify(core).touchConfiguration()
100 fun `post request with empty album title redirects to empty album title page`() {
102 addAlbum("album-id", album)
103 addHttpRequestPart("album", "album-id")
104 whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
105 verifyRedirect("emptyAlbumTitle.html")
109 fun `post request with non-empty album title and description redirects to album browser`() {
111 addAlbum("album-id", album)
112 addHttpRequestPart("album", "album-id")
113 addHttpRequestPart("title", "title")
114 addHttpRequestPart("description", "description")
115 verifyRedirect("imageBrowser.html?album=album-id") {
116 verify(modifier).setTitle("title")
117 verify(modifier).setDescription("description")
118 verify(modifier).update()
119 verify(core).touchConfiguration()