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() {
22 private val page = EditAlbumPage(template, webInterface)
24 private val album = mock<Album>()
25 private val parentAlbum = mock<Album>()
26 private val modifier = mockBuilder<Modifier>()
27 private val sone = mock<Sone>()
29 override fun getPage() = page
33 whenever(album.id).thenReturn("album-id")
34 whenever(album.sone).thenReturn(sone)
35 whenever(album.parent).thenReturn(parentAlbum)
36 whenever(album.modify()).thenReturn(modifier)
37 whenever(modifier.update()).thenReturn(album)
38 whenever(parentAlbum.id).thenReturn("parent-id")
39 whenever(sone.isLocal).thenReturn(true)
40 addHttpRequestHeader("Host", "www.te.st")
44 fun `page returns correct path`() {
45 assertThat(page.path, equalTo("editAlbum.html"))
49 fun `page requires login`() {
50 assertThat(page.requiresLogin(), equalTo(true))
54 fun `page returns correct title`() {
55 whenever(l10n.getString("Page.EditAlbum.Title")).thenReturn("edit album page")
56 assertThat(page.getPageTitle(freenetRequest), equalTo("edit album page"))
60 fun `get request does not redirect`() {
61 page.processTemplate(freenetRequest, templateContext)
65 fun `post request with invalid album redirects to invalid page`() {
67 verifyRedirect("invalid.html")
71 fun `post request with album of non-local sone redirects to no permissions page`() {
73 whenever(sone.isLocal).thenReturn(false)
74 addAlbum("album-id", album)
75 addHttpRequestPart("album", "album-id")
76 verifyRedirect("noPermission.html")
80 fun `post request with move left requested moves album to the left and redirects to album browser`() {
82 addAlbum("album-id", album)
83 addHttpRequestPart("album", "album-id")
84 addHttpRequestPart("moveLeft", "true")
85 verifyRedirect("imageBrowser.html?album=parent-id") {
86 verify(parentAlbum).moveAlbumUp(album)
87 verify(core).touchConfiguration()
92 fun `post request with move right requested moves album to the left and redirects to album browser`() {
94 addAlbum("album-id", album)
95 addHttpRequestPart("album", "album-id")
96 addHttpRequestPart("moveRight", "true")
97 verifyRedirect("imageBrowser.html?album=parent-id") {
98 verify(parentAlbum).moveAlbumDown(album)
99 verify(core).touchConfiguration()
104 fun `post request with empty album title redirects to empty album title page`() {
106 addAlbum("album-id", album)
107 addHttpRequestPart("album", "album-id")
108 whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
109 verifyRedirect("emptyAlbumTitle.html")
113 fun `post request with non-empty album title and description redirects to album browser`() {
115 addAlbum("album-id", album)
116 addHttpRequestPart("album", "album-id")
117 addHttpRequestPart("title", "title")
118 addHttpRequestPart("description", "description")
119 verifyRedirect("imageBrowser.html?album=album-id") {
120 verify(modifier).setTitle("title")
121 verify(modifier).setDescription("description")
122 verify(modifier).update()
123 verify(core).touchConfiguration()