1 package net.pterodactylus.sone.web.ajax
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
20 import org.mockito.ArgumentMatchers.any
21 import org.mockito.ArgumentMatchers.eq
22 import org.mockito.Mockito.verify
25 * Unit test for [EditImageAjaxPage].
27 class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") {
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)
35 fun `request without image results in invalid-image-id`() {
36 assertThatJsonFailed("invalid-image-id")
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")
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())
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()
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 {
71 it.addImage(ImageImpl("swapped").modify().setSone(sone).update())
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()
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()
87 addRequestParameter("image", "image-id")
88 assertThatJsonFailed("invalid-image-title")
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()
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)
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))
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())