* perf: reduce memory usage upon running server tests * perf: plug leaks in server/routes * perf: plug leaks in server/scripts * perf: plug leaks in server/policies * perf: plug leaks in server/models * perf: plug leaks in server/middlewares * perf: plug leaks in server/commands * fix: missing await on db.flush * perf: plug leaks in server/queues * chore: remove unused legacy funcs * fix: await on db.flush * perf: await on GC to run in between tests * fix: remove db refs * fix: revert embeds * perf: plug leaks in shared/i18n
119 lines
3.6 KiB
TypeScript
119 lines
3.6 KiB
TypeScript
import { subDays } from "date-fns";
|
|
import { Attachment, Document } from "@server/models";
|
|
import { buildAttachment, buildDocument } from "@server/test/factories";
|
|
import { getTestDatabase } from "@server/test/support";
|
|
import documentPermanentDeleter from "./documentPermanentDeleter";
|
|
|
|
const db = getTestDatabase();
|
|
|
|
afterAll(db.disconnect);
|
|
|
|
beforeEach(db.flush);
|
|
|
|
describe("documentPermanentDeleter", () => {
|
|
it("should destroy documents", async () => {
|
|
const document = await buildDocument({
|
|
publishedAt: subDays(new Date(), 90),
|
|
deletedAt: new Date(),
|
|
});
|
|
const countDeletedDoc = await documentPermanentDeleter([document]);
|
|
expect(countDeletedDoc).toEqual(1);
|
|
expect(
|
|
await Document.unscoped().count({
|
|
paranoid: false,
|
|
})
|
|
).toEqual(0);
|
|
});
|
|
|
|
it("should error when trying to destroy undeleted documents", async () => {
|
|
const document = await buildDocument({
|
|
publishedAt: new Date(),
|
|
});
|
|
let error;
|
|
|
|
try {
|
|
await documentPermanentDeleter([document]);
|
|
} catch (err) {
|
|
error = err.message;
|
|
}
|
|
|
|
expect(error).toEqual(
|
|
`Cannot permanently delete ${document.id} document. Please delete it and try again.`
|
|
);
|
|
});
|
|
|
|
it("should destroy attachments no longer referenced", async () => {
|
|
const document = await buildDocument({
|
|
publishedAt: subDays(new Date(), 90),
|
|
deletedAt: new Date(),
|
|
});
|
|
const attachment = await buildAttachment({
|
|
teamId: document.teamId,
|
|
documentId: document.id,
|
|
});
|
|
document.text = ``;
|
|
await document.save();
|
|
const countDeletedDoc = await documentPermanentDeleter([document]);
|
|
expect(countDeletedDoc).toEqual(1);
|
|
expect(await Attachment.count()).toEqual(0);
|
|
expect(
|
|
await Document.unscoped().count({
|
|
paranoid: false,
|
|
})
|
|
).toEqual(0);
|
|
});
|
|
|
|
it("should handle unknown attachment ids", async () => {
|
|
const document = await buildDocument({
|
|
publishedAt: subDays(new Date(), 90),
|
|
deletedAt: new Date(),
|
|
});
|
|
const attachment = await buildAttachment({
|
|
teamId: document.teamId,
|
|
documentId: document.id,
|
|
});
|
|
document.text = ``;
|
|
await document.save();
|
|
// remove attachment so it no longer exists in the database, this is also
|
|
// representative of a corrupt attachment id in the doc or the regex returning
|
|
// an incorrect string
|
|
await attachment.destroy({
|
|
force: true,
|
|
});
|
|
const countDeletedDoc = await documentPermanentDeleter([document]);
|
|
expect(countDeletedDoc).toEqual(1);
|
|
expect(await Attachment.count()).toEqual(0);
|
|
expect(
|
|
await Document.unscoped().count({
|
|
paranoid: false,
|
|
})
|
|
).toEqual(0);
|
|
});
|
|
|
|
it("should not destroy attachments referenced in other documents", async () => {
|
|
const document1 = await buildDocument();
|
|
const document = await buildDocument({
|
|
teamId: document1.teamId,
|
|
publishedAt: subDays(new Date(), 90),
|
|
deletedAt: subDays(new Date(), 60),
|
|
});
|
|
const attachment = await buildAttachment({
|
|
teamId: document1.teamId,
|
|
documentId: document.id,
|
|
});
|
|
document1.text = ``;
|
|
await document1.save();
|
|
document.text = ``;
|
|
await document.save();
|
|
expect(await Attachment.count()).toEqual(1);
|
|
const countDeletedDoc = await documentPermanentDeleter([document]);
|
|
expect(countDeletedDoc).toEqual(1);
|
|
expect(await Attachment.count()).toEqual(1);
|
|
expect(
|
|
await Document.unscoped().count({
|
|
paranoid: false,
|
|
})
|
|
).toEqual(1);
|
|
});
|
|
});
|