Files
outline/server/commands/teamUpdater.test.ts
T
Tom MoorandClaude Opus 5 34383b1690 fix: Duplicate team subdomain surfaced as a 500 error (#13329)
* fix: Friendly error and retry for duplicate team subdomains

Picking a subdomain already in use in settings surfaced the raw unique
constraint violation as a 500, and concurrent signups could race between
the availability check and the insert.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Use setCloudHosted helper rather than spying on env

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: Move retryOnUniqueConstraintError to storage/database

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 08:03:56 -04:00

181 lines
5.4 KiB
TypeScript

import type { CustomTheme } from "@shared/types";
import { randomString } from "@shared/random";
import { CommentingAccess, TeamPreference } from "@shared/types";
import { buildTeam, buildUser } from "@server/test/factories";
import { setCloudHosted, withAPIContext } from "@server/test/support";
import teamUpdater from "./teamUpdater";
describe("teamUpdater", () => {
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 withAPIContext(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 withAPIContext(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 withAPIContext(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 withAPIContext(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 withAPIContext(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 withAPIContext(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(
withAPIContext(user, (ctx) =>
teamUpdater(ctx, {
params: { subdomain },
user,
team,
})
)
).rejects.toThrow("Subdomain is already in use");
});
});
});