Files
crosscode-platform/apps/events/backend/tests/accountsCalls.test.ts
T
joshuaandClaude Opus 5.5 c627c61de1 Cross Events 3.0.0: sign-in through the church's Accounts
Settings -> Sign-in: check, move (everyone into Accounts with their password
hashes and authenticator apps, count/hash check, then switch) and undo for
14 days. Sign-in through auth-client with a one-time handoff to Events' own
token; staff roles stepped up to mfa; sign-out and back-channel logout;
user.updated/export/deleted; admin 2FA reset, unlock and profile changes via
Accounts; /login, /register, /forgot-password forward once moved; Events'
own sign-in endpoints answer SIGN_IN_MOVED. User.accountId, AccountsSignIn.

npm run test:accounts runs Events' code against the real Accounts app (11
tests); 5 new Jest tests. MIGRATING-TO-3.md, plan "Changes while building".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 14:10:30 +02:00

72 lines
4.1 KiB
TypeScript

// 3.0.0 (docs/EVENTS-3.0-PLAN.md §7): Accounts' user.export and user.deleted calls, answered with
// Events' existing export and deletion rules. The sign-in itself, the move and the other calls run
// against the real Accounts in tests/accounts-integration (npm run test:accounts).
const mockUsers = new Map<string, { id: string; accountId: string | null; tokenVersion: number }>();
const mockAnonymised: string[] = [];
let mockBlockers: { eventTitle: string; endDate: Date }[] = [];
jest.mock('../src/config/db', () => ({
user: {
findUnique: async ({ where }: { where: { accountId?: string; id?: string } }) =>
[...mockUsers.values()].find((u) => (where.accountId ? u.accountId === where.accountId : u.id === where.id)) ?? null,
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => {
const u = mockUsers.get(where.id)!;
if (data.tokenVersion && typeof data.tokenVersion === 'object') u.tokenVersion += 1;
if ('accountId' in data) u.accountId = data.accountId as string | null;
return u;
},
},
}));
jest.mock('../src/utils/privacy', () => ({
exportUserData: async (id: string) => ({ account: { id, name: 'Thandi' }, registrations: [{ event: 'Youth Camp' }], payments: [], seasonPasses: [] }),
deletionBlockers: async () => mockBlockers,
anonymiseUser: async (id: string) => { mockAnonymised.push(id); },
}));
import { IDENTITY_SCHEMA, userDeletedReplySchema, userExportReplySchema } from '@crosscode/contracts';
import { exportSections, handleAccountsCall } from '../src/accounts/calls';
const SUB = '5f0c7d2a-3b1e-4c8f-9a6d-2e4b1c7f8a90';
const base = { schema: IDENTITY_SCHEMA, callId: '0b6e1f2a-3c4d-4e5f-8a9b-0c1d2e3f4a5b', occurredAt: '2026-11-20T10:00:00.000Z', sub: SUB } as const;
beforeEach(() => {
mockUsers.clear();
mockUsers.set('u1', { id: 'u1', accountId: SUB, tokenVersion: 0 });
mockAnonymised.length = 0;
mockBlockers = [];
});
test('user.export answers with Events\' export as sections, empty ones left out', async () => {
const reply = await handleAccountsCall({ ...base, type: 'user.export', exportId: '1c7f2a3b-4d5e-4f6a-9b0c-1d2e3f4a5b6c' });
expect(userExportReplySchema.safeParse(reply).success).toBe(true);
const sections = (reply as { sections: { key: string; title: string }[] }).sections;
expect(sections.map((s) => s.key)).toEqual(['account', 'registrations']);
expect(sections[1]!.title).toBe('Registrations');
});
test('section keys fit the contract', () => {
expect(exportSections({ seasonPasses: [{}], kidsFamily: { a: 1 } }).map((s) => s.key)).toEqual(['season_passes', 'kids_family']);
});
test('user.deleted waits while there is a registration for an event that has not ended', async () => {
mockBlockers = [{ eventTitle: 'Youth Camp', endDate: new Date('2027-03-14T12:00:00Z') }];
const reply = await handleAccountsCall({ ...base, type: 'user.deleted', approvedAt: base.occurredAt });
expect(userDeletedReplySchema.parse(reply)).toMatchObject({ status: 'deferred', reason: 'Has a registration for Youth Camp (2027-03-14)' });
expect(new Date((reply as { retryAfter: string }).retryAfter).getTime()).toBeGreaterThan(new Date('2027-03-14T12:00:00Z').getTime());
expect(mockAnonymised).toEqual([]);
});
test('user.deleted anonymises, signs them out here and unlinks the account', async () => {
const reply = await handleAccountsCall({ ...base, type: 'user.deleted', approvedAt: base.occurredAt });
expect(reply).toEqual({ schema: IDENTITY_SCHEMA, status: 'deleted' });
expect(mockAnonymised).toEqual(['u1']);
expect(mockUsers.get('u1')).toMatchObject({ accountId: null, tokenVersion: 1 });
});
test('a call for someone Events doesn\'t know is answered as done', async () => {
const other = { ...base, sub: '8d2e4f6a-1b3c-4d5e-8f7a-9b0c1d2e3f4a' };
expect(await handleAccountsCall({ ...other, type: 'user.deleted', approvedAt: base.occurredAt })).toEqual({ schema: IDENTITY_SCHEMA, status: 'deleted' });
const exp = await handleAccountsCall({ ...other, type: 'user.export', exportId: '1c7f2a3b-4d5e-4f6a-9b0c-1d2e3f4a5b6c' });
expect((exp as { sections: unknown[] }).sections).toEqual([]);
});