// @vitest-environment node import { test, expect } from 'vitest'; import { checkShareAccess, expiryFromDays, hashPasscode } from './shareLinks.js'; const NOW = 1_800_000_000_000; test('expiry is a number of days from now, or never', () => { expect(expiryFromDays(null, NOW)).toBeNull(); expect(expiryFromDays(0, NOW)).toBeNull(); expect(expiryFromDays('x', NOW)).toBeNull(); expect(expiryFromDays(7, NOW)).toBe(NOW + 7 * 86_400_000); expect(expiryFromDays(9999, NOW)).toBe(NOW + 365 * 86_400_000); // never more than a year }); test('a link with no rules is open; an expired one says so', async () => { expect((await checkShareAccess({}, NOW)).ok).toBe(true); expect(await checkShareAccess({ expiresAt: NOW + 1000 }, NOW)).toEqual({ ok: true }); expect(await checkShareAccess({ expiresAt: NOW - 1 }, NOW)).toMatchObject({ ok: false, status: 410 }); }); test('a passcode is asked for, and only the right one opens the link', async () => { const passHash = await hashPasscode(' moses '); expect(passHash).not.toContain('moses'); expect(await checkShareAccess({ passHash }, NOW)).toMatchObject({ ok: false, status: 401, needsPasscode: true }); expect(await checkShareAccess({ passHash, supplied: 'aaron' }, NOW)).toMatchObject({ ok: false, status: 401, needsPasscode: true, error: expect.stringMatching(/not right/) }); expect(await checkShareAccess({ passHash, supplied: ' moses ' }, NOW)).toEqual({ ok: true }); expect(await hashPasscode(' ')).toBeNull(); }); test('an expired link is reported as expired even before the passcode is checked', async () => { const passHash = await hashPasscode('x'); expect(await checkShareAccess({ passHash, expiresAt: NOW - 5, supplied: 'x' }, NOW)).toMatchObject({ status: 410 }); });