mirror of
https://github.com/outline/outline.git
synced 2026-11-29 16:40:27 +03:00
* feat: Re-authorize collaboration connections when access changes Collaboration connections are authorized once, when the websocket is first established, so a connection outlives changes to the access it was granted. Add a processor that publishes access changes to the collaboration servers over Redis. Each server closes the connections it holds within the scope of the change and clients reconnect, at which point they are authorized as normal. Authorization stays in one place, the published scope only has to cover the connections that may be affected. The API update subscriber now shares a single Redis connection rather than opening its own. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor: Log an access change once rather than per connection Every closed connection is already logged by LoggerExtension, so record the reason and the number of connections affected once instead. The count also makes a scope that matches nothing visible. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
117 lines
3.1 KiB
TypeScript
117 lines
3.1 KiB
TypeScript
import { CollectionPermission } from "@shared/types";
|
|
import AuthenticationExtension from "@server/collaboration/AuthenticationExtension";
|
|
import type { CollectionUserEvent } from "@server/types";
|
|
import CollaborationAuthorizationProcessor from "./CollaborationAuthorizationProcessor";
|
|
|
|
describe("CollaborationAuthorizationProcessor", () => {
|
|
const processor = new CollaborationAuthorizationProcessor();
|
|
|
|
const buildAddUserEvent = (
|
|
overrides: Partial<CollectionUserEvent> = {}
|
|
): CollectionUserEvent => ({
|
|
name: "collections.add_user",
|
|
teamId: "team-id",
|
|
actorId: "actor-id",
|
|
ip: null,
|
|
userId: "user-id",
|
|
modelId: "membership-id",
|
|
collectionId: "collection-id",
|
|
data: { isNew: true },
|
|
...overrides,
|
|
});
|
|
|
|
const invalidate = () =>
|
|
vi.spyOn(AuthenticationExtension, "invalidate").mockResolvedValue();
|
|
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it("should invalidate when a membership is created", async () => {
|
|
const spy = invalidate();
|
|
|
|
await processor.perform(buildAddUserEvent());
|
|
|
|
expect(spy).toHaveBeenCalledWith({
|
|
userIds: ["user-id"],
|
|
collectionId: "collection-id",
|
|
});
|
|
});
|
|
|
|
it("should invalidate when a membership permission changed", async () => {
|
|
const spy = invalidate();
|
|
|
|
await processor.perform(
|
|
buildAddUserEvent({
|
|
data: { isNew: false },
|
|
changes: {
|
|
attributes: { permission: CollectionPermission.Read },
|
|
previous: { permission: CollectionPermission.ReadWrite },
|
|
},
|
|
})
|
|
);
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should not invalidate when a membership index changed", async () => {
|
|
const spy = invalidate();
|
|
|
|
await processor.perform(
|
|
buildAddUserEvent({
|
|
data: { isNew: false },
|
|
changes: {
|
|
attributes: { index: "P" },
|
|
previous: { index: "O" },
|
|
},
|
|
})
|
|
);
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should invalidate when a membership is removed", async () => {
|
|
const spy = invalidate();
|
|
|
|
// Removal events carry no data at all.
|
|
await processor.perform(
|
|
buildAddUserEvent({ name: "collections.remove_user", data: undefined })
|
|
);
|
|
|
|
expect(spy).toHaveBeenCalledWith({
|
|
userIds: ["user-id"],
|
|
collectionId: "collection-id",
|
|
});
|
|
});
|
|
|
|
it("should not scope a document membership to the document, as it is inherited", async () => {
|
|
const spy = invalidate();
|
|
|
|
await processor.perform({
|
|
name: "documents.remove_user",
|
|
teamId: "team-id",
|
|
actorId: "actor-id",
|
|
ip: null,
|
|
userId: "user-id",
|
|
modelId: "membership-id",
|
|
documentId: "document-id",
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith({ userIds: ["user-id"] });
|
|
});
|
|
|
|
it("should not scope a document group membership to the document", async () => {
|
|
const spy = invalidate();
|
|
|
|
await processor.perform({
|
|
name: "documents.remove_group",
|
|
teamId: "team-id",
|
|
actorId: "actor-id",
|
|
ip: null,
|
|
modelId: "group-id",
|
|
documentId: "document-id",
|
|
data: { membershipId: "membership-id" },
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalledWith({ groupId: "group-id" });
|
|
});
|
|
});
|