import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.URI;
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;
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;
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
}
@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);
}
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()) {
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].
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()
+ }
+ }
+
}