fix: Delete collection exports (#2595)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// @flow
|
||||
import Router from "koa-router";
|
||||
import fileOperationDeleter from "../../commands/fileOperationDeleter";
|
||||
import { NotFoundError, ValidationError } from "../../errors";
|
||||
import auth from "../../middlewares/authentication";
|
||||
import { FileOperation, Team } from "../../models";
|
||||
@@ -88,7 +89,7 @@ router.post("fileOperations.redirect", auth(), async (ctx) => {
|
||||
authorize(user, fileOp.type, team);
|
||||
|
||||
if (fileOp.state !== "complete") {
|
||||
throw new ValidationError("file operation is not complete yet");
|
||||
throw new ValidationError(`${fileOp.type} is not complete yet`);
|
||||
}
|
||||
|
||||
const accessUrl = await getSignedUrl(fileOp.key);
|
||||
@@ -96,4 +97,24 @@ router.post("fileOperations.redirect", auth(), async (ctx) => {
|
||||
ctx.redirect(accessUrl);
|
||||
});
|
||||
|
||||
router.post("fileOperations.delete", auth(), async (ctx) => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertUuid(id, "id is required");
|
||||
|
||||
const user = ctx.state.user;
|
||||
const team = await Team.findByPk(user.teamId);
|
||||
const fileOp = await FileOperation.findByPk(id);
|
||||
|
||||
if (!fileOp) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
authorize(user, fileOp.type, team);
|
||||
|
||||
await fileOperationDeleter(fileOp, user, ctx.request.ip);
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
};
|
||||
});
|
||||
export default router;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable flowtype/require-valid-file-annotation */
|
||||
import TestServer from "fetch-test-server";
|
||||
|
||||
import { Collection, User } from "../../models";
|
||||
import { Collection, User, Event, FileOperation } from "../../models";
|
||||
import webService from "../../services/web";
|
||||
import {
|
||||
buildAdmin,
|
||||
@@ -15,6 +15,14 @@ import { flushdb } from "../../test/support";
|
||||
const app = webService();
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
jest.mock("aws-sdk", () => {
|
||||
const mS3 = { deleteObject: jest.fn().mockReturnThis(), promise: jest.fn() };
|
||||
return {
|
||||
S3: jest.fn(() => mS3),
|
||||
Endpoint: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
afterAll(() => server.close());
|
||||
|
||||
@@ -234,7 +242,7 @@ describe("#fileOperations.redirect", () => {
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(400);
|
||||
expect(body.message).toEqual("file operation is not complete yet");
|
||||
expect(body.message).toEqual("export is not complete yet");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -281,3 +289,27 @@ describe("#fileOperations.info", () => {
|
||||
expect(res.status).toBe(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#fileOperations.delete", () => {
|
||||
it("should delete file operation", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const exportData = await buildFileOperation({
|
||||
type: "export",
|
||||
teamId: team.id,
|
||||
userId: admin.id,
|
||||
state: "complete",
|
||||
});
|
||||
|
||||
const deleteResponse = await server.post("/api/fileOperations.delete", {
|
||||
body: {
|
||||
token: admin.getJwtToken(),
|
||||
id: exportData.id,
|
||||
},
|
||||
});
|
||||
|
||||
expect(deleteResponse.status).toBe(200);
|
||||
expect(await Event.count()).toBe(1);
|
||||
expect(await FileOperation.count()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user