Expand test for upload image page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2017 02:01:59 +0000 (03:01 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2017 02:01:59 +0000 (03:01 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/UploadImagePageTest.kt
src/test/resources/net/pterodactylus/sone/web/image.png [new file with mode: 0644]
src/test/resources/net/pterodactylus/sone/web/no-image.png [new file with mode: 0644]

index a6f7aa3..d8b9462 100644 (file)
@@ -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<String, String> requestParameters = ArrayListMultimap.create();
        protected final Map<String, String> requestHeaders = new HashMap<>();
+       private final Map<String, String> uploadedFilesNames = new HashMap<>();
+       private final Map<String, String> uploadedFilesContentTypes = new HashMap<>();
+       private final Map<String, String> 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.<String>getArgument(0).toLowerCase());
                        }
                });
+               when(httpRequest.getUploadedFile(anyString())).thenAnswer(new Answer<HTTPUploadedFile>() {
+                       @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<String>() {
+                       @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()) {
index f63219f..0eb34d6 100644 (file)
@@ -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<Album>().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<Sone>())
+               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<String>("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<Image.Modifier>()
+               val image = mock<Image>().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 (file)
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 (file)
index 0000000..e69de29