🔥 Remove unused session manager from page toadlet
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / EditImageAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.data.impl.*
7 import net.pterodactylus.sone.template.ParserFilter
8 import net.pterodactylus.sone.template.RenderFilter
9 import net.pterodactylus.sone.template.ShortenFilter
10 import net.pterodactylus.sone.test.argumentCaptor
11 import net.pterodactylus.sone.test.getInstance
12 import net.pterodactylus.sone.test.isProvidedByMock
13 import net.pterodactylus.sone.test.mock
14 import net.pterodactylus.sone.test.whenever
15 import net.pterodactylus.sone.web.baseInjector
16 import org.hamcrest.MatcherAssert.assertThat
17 import org.hamcrest.Matchers.equalTo
18 import org.hamcrest.Matchers.notNullValue
19 import org.junit.Test
20 import org.mockito.ArgumentMatchers.any
21 import org.mockito.ArgumentMatchers.eq
22 import org.mockito.Mockito.verify
23
24 /**
25  * Unit test for [EditImageAjaxPage].
26  */
27 class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
28
29         private val parserFilter = mock<ParserFilter>()
30         private val shortenFilter = mock<ShortenFilter>()
31         private val renderFilter = mock<RenderFilter>()
32         override val page: JsonPage get() = EditImageAjaxPage(webInterface, parserFilter, shortenFilter, renderFilter)
33
34         @Test
35         fun `request without image results in invalid-image-id`() {
36                 assertThatJsonFailed("invalid-image-id")
37         }
38
39         @Test
40         fun `request with non-local image results in not-authorized`() {
41                 val sone = mock<Sone>()
42                 val image = ImageImpl().modify().setSone(sone).update()
43                 addImage(image, "image-id")
44                 addRequestParameter("image", "image-id")
45                 assertThatJsonFailed("not-authorized")
46         }
47
48         @Test
49         fun `moving an image to the left returns the correct values`() {
50                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
51                 val image = ImageImpl("image-id").modify().setSone(sone).update()
52                 AlbumImpl(sone).also {
53                         it.addImage(ImageImpl("swapped").modify().setSone(sone).update())
54                         it.addImage(image)
55                 }
56                 addImage(image)
57                 addRequestParameter("image", "image-id")
58                 addRequestParameter("moveLeft", "true")
59                 assertThatJsonIsSuccessful()
60                 assertThat(json["sourceImageId"]?.asText(), equalTo("image-id"))
61                 assertThat(json["destinationImageId"]?.asText(), equalTo("swapped"))
62                 verify(core).touchConfiguration()
63         }
64
65         @Test
66         fun `moving an image to the right returns the correct values`() {
67                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
68                 val image = ImageImpl("image-id").modify().setSone(sone).update()
69                 AlbumImpl(sone).also {
70                         it.addImage(image)
71                         it.addImage(ImageImpl("swapped").modify().setSone(sone).update())
72                 }
73                 addImage(image)
74                 addRequestParameter("image", "image-id")
75                 addRequestParameter("moveRight", "true")
76                 assertThatJsonIsSuccessful()
77                 assertThat(json["sourceImageId"]?.asText(), equalTo("image-id"))
78                 assertThat(json["destinationImageId"]?.asText(), equalTo("swapped"))
79                 verify(core).touchConfiguration()
80         }
81
82         @Test
83         fun `request with empty title results in invalid-image-title`() {
84                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
85                 val image = ImageImpl("image-id").modify().setSone(sone).update()
86                 addImage(image)
87                 addRequestParameter("image", "image-id")
88                 assertThatJsonFailed("invalid-image-title")
89         }
90
91         @Test
92         fun `request with title and description returns correct values`() {
93                 val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
94                 val image = ImageImpl("image-id").modify().setSone(sone).update()
95                 val parsed = Object()
96                 val shortened = Object()
97                 val rendered = "rendered description"
98                 whenever(parserFilter.format(any(), eq("some KSK@foo link"), any())).thenReturn(parsed)
99                 whenever(shortenFilter.format(any(), eq(parsed), any())).thenReturn(shortened)
100                 whenever(renderFilter.format(any(), eq(shortened), any())).thenReturn(rendered)
101                 addImage(image)
102                 addRequestParameter("image", "image-id")
103                 addRequestParameter("title", "some title")
104                 addRequestParameter("description", "some http://127.0.0.1:8888/KSK@foo link")
105                 addRequestHeader("Host", "127.0.0.1:8888")
106                 assertThatJsonIsSuccessful()
107                 assertThat(json["title"]?.asText(), equalTo("some title"))
108                 assertThat(json["description"]?.asText(), equalTo("some KSK@foo link"))
109                 assertThat(json["parsedDescription"]?.asText(), equalTo("rendered description"))
110                 verify(core).touchConfiguration()
111                 val parameterCaptor = argumentCaptor<MutableMap<String, Any?>>()
112                 verify(parserFilter).format(any(), any(), parameterCaptor.capture())
113                 assertThat(parameterCaptor.value["sone"], equalTo<Any>(sone))
114         }
115
116         @Test
117         fun `page can be created by dependency injection`() {
118                 assertThat(baseInjector.createChildInjector(
119                                 ParserFilter::class.isProvidedByMock(),
120                                 ShortenFilter::class.isProvidedByMock(),
121                                 RenderFilter::class.isProvidedByMock()
122                 ).getInstance<EditImageAjaxPage>(), notNullValue())
123         }
124
125 }