Files
outline/server/queues/tasks/DocumentAccessRequestNotificationTask.test.ts
T
Tom Moor 9666d8e825 feat: Add collection owners
- Collections can have owners, who also own every document in the
  collection, shown first in document bylines
- Owner is a role in the collection share panel and invite picker for
  individual users, adding an owner gives them the manage permission
- Lowering an owner's permission or removing their access removes the
  ownership
- ownerIds on collections.create, collections.update and the MCP
  collection tools
- The document owner filter includes documents in owned collections
- Access requests go to document and collection owners together
- Collection owners are subscribed to the collection and notified when
  they are added
- Deleted users are removed from collection owners
- Renames the personal collection policy helper to
  isPersonalCollectionOwner
2026-11-06 22:45:07 -05:00

455 lines
13 KiB
TypeScript

import {
NotificationEventType,
DocumentPermission,
CollectionPermission,
} from "@shared/types";
import Logger from "@server/logging/Logger";
import { AccessRequest, Notification, UserMembership } from "@server/models";
import DocumentAccessRequestNotificationsTask from "./DocumentAccessRequestNotificationsTask";
import {
buildAdmin,
buildCollection,
buildDocument,
buildTeam,
buildUser,
} from "@server/test/factories";
const ip = "127.0.0.1";
describe("DocumentAccessRequestNotificationsTask", () => {
let task: DocumentAccessRequestNotificationsTask;
beforeEach(() => {
task = new DocumentAccessRequestNotificationsTask();
vi.clearAllMocks();
});
describe("perform", () => {
it("should fail with the correct error if the document is not found", async () => {
const loggerSpy = vi.spyOn(Logger, "debug");
await task.perform({
name: "access_requests.create",
modelId: "ar1",
documentId: "doc1",
teamId: "team1",
actorId: "actor1",
ip,
});
expect(loggerSpy).toHaveBeenCalledWith(
"task",
"Document not found for access request notification",
{ documentId: "doc1" }
);
});
it("should send notifications to document managers", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const manager1 = await buildUser({ teamId: team.id });
const manager2 = await buildUser({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const document = await buildDocument({ teamId: team.id });
await UserMembership.create({
userId: manager1.id,
documentId: document.id,
createdById: manager1.id,
permission: DocumentPermission.Admin,
});
await UserMembership.create({
userId: manager2.id,
documentId: document.id,
createdById: manager2.id,
permission: DocumentPermission.Admin,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
event: NotificationEventType.RequestDocumentAccess,
userId: manager1.id,
actorId: actor.id,
documentId: document.id,
teamId: team.id,
})
);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
event: NotificationEventType.RequestDocumentAccess,
userId: manager2.id,
actorId: actor.id,
documentId: document.id,
teamId: team.id,
})
);
});
it("should send notifications to collection managers", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const manager1 = await buildAdmin({ teamId: team.id });
const manager2 = await buildUser({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
createdById: manager1.id,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
await UserMembership.create({
userId: manager2.id,
collectionId: collection.id,
createdById: manager2.id,
permission: CollectionPermission.Admin,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
event: NotificationEventType.RequestDocumentAccess,
userId: manager1.id,
actorId: actor.id,
documentId: document.id,
teamId: team.id,
})
);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
event: NotificationEventType.RequestDocumentAccess,
userId: manager2.id,
actorId: actor.id,
documentId: document.id,
teamId: team.id,
})
);
});
it("should not send notifications to suspended users", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const manager = await buildUser({
teamId: team.id,
suspendedAt: new Date(),
});
const actor = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
createdById: manager.id,
});
await UserMembership.create({
userId: manager.id,
documentId: document.id,
permission: DocumentPermission.Admin,
createdById: manager.id,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).not.toHaveBeenCalled();
});
it("should not send notification if user has disabled this notification type", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
createdById: admin.id,
});
await UserMembership.create({
userId: admin.id,
documentId: document.id,
permission: DocumentPermission.Admin,
createdById: admin.id,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
// disable notifications for this event type
admin.setNotificationEventType(
NotificationEventType.RequestDocumentAccess,
false
);
await admin.save();
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).not.toHaveBeenCalled();
});
it("should fall back to workspace admins when no document or collection admin is set", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const workspaceAdmin = await buildAdmin({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const collection = await buildCollection({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
// Remove the auto-created collection admin membership so workspace admin
// is the only fallback target.
await UserMembership.destroy({ where: { collectionId: collection.id } });
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
event: NotificationEventType.RequestDocumentAccess,
userId: workspaceAdmin.id,
actorId: actor.id,
documentId: document.id,
teamId: team.id,
})
);
});
it("should not fall back to workspace admins when document admins exist", async () => {
const spy = vi.spyOn(Notification, "create");
const team = await buildTeam();
const workspaceAdmin = await buildAdmin({ teamId: team.id });
const documentAdmin = await buildUser({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const document = await buildDocument({
teamId: team.id,
createdById: documentAdmin.id,
});
await UserMembership.create({
userId: documentAdmin.id,
documentId: document.id,
permission: DocumentPermission.Admin,
createdById: documentAdmin.id,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
const task = new DocumentAccessRequestNotificationsTask();
await task.perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
userId: documentAdmin.id,
})
);
expect(spy).not.toHaveBeenCalledWith(
expect.objectContaining({
userId: workspaceAdmin.id,
})
);
});
});
});
describe("DocumentAccessRequestNotificationsTask with owners", () => {
const request = async (ownerPermission: DocumentPermission) => {
const team = await buildTeam();
const admin = await buildUser({ teamId: team.id });
const owner = await buildUser({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
ownerIds: [owner.id],
});
await UserMembership.create({
userId: admin.id,
documentId: document.id,
createdById: admin.id,
permission: DocumentPermission.Admin,
});
await UserMembership.create({
userId: owner.id,
documentId: document.id,
createdById: admin.id,
permission: ownerPermission,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
await new DocumentAccessRequestNotificationsTask().perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
const notifications = await Notification.findAll({
where: {
documentId: document.id,
event: NotificationEventType.RequestDocumentAccess,
},
});
return {
admin,
owner,
recipientIds: notifications.map((notification) => notification.userId),
};
};
it("should notify owners that can manage the document instead of admins", async () => {
const { owner, recipientIds } = await request(DocumentPermission.ReadWrite);
expect(recipientIds).toEqual([owner.id]);
});
it("should fall back to document admins when owners cannot manage the document", async () => {
const { admin, recipientIds } = await request(DocumentPermission.Read);
expect(recipientIds).toEqual([admin.id]);
});
it("should notify collection owners that can manage the document", async () => {
const team = await buildTeam();
const admin = await buildUser({ teamId: team.id });
const owner = await buildUser({ teamId: team.id });
const actor = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
ownerIds: [owner.id],
});
await UserMembership.create({
userId: owner.id,
collectionId: collection.id,
createdById: owner.id,
permission: CollectionPermission.Admin,
});
const document = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
await UserMembership.create({
userId: admin.id,
documentId: document.id,
createdById: admin.id,
permission: DocumentPermission.Admin,
});
const accessRequest = await AccessRequest.create({
documentId: document.id,
userId: actor.id,
teamId: team.id,
});
await new DocumentAccessRequestNotificationsTask().perform({
name: "access_requests.create",
modelId: accessRequest.id,
documentId: document.id,
teamId: team.id,
actorId: actor.id,
ip,
});
const notifications = await Notification.findAll({
where: {
documentId: document.id,
event: NotificationEventType.RequestDocumentAccess,
},
});
expect(notifications.map((notification) => notification.userId)).toEqual([
owner.id,
]);
});
});