From 5c8ad31fa8d083ca1c87f292e00bc7b21fbd85a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 22 Nov 2016 21:08:03 +0100 Subject: [PATCH] Add unit test for delete album page --- .../net/pterodactylus/sone/web/WebPageTest.java | 11 +++ .../pterodactylus/sone/web/DeleteAlbumPageTest.kt | 102 +++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/DeleteAlbumPageTest.kt diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 9dff571..546f001 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -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.absent()); when(core.getPost(anyString())).thenReturn(Optional.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 index 0000000..fc69797 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/DeleteAlbumPageTest.kt @@ -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() + private val album = mock() + private val parentAlbum = mock() + + @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() + addAlbum("album-id", album) + addHttpRequestParameter("album", "album-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["album"], equalTo(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()) + addAlbum("album-id", album) + addHttpRequestParameter("album", "album-id") + expectedException.expect(redirectsTo("imageBrowser.html?album=parent-id")) + page.handleRequest(freenetRequest, templateContext) + } + +} -- 2.7.4