♻️ Refactor WOT connector
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / CreateAlbumPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.data.Album.*
5 import net.pterodactylus.sone.data.Album.Modifier.*
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.web.Method.*
10 import org.hamcrest.MatcherAssert.*
11 import org.hamcrest.Matchers.*
12 import org.junit.*
13 import org.mockito.Mockito.*
14
15 /**
16  * Unit test for [CreateAlbumPage].
17  */
18 class CreateAlbumPageTest : WebPageTest(::CreateAlbumPage) {
19
20         private val parentAlbum = createAlbum("parent-id")
21         private val newAlbum = createAlbum("album-id")
22
23         @Before
24         fun setupAlbums() {
25                 whenever(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum)
26                 whenever(currentSone.rootAlbum).thenReturn(parentAlbum)
27         }
28
29         @Test
30         fun `page returns correct path`() {
31                 assertThat(page.path, equalTo("createAlbum.html"))
32         }
33
34         @Test
35         fun `get request shows template`() {
36                 page.processTemplate(freenetRequest, templateContext)
37         }
38
39         @Test
40         fun `missing name results in attribute being set in template context`() {
41                 setMethod(POST)
42                 page.processTemplate(freenetRequest, templateContext)
43                 assertThat(templateContext["nameMissing"], equalTo<Any>(true))
44         }
45
46         private fun createAlbum(albumId: String) = deepMock<Album>().apply {
47                 whenever(id).thenReturn(albumId)
48                 selfMock<Modifier>().let { modifier ->
49                         whenever(modifier.update()).thenReturn(this@apply)
50                         whenever(this@apply.modify()).thenReturn(modifier)
51                 }
52         }
53
54         @Test
55         fun `title and description are set correctly on the album`() {
56                 setMethod(POST)
57                 addAlbum("parent-id", parentAlbum)
58                 addHttpRequestPart("name", "new name")
59                 addHttpRequestPart("description", "new description")
60                 addHttpRequestPart("parent", "parent-id")
61                 verifyRedirect("imageBrowser.html?album=album-id") {
62                         verify(newAlbum).modify()
63                         verify(newAlbum.modify()).setTitle("new name")
64                         verify(newAlbum.modify()).setDescription("new description")
65                         verify(newAlbum.modify()).update()
66                         verify(core).touchConfiguration()
67                 }
68         }
69
70         @Test
71         fun `root album is used if no parent is specified`() {
72                 setMethod(POST)
73                 addHttpRequestPart("name", "new name")
74                 addHttpRequestPart("description", "new description")
75                 verifyRedirect("imageBrowser.html?album=album-id")
76         }
77
78         @Test
79         fun `empty album title redirects to error page`() {
80                 setMethod(POST)
81                 whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
82                 addHttpRequestPart("name", "new name")
83                 addHttpRequestPart("description", "new description")
84                 verifyRedirect("emptyAlbumTitle.html")
85         }
86
87         @Test
88         fun `album description is filtered`() {
89                 setMethod(POST)
90                 addHttpRequestPart("name", "new name")
91                 addHttpRequestPart("description", "new http://localhost:12345/KSK@foo description")
92                 addHttpRequestHeader("Host", "localhost:12345")
93                 verifyRedirect("imageBrowser.html?album=album-id") {
94                         verify(newAlbum.modify()).setDescription("new KSK@foo description")
95                 }
96         }
97
98         @Test
99         fun `page can be created by dependency injection`() {
100                 assertThat(baseInjector.getInstance<CreateAlbumPage>(), notNullValue())
101         }
102
103         @Test
104         fun `page is annotated with correct template path`() {
105                 assertThat(page.templatePath, equalTo("/templates/createAlbum.html"))
106         }
107
108 }