Add unit test for delete image page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Nov 2016 06:16:07 +0000 (07:16 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Nov 2016 06:16:07 +0000 (07:16 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/DeleteImagePageTest.kt [new file with mode: 0644]

index 546f001..5a1d589 100644 (file)
@@ -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.<Sone>absent());
                when(core.getPost(anyString())).thenReturn(Optional.<Post>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 (file)
index 0000000..6ba4d03
--- /dev/null
@@ -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<Image>()
+       private val sone = mock<Sone>()
+
+       @Before
+       fun setupImage() {
+               val album = mock<Album>()
+               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<Any>(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
+               }
+       }
+
+}