Files
crosscode-platform/apps/accounts/backend/tests/lib.test.ts
T

96 lines
4.6 KiB
TypeScript

// The small pieces: passwords (including bcrypt hashes Cross Events 3.0 will bring across), codes,
// recovery codes, secrets at rest, licence states, the church profile rules.
import assert from 'node:assert/strict';
import { describe, test } from 'node:test';
import bcrypt from 'bcryptjs';
import { hashPassword, needsRehash, passwordProblem, verifyPassword } from '../src/lib/passwords.js';
import { codeAt, generateSecret, matchStep, otpauthUrl } from '../src/lib/totp.js';
import { hashRecoveryCode, newRecoveryCodes, normaliseRecoveryCode } from '../src/lib/recoveryCodes.js';
import { Secrets } from '../src/lib/crypto.js';
import { stateFor, type LicenceClaims } from '../src/hub/licence.js';
import { normalisePhone } from '../src/accounts/service.js';
import { waNumber } from '../src/lib/messages.js';
describe('passwords', () => {
test('Argon2id hashes verify, wrong passwords don\'t', async () => {
const h = await hashPassword('Correct-horse-1');
assert.match(h, /^\$argon2id\$/);
assert.equal(await verifyPassword('Correct-horse-1', h), true);
assert.equal(await verifyPassword('wrong-password', h), false);
assert.equal(needsRehash(h), false);
});
test('bcrypt hashes from Cross Events verify and are marked for re-hashing', async () => {
const h = await bcrypt.hash('Old-events-password', 4);
assert.equal(await verifyPassword('Old-events-password', h), true);
assert.equal(needsRehash(h), true);
assert.equal(await verifyPassword('anything', null), false);
assert.equal(await verifyPassword('anything', 'not-a-hash'), false);
});
test('at least 8 characters', () => {
assert.ok(passwordProblem('short'));
assert.equal(passwordProblem('long-enough'), null);
});
});
describe('authenticator codes', () => {
test('a code works once, with a step either side for clock drift', () => {
const secret = generateSecret();
const now = Date.now();
const step = Math.floor(now / 30000);
assert.equal(matchStep(secret, codeAt(secret, step), { now }), step);
assert.equal(matchStep(secret, codeAt(secret, step - 1), { now }), step - 1);
assert.equal(matchStep(secret, codeAt(secret, step - 3), { now }), null);
assert.equal(matchStep(secret, codeAt(secret, step), { now, afterStep: step }), null);
assert.match(otpauthUrl(secret, 'a@b.co', 'Hope Family'), /^otpauth:\/\/totp\/Hope%20Family%3Aa%40b\.co\?secret=/);
});
test('the same codes as Cross Events 2.3 for the same secret (RFC 6238 test vector)', () => {
// RFC 6238 SHA-1 secret "12345678901234567890" in base32, T = 59 s → 94287082 (last 6 digits).
assert.equal(codeAt('GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ', 1), '287082');
});
});
describe('recovery codes', () => {
test('ten, readable, matched whatever the case and dashes', () => {
const { codes, hashes } = newRecoveryCodes();
assert.equal(codes.length, 10);
assert.match(codes[0]!, /^[a-z2-9]{4}-[a-z2-9]{4}-[a-z2-9]{4}$/);
assert.equal(hashRecoveryCode(codes[0]!.toUpperCase().replace(/-/g, ' ')), hashes[0]);
assert.equal(normaliseRecoveryCode('ABCD EFGH JKMN'), 'abcd-efgh-jkmn');
});
});
describe('secrets at rest', () => {
test('round trip; another key can\'t read it; changed data fails', () => {
const a = new Secrets('one-secret-that-is-long-enough-for-tests');
const enc = a.encrypt('totp-secret');
assert.match(enc, /^enc:/);
assert.equal(a.decrypt(enc), 'totp-secret');
assert.throws(() => new Secrets('another-secret-that-is-long-enough').decrypt(enc));
assert.throws(() => a.decrypt(`${enc.slice(0, -2)}00`));
});
});
describe('licence states (Accounts never goes read-only)', () => {
const base: LicenceClaims = { v: 1, installId: 'i', customer: 'Hope', status: 'active', licenceExpiresAt: null, validUntil: '2026-12-01T00:00:00Z', graceDays: 14 };
test('active, grace, lapsed, revoked, unlicensed', () => {
assert.equal(stateFor(base, 'i', new Date('2026-11-20')).state, 'active');
assert.equal(stateFor(base, 'i', new Date('2026-12-05')).state, 'grace');
assert.equal(stateFor(base, 'i', new Date('2027-01-01')).state, 'lapsed');
assert.equal(stateFor({ ...base, status: 'revoked' }, 'i').state, 'revoked');
assert.equal(stateFor(null, null).state, 'unlicensed');
assert.equal(stateFor(base, 'other').state, 'unlicensed');
});
});
describe('phone numbers', () => {
test('South African numbers in every common format', () => {
assert.equal(normalisePhone('082 123 4567'), '+27821234567');
assert.equal(normalisePhone('+27 82 123 4567'), '+27821234567');
assert.equal(normalisePhone('hello'), null);
assert.equal(waNumber('+27821234567'), '27821234567');
});
});