Files
outline/server/queues/tasks/SyncUserGroupsTask.test.ts
T
Tom MoorandClaude Opus 5.5 cdc8d17b9b feat: Gate group sync behind the GroupSync entitlement
Add a syncGroups team policy requiring an admin and the GroupSync plan
feature, enforced when enabling or setting up group sync and before each
sync task runs. Group sync can still be disabled after the entitlement is
lost.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-06 23:29:54 -04:00

135 lines
4.0 KiB
TypeScript

import { vi } from "vitest";
import { Plan } from "@shared/types";
import {
AuthenticationProvider,
Group,
GroupUser,
Team,
UserAuthentication,
} from "@server/models";
import { buildUser } from "@server/test/factories";
import { mockTaskSchedule } from "@server/test/support";
import { PluginManager } from "@server/utils/PluginManager";
import SyncUserGroupsTask from "./SyncUserGroupsTask";
async function setup() {
const user = await buildUser();
const authenticationProvider = (await AuthenticationProvider.findOne({
where: { teamId: user.teamId },
}))!;
await authenticationProvider.update({
settings: { groupSyncEnabled: true },
});
const authentication = (await UserAuthentication.findOne({
where: { userId: user.id },
}))!;
await authentication.update({ accessToken: "access-token" });
const fetchUserGroups = vi
.fn()
.mockResolvedValue([{ id: "ext-1", name: "Engineering" }]);
vi.spyOn(PluginManager, "getGroupSyncProvider").mockReturnValue({
useGroupClaim: false,
fetchUserGroups,
});
return { user, authentication, fetchUserGroups };
}
describe("SyncUserGroupsTask", () => {
const schedule = mockTaskSchedule();
beforeEach(() => {
vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Business);
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should schedule with a job id unique to the user authentication", async () => {
const { authentication } = await setup();
await new SyncUserGroupsTask().schedule({
userAuthenticationId: authentication.id,
});
expect(schedule).toHaveBeenCalledWith(
{ userAuthenticationId: authentication.id },
{ jobId: `sync-user-groups:${authentication.id}` }
);
});
it("should sync groups using the stored access token", async () => {
const { user, authentication, fetchUserGroups } = await setup();
await new SyncUserGroupsTask().perform({
userAuthenticationId: authentication.id,
});
expect(fetchUserGroups).toHaveBeenCalledWith("access-token", {
groupSyncEnabled: true,
});
const group = await Group.findOne({
where: { teamId: user.teamId, name: "Engineering" },
});
expect(group).not.toBeNull();
const membership = await GroupUser.findOne({
where: { groupId: group!.id, userId: user.id },
});
expect(membership).not.toBeNull();
});
it("should not sync groups for suspended users", async () => {
const { user, authentication, fetchUserGroups } = await setup();
await user.update({ suspendedAt: new Date() });
await new SyncUserGroupsTask().perform({
userAuthenticationId: authentication.id,
});
expect(fetchUserGroups).not.toHaveBeenCalled();
});
it("should not sync groups when group sync is disabled", async () => {
const { user, authentication, fetchUserGroups } = await setup();
const authenticationProvider = (await AuthenticationProvider.findOne({
where: { teamId: user.teamId },
}))!;
await authenticationProvider.update({
settings: { groupSyncEnabled: false },
});
await new SyncUserGroupsTask().perform({
userAuthenticationId: authentication.id,
});
expect(fetchUserGroups).not.toHaveBeenCalled();
});
it("should not sync groups when the team is not entitled to group sync", async () => {
const { authentication, fetchUserGroups } = await setup();
vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Community);
await new SyncUserGroupsTask().perform({
userAuthenticationId: authentication.id,
});
expect(fetchUserGroups).not.toHaveBeenCalled();
});
it("should throw when the provider fails so the task is retried", async () => {
const { authentication, fetchUserGroups } = await setup();
fetchUserGroups.mockRejectedValue(new Error("provider unavailable"));
await expect(
new SyncUserGroupsTask().perform({
userAuthenticationId: authentication.id,
})
).rejects.toThrow("provider unavailable");
});
});