fix: Improve validation on documents.import endpoint

This commit is contained in:
Tom Moor 2020-11-26 20:29:45 -08:00
parent 56d5f048f9
commit 8bddc1b338
2 changed files with 17 additions and 0 deletions

View File

@ -815,7 +815,12 @@ router.post("documents.unstar", auth(), async (ctx) => {
router.post("documents.create", auth(), createDocumentFromContext);
router.post("documents.import", auth(), async (ctx) => {
if (!ctx.is("multipart/form-data")) {
throw new InvalidRequestError("Request type must be multipart/form-data");
}
const file: any = Object.values(ctx.request.files)[0];
ctx.assertPresent(file, "file is required");
const user = ctx.state.user;
authorize(user, "create", Document);

View File

@ -1524,6 +1524,18 @@ describe("#documents.unstar", () => {
});
});
describe("#documents.import", () => {
it("should error if no file is passed", async () => {
const user = await buildUser();
const res = await server.post("/api/documents.import", {
body: {
token: user.getJwtToken(),
},
});
expect(res.status).toEqual(400);
});
});
describe("#documents.create", () => {
it("should create as a new document", async () => {
const { user, collection } = await seed();