fix: documentUpdater called without change can result in lastModifiedById being updated
This commit is contained in:
54
server/commands/documentUpdater.test.ts
Normal file
54
server/commands/documentUpdater.test.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { sequelize } from "@server/database/sequelize";
|
||||||
|
import { Event } from "@server/models";
|
||||||
|
import { buildDocument, buildUser } from "@server/test/factories";
|
||||||
|
import { flushdb } from "@server/test/support";
|
||||||
|
import documentUpdater from "./documentUpdater";
|
||||||
|
|
||||||
|
beforeEach(() => flushdb());
|
||||||
|
|
||||||
|
describe("documentUpdater", () => {
|
||||||
|
const ip = "127.0.0.1";
|
||||||
|
|
||||||
|
it("should change lastModifiedById", async () => {
|
||||||
|
const user = await buildUser();
|
||||||
|
let document = await buildDocument({
|
||||||
|
teamId: user.teamId,
|
||||||
|
});
|
||||||
|
|
||||||
|
document = await sequelize.transaction(async (transaction) =>
|
||||||
|
documentUpdater({
|
||||||
|
text: "Changed",
|
||||||
|
document,
|
||||||
|
user,
|
||||||
|
ip,
|
||||||
|
transaction,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const event = await Event.findOne();
|
||||||
|
expect(document.lastModifiedById).toEqual(user.id);
|
||||||
|
expect(event!.name).toEqual("documents.update");
|
||||||
|
expect(event!.documentId).toEqual(document.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not change lastModifiedById or generate event if nothing changed", async () => {
|
||||||
|
const user = await buildUser();
|
||||||
|
let document = await buildDocument({
|
||||||
|
teamId: user.teamId,
|
||||||
|
});
|
||||||
|
|
||||||
|
document = await sequelize.transaction(async (transaction) =>
|
||||||
|
documentUpdater({
|
||||||
|
title: document.title,
|
||||||
|
document,
|
||||||
|
user,
|
||||||
|
ip,
|
||||||
|
transaction,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const event = await Event.findOne();
|
||||||
|
expect(document.lastModifiedById).not.toEqual(user.id);
|
||||||
|
expect(event).toEqual(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -68,16 +68,12 @@ export default async function documentUpdater({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.lastModifiedById = user.id;
|
|
||||||
const changed = document.changed();
|
const changed = document.changed();
|
||||||
|
|
||||||
if (publish) {
|
if (publish) {
|
||||||
|
document.lastModifiedById = user.id;
|
||||||
await document.publish(user.id, { transaction });
|
await document.publish(user.id, { transaction });
|
||||||
} else {
|
|
||||||
await document.save({ transaction });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (publish) {
|
|
||||||
await Event.create(
|
await Event.create(
|
||||||
{
|
{
|
||||||
name: "documents.publish",
|
name: "documents.publish",
|
||||||
@@ -93,6 +89,9 @@ export default async function documentUpdater({
|
|||||||
{ transaction }
|
{ transaction }
|
||||||
);
|
);
|
||||||
} else if (changed) {
|
} else if (changed) {
|
||||||
|
document.lastModifiedById = user.id;
|
||||||
|
await document.save({ transaction });
|
||||||
|
|
||||||
await Event.create(
|
await Event.create(
|
||||||
{
|
{
|
||||||
name: "documents.update",
|
name: "documents.update",
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ describe("revisionCreator", () => {
|
|||||||
const event = await Event.findOne();
|
const event = await Event.findOne();
|
||||||
expect(revision.documentId).toEqual(document.id);
|
expect(revision.documentId).toEqual(document.id);
|
||||||
expect(revision.userId).toEqual(user.id);
|
expect(revision.userId).toEqual(user.id);
|
||||||
|
expect(revision.createdAt).toEqual(document.updatedAt);
|
||||||
expect(event!.name).toEqual("revisions.create");
|
expect(event!.name).toEqual("revisions.create");
|
||||||
expect(event!.modelId).toEqual(revision.id);
|
expect(event!.modelId).toEqual(revision.id);
|
||||||
expect(event!.createdAt).toEqual(document.updatedAt);
|
expect(event!.createdAt).toEqual(document.updatedAt);
|
||||||
|
|||||||
Reference in New Issue
Block a user