Files
outline/server/commands/teamUpdater.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

246 lines
7.2 KiB
TypeScript

import type { MockInstance } from "vitest";
import { vi } from "vitest";
import type { CustomTheme } from "@shared/types";
import { randomString } from "@shared/random";
import { CommentingAccess, Plan, TeamPreference } from "@shared/types";
import { Team } from "@server/models";
import { buildAdmin, buildTeam, buildUser } from "@server/test/factories";
import { setCloudHosted, withContext } from "@server/test/support";
import teamUpdater from "./teamUpdater";
describe("teamUpdater", () => {
describe("default share expiry", () => {
let planSpy: MockInstance<() => Plan>;
beforeEach(() => {
planSpy = vi.spyOn(Team.prototype, "plan", "get");
});
afterEach(() => {
planSpy.mockRestore();
});
const update = async (days: number) => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
return {
team,
user,
run: () =>
withContext(user, (ctx) =>
teamUpdater(ctx, {
params: {
preferences: { [TeamPreference.DefaultShareExpiryDays]: days },
},
user,
team,
})
),
};
};
it("should allow setting with the advanced security entitlement", async () => {
planSpy.mockReturnValue(Plan.Business);
const { run } = await update(30);
const updated = await run();
await updated.reload();
expect(
updated.getPreference(TeamPreference.DefaultShareExpiryDays)
).toEqual(30);
});
it("should require the advanced security entitlement to set", async () => {
planSpy.mockReturnValue(Plan.Community);
const { run } = await update(30);
await expect(run()).rejects.toThrow();
});
it("should allow removing without the entitlement", async () => {
planSpy.mockReturnValue(Plan.Community);
const { team, run } = await update(0);
team.setPreference(TeamPreference.DefaultShareExpiryDays, 30);
await team.save();
const updated = await run();
await updated.reload();
expect(
updated.getPreference(TeamPreference.DefaultShareExpiryDays)
).toEqual(0);
});
});
describe("preferences", () => {
it("should update preference when value changes", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const newValue = CommentingAccess.None;
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: {
preferences: {
[TeamPreference.Commenting]: newValue,
},
},
user,
team,
})
);
expect(updatedTeam.getPreference(TeamPreference.Commenting)).toEqual(
newValue
);
});
it("should not update preference when value is the same (isEqual check)", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
// Set a known preference value
team.setPreference(TeamPreference.ViewersCanExport, true);
await team.save();
// Try to set the same value again
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: {
preferences: {
[TeamPreference.ViewersCanExport]: true,
},
},
user,
team,
})
);
// Verify the value is still correct
expect(
updatedTeam.getPreference(TeamPreference.ViewersCanExport)
).toEqual(true);
});
it("should handle complex preference values with deep equality", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
// Set initial theme
team.setPreference(TeamPreference.CustomTheme, {
accent: "#FF0000",
accentText: "#00FF00",
});
await team.save();
// Pass an object with the same values (but different reference)
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: {
preferences: {
[TeamPreference.CustomTheme]: {
accent: "#FF0000",
accentText: "#00FF00",
},
},
},
user,
team,
})
);
// The theme should still be set correctly with isEqual comparison
const theme = updatedTeam.getPreference(
TeamPreference.CustomTheme
) as Partial<CustomTheme>;
expect(theme?.accent).toEqual("#FF0000");
expect(theme?.accentText).toEqual("#00FF00");
});
it("should update only preferences that have different values", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
// Set initial values
team.setPreference(TeamPreference.Commenting, CommentingAccess.Members);
team.setPreference(TeamPreference.ViewersCanExport, true);
await team.save();
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: {
preferences: {
// Same - should skip due to isEqual
[TeamPreference.Commenting]: CommentingAccess.Members,
[TeamPreference.ViewersCanExport]: false, // Different - should update
},
},
user,
team,
})
);
expect(updatedTeam.getPreference(TeamPreference.Commenting)).toEqual(
CommentingAccess.Members
);
expect(
updatedTeam.getPreference(TeamPreference.ViewersCanExport)
).toEqual(false);
});
});
describe("basic attributes", () => {
it("should update team name", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: { name: "Updated Team Name" },
user,
team,
})
);
expect(updatedTeam.name).toEqual("Updated Team Name");
});
});
describe("subdomain", () => {
beforeEach(setCloudHosted);
it("should update subdomain when available", async () => {
const subdomain = `available-${randomString({ length: 10, charset: "alphabetic", capitalization: "lowercase" })}`;
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const updatedTeam = await withContext(user, (ctx) =>
teamUpdater(ctx, {
params: { subdomain },
user,
team,
})
);
expect(updatedTeam.subdomain).toEqual(subdomain);
});
it("should throw a validation error when subdomain is taken", async () => {
const subdomain = `taken-${randomString({ length: 10, charset: "alphabetic", capitalization: "lowercase" })}`;
await buildTeam({ subdomain });
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
await expect(
withContext(user, (ctx) =>
teamUpdater(ctx, {
params: { subdomain },
user,
team,
})
)
).rejects.toThrow("Subdomain is already in use");
});
});
});