Skip to content

Commit 544181d

Browse files
committed
test(connect): update oauth-scope tests for server-side nonce state format
Update the connect-route test harness in oauth-scope.test.ts to match the new state format: state now carries only a 64-char hex nonce, with userId stored server-side in Redis. Add a Redis mock to buildConnectApp so the callback route can look up the userId during tests.
1 parent ace79ea commit 544181d

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

apps/backend/src/__tests__/oauth-scope.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@ const USER_ID = 'user-scope-test';
3838

3939
// ── Connect-route test harness ────────────────────────────────────────────────
4040

41-
function makeConnectState(userId: string): string {
42-
return Buffer.from(JSON.stringify({ userId, nonce: 'test-nonce' })).toString('base64');
41+
// 64 lowercase hex chars — matches the format generated by randomBytes(32).toString('hex')
42+
const TEST_NONCE = 'a'.repeat(64);
43+
44+
function makeConnectState(_userId: string): string {
45+
// State carries only the nonce; userId is stored server-side in Redis.
46+
return TEST_NONCE;
4347
}
4448

45-
function buildConnectApp(mockPrisma: Partial<PrismaClient>) {
49+
function buildConnectApp(mockPrisma: Partial<PrismaClient>, userId = USER_ID) {
50+
const mockRedis = {
51+
get: vi.fn().mockResolvedValue(JSON.stringify({ userId })),
52+
del: vi.fn().mockResolvedValue(1),
53+
set: vi.fn().mockResolvedValue('OK'),
54+
};
55+
4656
const app = Fastify({ logger: false });
4757
app.decorate('prisma', mockPrisma as PrismaClient);
48-
app.decorate('authenticate', async (req: any) => { req.user = { id: USER_ID }; });
58+
app.decorate('redis', mockRedis as any);
59+
app.decorate('authenticate', async (req: any) => { req.user = { id: userId }; });
4960
app.register(connectRoutes, { prefix: '/api/connect' });
5061
return app.ready().then(() => app);
5162
}

0 commit comments

Comments
 (0)