fix: Two API endpoints where requesting a permenantly deleted document results in server error
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
import Router from "koa-router";
|
||||
import Sequelize from "sequelize";
|
||||
import documentMover from "../commands/documentMover";
|
||||
import { InvalidRequestError } from "../errors";
|
||||
import { NotFoundError, InvalidRequestError } from "../errors";
|
||||
import auth from "../middlewares/authentication";
|
||||
import {
|
||||
Backlink,
|
||||
@ -411,6 +411,10 @@ async function loadDocument({ id, shareId, user }) {
|
||||
userId: user ? user.id : undefined,
|
||||
paranoid: false,
|
||||
});
|
||||
if (!document) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
if (document.deletedAt) {
|
||||
authorize(user, "restore", document);
|
||||
} else {
|
||||
@ -456,6 +460,9 @@ router.post("documents.restore", auth(), async (ctx) => {
|
||||
userId: user.id,
|
||||
paranoid: false,
|
||||
});
|
||||
if (!document) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
if (collectionId) {
|
||||
ctx.assertUuid(collectionId, "collectionId must be a uuid");
|
||||
|
@ -185,6 +185,15 @@ describe("#documents.info", () => {
|
||||
expect(body.data.id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it("should not error if document doesn't exist", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/documents.info", {
|
||||
body: { token: user.getJwtToken(), id: "test" },
|
||||
});
|
||||
expect(res.status).toEqual(404);
|
||||
});
|
||||
|
||||
it("should require authorization without token", async () => {
|
||||
const { document } = await seed();
|
||||
const res = await server.post("/api/documents.info", {
|
||||
@ -1309,6 +1318,15 @@ describe("#documents.restore", () => {
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
|
||||
it("should not error if document doesn't exist", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/documents.restore", {
|
||||
body: { token: user.getJwtToken(), id: "test" },
|
||||
});
|
||||
expect(res.status).toEqual(404);
|
||||
});
|
||||
|
||||
it("should require authentication", async () => {
|
||||
const res = await server.post("/api/documents.restore");
|
||||
const body = await res.json();
|
||||
|
Reference in New Issue
Block a user