Files
outline/server/queues/processors/DocumentMovedProcessor.test.ts
38a3e651a7 fix: Rebuild of sourced permissions on document move (#11229)
* only recalculate permssions for moved document

* stop duplicating permissions

* add tests

* fix comment

* filter duplicates

* filter duplicates

* minor fixes

* fix child document permissions not being recalculated

* expand tests

* Update DocumentMovedProcessor.test.ts

* Update server/queues/processors/DocumentMovedProcessor.test.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* requested changes

* remove all sourced permissions before calculating new ones

* remove all sourced permissions before recalculating

* Add additional tests

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-05 08:22:03 -05:00

643 lines
20 KiB
TypeScript

import { UserMembership, GroupMembership } from "@server/models";
import {
buildDocument,
buildCollection,
buildUser,
buildTeam,
buildAdmin,
buildGroup,
} from "@server/test/factories";
import DocumentMovedProcessor from "./DocumentMovedProcessor";
const ip = "127.0.0.1";
describe("DocumentMovedProcessor", () => {
it("should add sourced permissions from the top document when a document is moved into a new parent", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const topDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
const sourceUserMembership = await UserMembership.create({
userId: user2.id,
documentId: topDocument.id,
createdById: user.id,
});
const sourceGroupMembership = await GroupMembership.create({
groupId: group.id,
documentId: topDocument.id,
createdById: user.id,
});
// remove sourced permissions from childDocument
await UserMembership.destroy({
where: { documentId: childDocument.id, userId: user2.id },
});
await GroupMembership.destroy({
where: { documentId: childDocument.id, groupId: group.id },
});
// trigger move event on childDocument
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// doc2 should have sourced permissions from topDocument
const memberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
const groupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(memberships.length).toBe(1);
expect(groupMemberships.length).toBe(1);
expect(memberships[0].sourceId).toBe(sourceUserMembership.id);
expect(groupMemberships[0].sourceId).toBe(sourceGroupMembership.id);
});
it("should not reapply sourced permissions to sibling documents when a document is moved into a new parent", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const topDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const doc1 = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
const doc2 = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
await UserMembership.create({
userId: user2.id,
documentId: topDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group.id,
documentId: topDocument.id,
createdById: user.id,
});
// Remove source permissions from doc2
await UserMembership.destroy({
where: { documentId: doc2.id, userId: user2.id },
});
await GroupMembership.destroy({
where: { documentId: doc2.id, groupId: group.id },
});
// trigger move event on doc1
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: doc1.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// sourced permission from user2 remains removed
const userMemberships = await UserMembership.findAll({
where: { documentId: doc2.id, userId: user2.id },
});
const groupMemberships = await GroupMembership.findAll({
where: { documentId: doc2.id, groupId: group.id },
});
expect(userMemberships.length).toBe(0);
expect(groupMemberships.length).toBe(0);
});
it("should not create duplicate sourced permissions when a document is moved", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const topDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const doc1 = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
const doc2 = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
await UserMembership.create({
userId: user2.id,
documentId: topDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group.id,
documentId: topDocument.id,
createdById: user.id,
});
// Trigger move event on doc1
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: doc1.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// there should be no duplicate permissions are created for doc1 and doc2
const doc1UserMemberships = await UserMembership.findAll({
where: { documentId: doc1.id, userId: user2.id },
});
const doc2UserMemberships = await UserMembership.findAll({
where: { documentId: doc2.id, userId: user2.id },
});
const doc1GroupMemberships = await GroupMembership.findAll({
where: { documentId: doc1.id, groupId: group.id },
});
const doc2GroupMemberships = await GroupMembership.findAll({
where: { documentId: doc2.id, groupId: group.id },
});
expect(doc1UserMemberships.length).toBe(1);
expect(doc2UserMemberships.length).toBe(1);
expect(doc1GroupMemberships.length).toBe(1);
expect(doc2GroupMemberships.length).toBe(1);
});
it("should propagate sourced permissions to direct child documents of the moved document", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const topDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
const sourceUserMembership = await UserMembership.create({
userId: user2.id,
documentId: topDocument.id,
createdById: user.id,
});
const sourceGroupMembership = await GroupMembership.create({
groupId: group.id,
documentId: topDocument.id,
createdById: user.id,
});
// Remove sourced permissions from childDocument
await UserMembership.destroy({
where: { documentId: childDocument.id, userId: user2.id },
});
await GroupMembership.destroy({
where: { documentId: childDocument.id, groupId: group.id },
});
// Trigger move event on childDocument
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// Verify permissions for childDocument
const childUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
const childGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(childUserMemberships.length).toBe(1);
expect(childGroupMemberships.length).toBe(1);
expect(childUserMemberships[0].sourceId).toBe(sourceUserMembership.id);
expect(childGroupMemberships[0].sourceId).toBe(sourceGroupMembership.id);
});
it("should propagate sourced permissions to all deep child documents of the moved document", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const topDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: topDocument.id,
});
const grandChildDocument = await buildDocument({
teamId: team.id,
parentDocumentId: childDocument.id,
});
const greatGrandChildDocument = await buildDocument({
teamId: team.id,
parentDocumentId: grandChildDocument.id,
});
const sourceUserMembership = await UserMembership.create({
userId: user2.id,
documentId: topDocument.id,
createdById: user.id,
});
const sourceGroupMembership = await GroupMembership.create({
groupId: group.id,
documentId: topDocument.id,
createdById: user.id,
});
// Remove sourced permissions from grandChildDocument and greatGrandChildDocument
await UserMembership.destroy({
where: { documentId: grandChildDocument.id, userId: user2.id },
});
await GroupMembership.destroy({
where: { documentId: grandChildDocument.id, groupId: group.id },
});
await UserMembership.destroy({
where: { documentId: greatGrandChildDocument.id, userId: user2.id },
});
await GroupMembership.destroy({
where: { documentId: greatGrandChildDocument.id, groupId: group.id },
});
// Trigger move event on childDocument
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// Verify permissions for grandChildDocument
const grandChildUserMemberships = await UserMembership.findAll({
where: { documentId: grandChildDocument.id, userId: user2.id },
});
const grandChildGroupMemberships = await GroupMembership.findAll({
where: { documentId: grandChildDocument.id, groupId: group.id },
});
expect(grandChildUserMemberships.length).toBe(1);
expect(grandChildGroupMemberships.length).toBe(1);
expect(grandChildUserMemberships[0].sourceId).toBe(sourceUserMembership.id);
expect(grandChildGroupMemberships[0].sourceId).toBe(
sourceGroupMembership.id
);
// Verify permissions for greatGrandChildDocument
const greatGrandChildUserMemberships = await UserMembership.findAll({
where: { documentId: greatGrandChildDocument.id, userId: user2.id },
});
const greatGrandChildGroupMemberships = await GroupMembership.findAll({
where: { documentId: greatGrandChildDocument.id, groupId: group.id },
});
expect(greatGrandChildUserMemberships.length).toBe(1);
expect(greatGrandChildGroupMemberships.length).toBe(1);
expect(greatGrandChildUserMemberships[0].sourceId).toBe(
sourceUserMembership.id
);
expect(greatGrandChildGroupMemberships[0].sourceId).toBe(
sourceGroupMembership.id
);
});
it("should not carry over sourced permissions from previous parent", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const parentDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: parentDocument.id,
});
const newParentDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
// Add user and group to parent document
await UserMembership.create({
userId: user2.id,
documentId: parentDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group.id,
documentId: parentDocument.id,
createdById: user.id,
});
// Add different permissions to the new parent document
const user3 = await buildUser({ teamId: team.id });
const group2 = await buildGroup({ teamId: team.id });
await UserMembership.create({
userId: user3.id,
documentId: newParentDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group2.id,
documentId: newParentDocument.id,
createdById: user.id,
});
// Verify inherited permissions exist on child from the original parent
let childUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
let childGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(childUserMemberships.length).toBe(1);
expect(childGroupMemberships.length).toBe(1);
expect(childUserMemberships[0].sourceId).toBeTruthy();
expect(childGroupMemberships[0].sourceId).toBeTruthy();
// Move child to a new parent document
childDocument.parentDocumentId = newParentDocument.id;
await childDocument.save();
// Trigger move event
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// Verify inherited permissions from the original parent are removed
childUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
childGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(childUserMemberships.length).toBe(0);
expect(childGroupMemberships.length).toBe(0);
// Verify inherited permissions from the new parent are applied
const newChildUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user3.id },
});
const newChildGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group2.id },
});
expect(newChildUserMemberships.length).toBe(1);
expect(newChildGroupMemberships.length).toBe(1);
expect(newChildUserMemberships[0].sourceId).toBeTruthy();
expect(newChildGroupMemberships[0].sourceId).toBeTruthy();
});
it("should remove all sourced permissions when a document is moved to root level", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const parentDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: parentDocument.id,
});
await UserMembership.create({
userId: user2.id,
documentId: parentDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group.id,
documentId: parentDocument.id,
createdById: user.id,
});
// Verify sourced permissions exist on child
let childUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
let childGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(childUserMemberships.length).toBe(1);
expect(childGroupMemberships.length).toBe(1);
// Move child to root level (no parent)
childDocument.parentDocumentId = null;
await childDocument.save();
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// Sourced permissions should be removed
childUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
childGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(childUserMemberships.length).toBe(0);
expect(childGroupMemberships.length).toBe(0);
});
it("should preserve direct (non-sourced) permissions when a document is moved", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
const user2 = await buildUser({ teamId: team.id });
const user3 = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
const group2 = await buildGroup({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const parentDocument = await buildDocument({
collectionId: collection.id,
teamId: team.id,
});
const childDocument = await buildDocument({
teamId: team.id,
parentDocumentId: parentDocument.id,
});
// Add sourced permissions via parent
await UserMembership.create({
userId: user2.id,
documentId: parentDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group.id,
documentId: parentDocument.id,
createdById: user.id,
});
// Add direct permissions to the child document
await UserMembership.create({
userId: user3.id,
documentId: childDocument.id,
createdById: user.id,
});
await GroupMembership.create({
groupId: group2.id,
documentId: childDocument.id,
createdById: user.id,
});
// Move child to root level
childDocument.parentDocumentId = null;
await childDocument.save();
const processor = new DocumentMovedProcessor();
await processor.perform({
name: "documents.move",
documentId: childDocument.id,
collectionId: collection.id,
teamId: team.id,
actorId: user.id,
ip,
data: {
collectionIds: [],
documentIds: [],
},
});
// Sourced permissions should be removed
const sourcedUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user2.id },
});
const sourcedGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group.id },
});
expect(sourcedUserMemberships.length).toBe(0);
expect(sourcedGroupMemberships.length).toBe(0);
// Direct permissions should be preserved
const directUserMemberships = await UserMembership.findAll({
where: { documentId: childDocument.id, userId: user3.id },
});
const directGroupMemberships = await GroupMembership.findAll({
where: { documentId: childDocument.id, groupId: group2.id },
});
expect(directUserMemberships.length).toBe(1);
expect(directGroupMemberships.length).toBe(1);
expect(directUserMemberships[0].sourceId).toBeNull();
expect(directGroupMemberships[0].sourceId).toBeNull();
});
});