From 52f3202e3d96c1c2d5dc60ac1d5fa7e3b340a45f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 23 Nov 2016 07:16:07 +0100 Subject: [PATCH] Add unit test for delete image page --- .../net/pterodactylus/sone/web/WebPageTest.java | 8 ++ .../pterodactylus/sone/web/DeleteImagePageTest.kt | 88 ++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/DeleteImagePageTest.kt diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 546f001..5a1d589 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -19,6 +19,7 @@ import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.Preferences; import net.pterodactylus.sone.core.UpdateChecker; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; @@ -89,6 +90,8 @@ public abstract class WebPageTest { when(core.getSone(anyString())).thenReturn(Optional.absent()); when(core.getPost(anyString())).thenReturn(Optional.absent()); when(core.getAlbum(anyString())).thenReturn(null); + when(core.getImage(anyString())).thenReturn(null); + when(core.getImage(anyString(), anyBoolean())).thenReturn(null); } @Before @@ -156,4 +159,9 @@ public abstract class WebPageTest { when(core.getAlbum(eq(albumId))).thenReturn(album); } + protected void addImage(String imageId, Image image) { + when(core.getImage(eq(imageId))).thenReturn(image); + when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image); + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeleteImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeleteImagePageTest.kt new file mode 100644 index 0000000..6ba4d03 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/DeleteImagePageTest.kt @@ -0,0 +1,88 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.WebTestUtils.redirectsTo +import net.pterodactylus.util.web.Method.GET +import net.pterodactylus.util.web.Method.POST +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import org.mockito.Mockito.verify +import kotlin.test.fail + +/** + * Unit test for [DeleteImagePage]. + */ +class DeleteImagePageTest : WebPageTest() { + + private val page = DeleteImagePage(template, webInterface) + private val image = mock() + private val sone = mock() + + @Before + fun setupImage() { + val album = mock() + whenever(album.id).thenReturn("album-id") + whenever(image.id).thenReturn("image-id") + whenever(image.sone).thenReturn(sone) + whenever(image.album).thenReturn(album) + whenever(sone.isLocal).thenReturn(true) + } + + @Test + fun `get request with invalid image redirects to invalid page`() { + request("", GET) + expectedException.expect(redirectsTo("invalid.html")) + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `get request with image from non-local sone redirects to no permissions page`() { + request("", GET) + whenever(sone.isLocal).thenReturn(false) + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + expectedException.expect(redirectsTo("noPermission.html")) + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `get request with image from local sone sets image in template context`() { + request("", GET) + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + page.processTemplate(freenetRequest, templateContext) + assertThat(templateContext["image"], equalTo(image)) + } + + @Test + fun `post request with abort delete flag set redirects to image browser`() { + request("", POST) + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + addHttpRequestParameter("abortDelete", "true") + expectedException.expect(redirectsTo("imageBrowser.html?image=image-id")) + page.processTemplate(freenetRequest, templateContext) + } + + @Test + fun `post request deletes image and redirects to image browser`() { + request("", POST) + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + expectedException.expect(redirectsTo("imageBrowser.html?album=album-id")) + try { + page.processTemplate(freenetRequest, templateContext) + fail() + } catch (e: Exception) { + verify(webInterface.core).deleteImage(image) + throw e + } + } + +} -- 2.7.4