From: David ‘Bombe’ Roden Date: Sun, 22 Jan 2017 02:01:59 +0000 (+0100) Subject: Expand test for upload image page X-Git-Tag: 0.9.7^2~321 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c2c776f038ec312447ab64504efa88b1f3533b9a;p=Sone.git Expand test for upload image page --- diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index a6f7aa3..d8b9462 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -12,6 +12,7 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.URI; @@ -47,11 +48,14 @@ import net.pterodactylus.util.web.Response; import freenet.clients.http.ToadletContext; import freenet.l10n.BaseL10n; +import freenet.support.SimpleReadOnlyArrayBucket; +import freenet.support.api.Bucket; import freenet.support.api.HTTPRequest; +import freenet.support.api.HTTPUploadedFile; +import freenet.support.io.NullBucket; import com.google.common.base.Optional; import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.HashMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Multimap; import com.google.common.eventbus.EventBus; @@ -85,6 +89,9 @@ public abstract class WebPageTest { protected final HTTPRequest httpRequest = mock(HTTPRequest.class); protected final Multimap requestParameters = ArrayListMultimap.create(); protected final Map requestHeaders = new HashMap<>(); + private final Map uploadedFilesNames = new HashMap<>(); + private final Map uploadedFilesContentTypes = new HashMap<>(); + private final Map uploadedFilesSources = new HashMap<>(); protected final FreenetRequest freenetRequest = mock(FreenetRequest.class); private final PipedOutputStream responseOutputStream = new PipedOutputStream(); private final PipedInputStream responseInputStream; @@ -168,6 +175,36 @@ public abstract class WebPageTest { return requestHeaders.get(invocation.getArgument(0).toLowerCase()); } }); + when(httpRequest.getUploadedFile(anyString())).thenAnswer(new Answer() { + @Override + public HTTPUploadedFile answer(InvocationOnMock invocation) throws Throwable { + final String name = invocation.getArgument(0); + if (!uploadedFilesSources.containsKey(name)) { + return null; + } + return new HTTPUploadedFile() { + @Override + public String getContentType() { + return uploadedFilesContentTypes.get(name); + } + + @Override + public Bucket getData() { + try (InputStream inputStream = getClass().getResourceAsStream(uploadedFilesSources.get(name))) { + byte[] bytes = ByteStreams.toByteArray(inputStream); + return new SimpleReadOnlyArrayBucket(bytes, 0, bytes.length); + } catch (IOException ioe1) { + return new NullBucket(); + } + } + + @Override + public String getFilename() { + return uploadedFilesNames.get(name); + } + }; + } + }); } @Before @@ -204,6 +241,16 @@ public abstract class WebPageTest { } @Before + public void setupL10n() { + when(l10n.getString(anyString())).thenAnswer(new Answer() { + @Override + public String answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArgument(0); + } + }); + } + + @Before public final void setupIdentityManager() { when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities); } @@ -285,6 +332,12 @@ public abstract class WebPageTest { when(core.getTemporaryImage(eq(imageId))).thenReturn(temporaryImage); } + protected void addUploadedFile(@Nonnull String name, @Nonnull String filename, @Nonnull String contentType, @Nonnull String resource) { + uploadedFilesNames.put(name, filename); + uploadedFilesContentTypes.put(name, contentType); + uploadedFilesSources.put(name, resource); + } + protected byte[] getResponseBytes() throws IOException { response.getContent().close(); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/UploadImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/UploadImagePageTest.kt index f63219f..0eb34d6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/UploadImagePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/UploadImagePageTest.kt @@ -1,10 +1,20 @@ 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.data.TemporaryImage import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.mockBuilder import net.pterodactylus.sone.test.whenever -import net.pterodactylus.util.web.Method +import net.pterodactylus.util.web.Method.POST +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo import org.junit.Test +import org.mockito.Mockito.any +import org.mockito.Mockito.eq +import org.mockito.Mockito.never +import org.mockito.Mockito.verify /** * Unit test for [UploadImagePage]. @@ -12,18 +22,78 @@ import org.junit.Test class UploadImagePageTest : WebPageTest() { private val parentAlbum = mock().apply { + whenever(id).thenReturn("parent-id") whenever(sone).thenReturn(currentSone) } override fun getPage() = UploadImagePage(template, webInterface) @Test + fun `get request does not redirect or upload anything`() { + page.handleRequest(freenetRequest, templateContext) + verify(core, never()).createTemporaryImage(any(), any()) + verify(core, never()).createImage(any(), any(), any()) + } + + @Test + fun `post request without parent results in no permission error page`() { + request("", POST) + verifyRedirect("noPermission.html") + } + + @Test + fun `post request with parent that is not the current sone results in no permission error page`() { + request("", POST) + addHttpRequestParameter("parent", "parent-id") + whenever(parentAlbum.sone).thenReturn(mock()) + addAlbum("parent-id", parentAlbum) + verifyRedirect("noPermission.html") + } + + @Test fun `post request with empty name redirects to error page`() { - request("", Method.POST) + request("", POST) addAlbum("parent-id", parentAlbum) addHttpRequestParameter("parent", "parent-id") addHttpRequestParameter("title", " ") verifyRedirect("emptyImageTitle.html") } + @Test + fun `uploading an invalid image results in no redirect and message set in template context`() { + request("", POST) + addAlbum("parent-id", parentAlbum) + addHttpRequestParameter("parent", "parent-id") + addHttpRequestParameter("title", "title") + addUploadedFile("image", "image.png", "image/png", "no-image.png") + page.handleRequest(freenetRequest, templateContext) + verify(core, never()).createTemporaryImage(any(), any()) + assertThat(templateContext["messages"] as String?, equalTo("Page.UploadImage.Error.InvalidImage")) + } + + @Test + fun `uploading a valid image uploads image and redirects to album browser`() { + request("", POST) + addAlbum("parent-id", parentAlbum) + addHttpRequestParameter("parent", "parent-id") + addHttpRequestParameter("title", "Title") + addHttpRequestParameter("description", "Description") + addUploadedFile("image", "image.png", "image/png", "image.png") + val temporaryImage = TemporaryImage("temp-image") + val imageModifier = mockBuilder() + val image = mock().apply { + whenever(modify()).thenReturn(imageModifier) + } + whenever(core.createTemporaryImage(eq("image/png"), any())).thenReturn(temporaryImage) + whenever(core.createImage(currentSone, parentAlbum, temporaryImage)).thenReturn(image) + verifyRedirect("imageBrowser.html?album=parent-id") { + verify(image).modify() + verify(imageModifier).setWidth(2) + verify(imageModifier).setHeight(1) + verify(imageModifier).setTitle("Title") + verify(imageModifier).setDescription("Description") + verify(imageModifier).update() + } + } + } diff --git a/src/test/resources/net/pterodactylus/sone/web/image.png b/src/test/resources/net/pterodactylus/sone/web/image.png new file mode 100644 index 0000000..6daa837 Binary files /dev/null and b/src/test/resources/net/pterodactylus/sone/web/image.png differ diff --git a/src/test/resources/net/pterodactylus/sone/web/no-image.png b/src/test/resources/net/pterodactylus/sone/web/no-image.png new file mode 100644 index 0000000..e69de29