test: Add additional tests and input validation

This commit is contained in:
Tom Moor 2020-12-28 15:30:01 -08:00
parent b932457fd3
commit 2787e56de3
5 changed files with 68 additions and 2 deletions

View File

@ -8,7 +8,7 @@ import invariant from "invariant";
import { values, keys } from "lodash";
import uuid from "uuid";
import { parseOutlineExport } from "../../shared/utils/zip";
import { InvalidRequestError } from "../errors";
import { FileImportError } from "../errors";
import { Attachment, Document, Collection, User } from "../models";
import attachmentCreator from "./attachmentCreator";
import documentCreator from "./documentCreator";
@ -34,7 +34,13 @@ export default async function documentBatchImporter({
try {
items = await await parseOutlineExport(zipData);
} catch (err) {
throw new InvalidRequestError(err.message);
throw new FileImportError(err.message);
}
if (!items.filter((item) => item.type === "document").length) {
throw new FileImportError(
"Uploaded file does not contain importable documents"
);
}
// store progress and pointers
@ -96,6 +102,8 @@ export default async function documentBatchImporter({
ip,
});
await fs.promises.unlink(tmpFilePath);
// must be a nested document, find and reference the parent document
let parentDocumentId;
if (item.depth > 1) {

View File

@ -1,6 +1,7 @@
// @flow
import path from "path";
import File from "formidable/lib/file";
import { Attachment, Document, Collection } from "../models";
import { buildUser } from "../test/factories";
import { flushdb } from "../test/support";
import documentBatchImporter from "./documentBatchImporter";
@ -31,5 +32,59 @@ describe("documentBatchImporter", () => {
expect(response.collections.length).toEqual(1);
expect(response.documents.length).toEqual(8);
expect(response.attachments.length).toEqual(6);
expect(await Collection.count()).toEqual(1);
expect(await Document.count()).toEqual(8);
expect(await Attachment.count()).toEqual(6);
});
it("should throw an error with corrupt zip", async () => {
const user = await buildUser();
const name = "corrupt.zip";
const file = new File({
name,
type: "application/zip",
path: path.resolve(__dirname, "..", "test", "fixtures", name),
});
let error;
try {
await documentBatchImporter({
type: "outline",
user,
file,
ip,
});
} catch (err) {
error = err;
}
expect(error && error.message).toBeTruthy();
});
it("should throw an error with empty zip", async () => {
const user = await buildUser();
const name = "empty.zip";
const file = new File({
name,
type: "application/zip",
path: path.resolve(__dirname, "..", "test", "fixtures", name),
});
let error;
try {
await documentBatchImporter({
type: "outline",
user,
file,
ip,
});
} catch (err) {
error = err;
}
expect(error && error.message).toBe(
"Uploaded file does not contain importable documents"
);
});
});

1
server/test/fixtures/corrupt.zip vendored Normal file
View File

@ -0,0 +1 @@
CORRUPT

BIN
server/test/fixtures/empty.zip vendored Normal file

Binary file not shown.

View File

@ -279,6 +279,8 @@
"Export in progress…": "Export in progress…",
"Import": "Import",
"It is possible to import a zip file of folders and Markdown files previously exported from an Outline instance. Support will soon be added for importing from other services.": "It is possible to import a zip file of folders and Markdown files previously exported from an Outline instance. Support will soon be added for importing from other services.",
"Sorry, the file <1>{{fileName}}</1> is missing valid collections or documents.": "Sorry, the file <1>{{fileName}}</1> is missing valid collections or documents.",
"<0>{{fileName}}</0> looks good, the following collections and their documents will be imported:": "<0>{{fileName}}</0> looks good, the following collections and their documents will be imported:",
"Importing": "Importing",
"Confirm & Import": "Confirm & Import",
"Choose File…": "Choose File…",