Files
outline/server/models/UserMembership.test.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

155 lines
4.7 KiB
TypeScript

import { sleep } from "@shared/utils/timers";
import { CollectionPermission } from "@shared/types";
import {
buildCollection,
buildDocument,
buildGroup,
buildUser,
} from "@server/test/factories";
import { sequelize } from "@server/storage/database";
import { withContext } from "@server/test/support";
import Event from "./Event";
import GroupMembership from "./GroupMembership";
import UserMembership from "./UserMembership";
describe("UserMembership", () => {
describe("events", () => {
it("should derive the event teamId from the document when the context has no authenticated user", async () => {
const document = await buildDocument();
const user = await buildUser({ teamId: document.teamId });
await UserMembership.create({
createdById: user.id,
userId: user.id,
documentId: document.id,
});
const event = await Event.findOne({
where: { name: "documents.add_user", documentId: document.id },
});
expect(event).not.toBeNull();
expect(event?.teamId).toEqual(document.teamId);
});
});
describe("withCollection scope", () => {
it("should return the collection", async () => {
const collection = await buildCollection();
const user = await buildUser({ teamId: collection.teamId });
await UserMembership.create({
createdById: user.id,
userId: user.id,
collectionId: collection.id,
});
const membership = await UserMembership.scope("withCollection").findOne({
where: {
userId: user.id,
collectionId: collection.id,
},
});
expect(membership).toBeDefined();
expect(membership?.collection).toBeDefined();
expect(membership?.collection?.id).toEqual(collection.id);
});
});
describe("validateLastAdminPermission", () => {
it("should not allow the last user and group manager to be removed concurrently", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const group = await buildGroup({ teamId: user.teamId });
// The collection creator is its only user manager.
const userMembership = await UserMembership.findOne({
where: { collectionId: collection.id, userId: user.id },
rejectOnEmpty: true,
});
const groupMembership = await GroupMembership.create({
createdById: user.id,
groupId: group.id,
collectionId: collection.id,
permission: CollectionPermission.Admin,
});
let removed: () => void = () => undefined;
const first = new Promise<void>((resolve) => {
removed = resolve;
});
let commit: () => void = () => undefined;
const held = new Promise<void>((resolve) => {
commit = resolve;
});
// The user membership goes first, leaving the group as the only manager.
const one = withContext(user, async (ctx) => {
await userMembership.destroyWithCtx(ctx);
removed();
await held;
});
await first;
const two = withContext(user, (ctx) =>
groupMembership.destroyWithCtx(ctx)
);
await sleep(250);
commit();
await one;
await expect(two).rejects.toThrow(
"At least one user or group must have manage permissions"
);
});
});
describe("findOneForUpdate", () => {
it("should create one membership for concurrent grants", async () => {
const collection = await buildCollection();
const user = await buildUser({ teamId: collection.teamId });
const grant = (permission: CollectionPermission, ids: string[]) =>
sequelize.transaction(async (transaction) => {
const [collectionId, userId] = ids;
const membership = await UserMembership.findOneForUpdate(
{ collectionId, userId },
{ transaction }
);
await sleep(50);
if (membership) {
membership.permission = permission;
return membership.save({ transaction });
}
return UserMembership.create(
{
collectionId,
userId,
permission,
createdById: user.id,
},
{ transaction }
);
});
await Promise.all([
grant(CollectionPermission.Read, [collection.id, user.id]),
grant(CollectionPermission.ReadWrite, [
collection.id.toUpperCase(),
user.id.toUpperCase(),
]),
]);
const memberships = await UserMembership.findAll({
where: { collectionId: collection.id, userId: user.id },
});
expect(memberships).toHaveLength(1);
});
});
});