Add unit test for delete album page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 22 Nov 2016 20:08:03 +0000 (21:08 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Nov 2016 05:55:22 +0000 (06:55 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/DeleteAlbumPageTest.kt [new file with mode: 0644]

index 9dff571..546f001 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Set;
 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.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
@@ -74,6 +75,8 @@ public abstract class WebPageTest {
                                return "";
                        }
                });
+               when(httpRequest.getParam(anyString())).thenReturn("");
+               when(httpRequest.getParam(anyString(), anyString())).thenReturn("");
        }
 
        @Before
@@ -85,6 +88,7 @@ public abstract class WebPageTest {
                when(core.getLocalSones()).thenReturn(localSones);
                when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
                when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
+               when(core.getAlbum(anyString())).thenReturn(null);
        }
 
        @Before
@@ -126,6 +130,9 @@ public abstract class WebPageTest {
                                return value.substring(0, Math.min(maxLength, value.length()));
                        }
                });
+               when(httpRequest.getParam(eq(name))).thenReturn(value);
+               when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
+               when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
        }
 
        protected void addPost(String postId, Post post) {
@@ -145,4 +152,8 @@ public abstract class WebPageTest {
                ownIdentities.add(ownIdentity);
        }
 
+       protected void addAlbum(String albumId, Album album) {
+               when(core.getAlbum(eq(albumId))).thenReturn(album);
+       }
+
 }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/DeleteAlbumPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/DeleteAlbumPageTest.kt
new file mode 100644 (file)
index 0000000..fc69797
--- /dev/null
@@ -0,0 +1,102 @@
+package net.pterodactylus.sone.web
+
+import net.pterodactylus.sone.data.Album
+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.ArgumentMatchers.anyString
+
+/**
+ * Unit test for [DeleteAlbumPage].
+ */
+class DeleteAlbumPageTest : WebPageTest() {
+
+       private val page = DeleteAlbumPage(template, webInterface)
+
+       private val sone = mock<Sone>()
+       private val album = mock<Album>()
+       private val parentAlbum = mock<Album>()
+
+       @Before
+       fun setupAlbums() {
+               whenever(sone.id).thenReturn("sone-id")
+               whenever(sone.isLocal).thenReturn(true)
+               whenever(parentAlbum.id).thenReturn("parent-id")
+               whenever(album.id).thenReturn("album-id")
+               whenever(album.sone).thenReturn(sone)
+               whenever(album.parent).thenReturn(parentAlbum)
+               whenever(sone.rootAlbum).thenReturn(parentAlbum)
+       }
+
+       @Test
+       fun `get request with invalid album ID results in redirect to invalid page`() {
+               request("", GET)
+               whenever(core.getAlbum(anyString())).thenReturn(null)
+               expectedException.expect(redirectsTo("invalid.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `get request with valid album ID sets album in template context`() {
+               request("", GET)
+               val album = mock<Album>()
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["album"], equalTo<Any>(album))
+       }
+
+       @Test
+       fun `post request redirects to invalid page if album is invalid`() {
+               request("", POST)
+               expectedException.expect(redirectsTo("invalid.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request redirects to no permissions page if album is not local`() {
+               request("", POST)
+               whenever(sone.isLocal).thenReturn(false)
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               expectedException.expect(redirectsTo("noPermission.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `post request with abort delete parameter set redirects to album browser`() {
+               request("", POST)
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               addHttpRequestParameter("abortDelete", "true")
+               expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `album is deleted and page redirects to sone if parent album is root album`() {
+               request("", POST)
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               expectedException.expect(redirectsTo("imageBrowser.html?sone=sone-id"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+       @Test
+       fun `album is deleted and page redirects to album if parent album is not root album`() {
+               request("", POST)
+               whenever(sone.rootAlbum).thenReturn(mock<Album>())
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               expectedException.expect(redirectsTo("imageBrowser.html?album=parent-id"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+}