2020-12-18 05:19:31 +00:00
|
|
|
// @flow
|
|
|
|
import path from "path";
|
|
|
|
import File from "formidable/lib/file";
|
2020-12-28 23:30:01 +00:00
|
|
|
import { Attachment, Document, Collection } from "../models";
|
2020-12-18 05:19:31 +00:00
|
|
|
import { buildUser } from "../test/factories";
|
|
|
|
import { flushdb } from "../test/support";
|
2020-12-29 02:51:12 +00:00
|
|
|
import collectionImporter from "./collectionImporter";
|
2020-12-18 05:19:31 +00:00
|
|
|
|
|
|
|
jest.mock("../utils/s3");
|
|
|
|
|
|
|
|
beforeEach(() => flushdb());
|
|
|
|
|
2020-12-29 02:51:12 +00:00
|
|
|
describe("collectionImporter", () => {
|
2020-12-18 05:19:31 +00:00
|
|
|
const ip = "127.0.0.1";
|
|
|
|
|
|
|
|
it("should import documents in outline format", async () => {
|
|
|
|
const user = await buildUser();
|
|
|
|
const name = "outline.zip";
|
|
|
|
const file = new File({
|
|
|
|
name,
|
|
|
|
type: "application/zip",
|
|
|
|
path: path.resolve(__dirname, "..", "test", "fixtures", name),
|
|
|
|
});
|
|
|
|
|
2020-12-29 02:51:12 +00:00
|
|
|
const response = await collectionImporter({
|
2020-12-18 05:19:31 +00:00
|
|
|
type: "outline",
|
|
|
|
user,
|
|
|
|
file,
|
|
|
|
ip,
|
|
|
|
});
|
2020-12-24 18:18:53 +00:00
|
|
|
|
2020-12-27 07:12:22 +00:00
|
|
|
expect(response.collections.length).toEqual(1);
|
|
|
|
expect(response.documents.length).toEqual(8);
|
|
|
|
expect(response.attachments.length).toEqual(6);
|
2020-12-28 23:30:01 +00:00
|
|
|
|
|
|
|
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 {
|
2020-12-29 02:51:12 +00:00
|
|
|
await collectionImporter({
|
2020-12-28 23:30:01 +00:00
|
|
|
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 {
|
2020-12-29 02:51:12 +00:00
|
|
|
await collectionImporter({
|
2020-12-28 23:30:01 +00:00
|
|
|
type: "outline",
|
|
|
|
user,
|
|
|
|
file,
|
|
|
|
ip,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error && error.message).toBe(
|
|
|
|
"Uploaded file does not contain importable documents"
|
|
|
|
);
|
2020-12-18 05:19:31 +00:00
|
|
|
});
|
|
|
|
});
|