Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

6.3 KB
Raw Blame
import { WebauthnChallenges } from '../../../../src/db/entities/WebauthnChallenges.entity';
import type { WebauthnChallengesRepository } from '../../../../src/db/repositories/WebauthnChallenges.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createUser } from '../../../helpers/factories';
import { countRows, findRow, findRows, insertRow } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';

import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';

const testDb = createSnapshotTestDb();
let t: TestOrm;
let challenges: WebauthnChallengesRepository;

beforeAll(async () => {
  t = await createTestOrm(testDb);
  challenges = t.repo(WebauthnChallenges);
});
beforeEach(() => {
  resetTestDb(testDb);
  t.clear();
});
afterAll(async () => {
  await t.close();
  testDb.close();
});

async function seedChallenge(challenge: string, userId: number | null, type: string, expiresAt: number): Promise<void> {
  await insertRow(t, WebauthnChallenges, { challenge, user: userId, type, expires_at: expiresAt });
}

function challengeCount(): Promise<number> {
  return countRows(t, WebauthnChallenges);
}

describe('WebauthnChallengesRepository', () => {
  describe('purgeExpired', () => {
    it('WEBAUTHN-CHAL-REPO-001: deletes only rows past their expiry, live rows survive', async () => {
      const { user } = createUser(testDb);
      const now = Date.now();
      await seedChallenge('expired-1', user.id, 'registration', now - 1000);
      await seedChallenge('expired-2', null, 'authentication', now - 1);
      await seedChallenge('live', user.id, 'registration', now + 60_000);

      await challenges.purgeExpired(now);

      expect(await challengeCount()).toBe(1);
      const remaining = await findRows(t, WebauthnChallenges);
      expect(remaining.map((r) => ({ challenge: r.challenge }))).toEqual([{ challenge: 'live' }]);
    });

    it('WEBAUTHN-CHAL-REPO-002: an empty table is a no-op', async () => {
      await challenges.purgeExpired(Date.now());
      expect(await challengeCount()).toBe(0);
    });
  });

  describe('insertChallenge', () => {
    it('WEBAUTHN-CHAL-REPO-003: writes the exact column set, user-bound (registration)', async () => {
      const { user } = createUser(testDb);
      const expiresAt = Date.now() + 300_000;
      await challenges.insertChallenge({
        challenge: 'reg-chal',
        user_id: user.id,
        type: 'registration',
        expires_at: expiresAt,
      });

      const row = await findRow(t, WebauthnChallenges, { challenge: 'reg-chal' });
      expect(row).toMatchObject({
        challenge: 'reg-chal',
        user_id: user.id,
        type: 'registration',
        expires_at: expiresAt,
      });
    });

    it('WEBAUTHN-CHAL-REPO-004: writes a NULL user_id (anonymous authentication challenge)', async () => {
      const expiresAt = Date.now() + 300_000;
      await challenges.insertChallenge({
        challenge: 'auth-chal',
        user_id: null,
        type: 'authentication',
        expires_at: expiresAt,
      });

      const row = await findRow(t, WebauthnChallenges, { challenge: 'auth-chal' });
      expect(row).toMatchObject({ user_id: null, type: 'authentication' });
    });
  });

  describe('claimChallenge', () => {
    it('WEBAUTHN-CHAL-REPO-005: claims a live, matching row and returns its user_id', async () => {
      const { user } = createUser(testDb);
      const now = Date.now();
      await seedChallenge('claim-me', user.id, 'registration', now + 60_000);

      const result = await challenges.claimChallenge('claim-me', 'registration', now);

      expect(result).toEqual({ user_id: user.id });
      expect(await challengeCount()).toBe(0); // spent
    });

    it('WEBAUTHN-CHAL-REPO-006: claims an anonymous (null user_id) row', async () => {
      const now = Date.now();
      await seedChallenge('anon-claim', null, 'authentication', now + 60_000);

      expect(await challenges.claimChallenge('anon-claim', 'authentication', now)).toEqual({ user_id: null });
    });

    it('WEBAUTHN-CHAL-REPO-007: an unknown challenge is null, nothing is deleted', async () => {
      expect(await challenges.claimChallenge('never-stored', 'registration', Date.now())).toBeNull();
    });

    it("WEBAUTHN-CHAL-REPO-008: an expired row is null and stays in the table (purgeExpired's job, not claimChallenge's)", async () => {
      const now = Date.now();
      await seedChallenge('stale', null, 'registration', now - 1);

      expect(await challenges.claimChallenge('stale', 'registration', now)).toBeNull();
      expect(await challengeCount()).toBe(1); // still there — claimChallenge only deletes a MATCHING row
    });

    it('WEBAUTHN-CHAL-REPO-009: a type mismatch is null, the row survives for its real type', async () => {
      const { user } = createUser(testDb);
      const now = Date.now();
      await seedChallenge('wrong-type', user.id, 'registration', now + 60_000);

      expect(await challenges.claimChallenge('wrong-type', 'authentication', now)).toBeNull();
      expect(await challengeCount()).toBe(1);
      expect(await challenges.claimChallenge('wrong-type', 'registration', now)).toEqual({ user_id: user.id });
    });

    it('WEBAUTHN-CHAL-REPO-010: single-use — a second claim of the same challenge is null', async () => {
      const { user } = createUser(testDb);
      const now = Date.now();
      await seedChallenge('once-only', user.id, 'registration', now + 60_000);

      expect(await challenges.claimChallenge('once-only', 'registration', now)).toEqual({ user_id: user.id });
      expect(await challenges.claimChallenge('once-only', 'registration', now)).toBeNull();
    });

    it('WEBAUTHN-CHAL-REPO-011: two concurrent claims of the same challenge — exactly one winner', async () => {
      const { user } = createUser(testDb);
      const now = Date.now();
      await seedChallenge('race-me', user.id, 'registration', now + 60_000);

      const [a, b] = await Promise.all([
        challenges.claimChallenge('race-me', 'registration', now),
        challenges.claimChallenge('race-me', 'registration', now),
      ]);

      const winners = [a, b].filter((r) => r !== null);
      expect(winners).toHaveLength(1);
      expect(winners[0]).toEqual({ user_id: user.id });
      expect(await challengeCount()).toBe(0);
    });
  });
});