Files
menu/test/auth.test.ts
T
tanshu e64f65642f Add ESLint and Prettier
Prettier owns formatting: .prettierrc.json, format and format:check scripts, and
a one-off pass over the repo so the check is green from the start. Embedded
language formatting is off deliberately - the renderers build HTML in html
tagged templates and letting Prettier reflow those would change the markup
customers receive. docs/ is excluded because it is hand-wrapped prose, and shell
scripts have no Prettier parser.

ESLint covers the JavaScript: the two browser scripts, the build and check
scripts, and its own config. no-var is scoped out for the browser scripts on
purpose; they are plain ES5-shaped IIFEs and a hoisting difference would only
show up in a browser.

ESLint cannot lint the TypeScript here: typescript@7 is the native port with no
JavaScript compiler API, and typescript-eslint requires a 4.8-6.1 peer, so it
refuses to install and forcing it through would leave a parser that cannot read
the compiler. tsc --noEmit stays the TypeScript gate; README records the two
ways to change that.

lint and format:check now run in deploy.sh, and VS Code formats on save with
Prettier and surfaces ESLint findings.
2026-09-22 23:15:08 +05:30

102 lines
3.6 KiB
TypeScript

import { strict as assert } from 'node:assert';
import { describe, it } from 'node:test';
import {
LoginThrottle,
createSessionToken,
hashPassword,
verifyPassword,
verifySessionToken,
} from '../src/auth.ts';
const NOW = new Date('2026-05-01T06:00:00.000Z');
describe('the admin password', () => {
it('verifies a correct password and rejects a wrong one', async () => {
const stored = await hashPassword('correct horse battery staple');
assert.match(stored, /^scrypt\$32768\$8\$1\$/);
assert.equal(await verifyPassword('correct horse battery staple', stored), true);
assert.equal(await verifyPassword('Correct horse battery staple', stored), false);
assert.equal(await verifyPassword('', stored), false);
});
it('salts every hash, so the same password never produces the same string', async () => {
const first = await hashPassword('same password');
const second = await hashPassword('same password');
assert.notEqual(first, second);
assert.equal(await verifyPassword('same password', first), true);
assert.equal(await verifyPassword('same password', second), true);
});
it('rejects malformed stored values instead of throwing', async () => {
for (const stored of [
'',
'plaintext',
'scrypt$1$2$3',
'bcrypt$1$2$3$4$5',
'scrypt$x$8$1$aa$bb',
]) {
assert.equal(await verifyPassword('anything', stored), false);
}
});
});
describe('the session token', () => {
const secret = 'a-long-random-secret';
it('accepts a fresh token', () => {
const token = createSessionToken(secret, 60_000, NOW);
assert.equal(verifySessionToken(token, secret, NOW), true);
assert.equal(verifySessionToken(token, secret, new Date(NOW.getTime() + 59_000)), true);
});
it('rejects an expired token', () => {
const token = createSessionToken(secret, 60_000, NOW);
assert.equal(verifySessionToken(token, secret, new Date(NOW.getTime() + 61_000)), false);
});
it('rejects a token signed with another secret, a tampered expiry, and rubbish', () => {
const token = createSessionToken(secret, 60_000, NOW);
assert.equal(verifySessionToken(token, 'another-secret', NOW), false);
const [, signature] = token.split('.');
assert.equal(verifySessionToken(`${NOW.getTime() + 999_999}.${signature}`, secret, NOW), false);
assert.equal(verifySessionToken(undefined, secret, NOW), false);
assert.equal(verifySessionToken('', secret, NOW), false);
assert.equal(verifySessionToken('no-dot', secret, NOW), false);
assert.equal(verifySessionToken(`.${signature}`, secret, NOW), false);
});
});
describe('the login throttle', () => {
it('blocks the sixth failure inside the window and reports how long to wait', () => {
let time = NOW.getTime();
const throttle = new LoginThrottle({
maxFailures: 5,
windowMs: 60_000,
now: () => new Date(time),
});
for (let attempt = 0; attempt < 5; attempt += 1) {
assert.equal(throttle.check('1.2.3.4').allowed, true);
throttle.recordFailure('1.2.3.4');
}
const blocked = throttle.check('1.2.3.4');
assert.equal(blocked.allowed, false);
assert.ok(blocked.retryAfterSeconds > 0);
assert.equal(throttle.check('5.6.7.8').allowed, true);
time += 61_000;
assert.equal(throttle.check('1.2.3.4').allowed, true);
});
it('clears failures on a successful login', () => {
const throttle = new LoginThrottle({ maxFailures: 2, windowMs: 60_000, now: () => NOW });
throttle.recordFailure('ip');
throttle.recordFailure('ip');
assert.equal(throttle.check('ip').allowed, false);
throttle.reset('ip');
assert.equal(throttle.check('ip').allowed, true);
});
});