- Add lint, formatting, vet, and publish workflow checks - Run Go database tests with PostgreSQL and add Java tests - Make consumer sync opt-in; update docs and Go dependency
44 lines
2.1 KiB
JavaScript
44 lines
2.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
import { hashPassword, passwordHashNeedsUpgrade, verifyAndUpgradePasswordAsync, verifyPassword } from '../src/auth.js';
|
|
import { verifyTotp } from '../src/totp.js';
|
|
|
|
// The Go, Node and Python auth helpers load the same fixtures; see "description" in the file.
|
|
const contractUrl = new URL('../../../contract/auth-contract.json', import.meta.url);
|
|
const contract = existsSync(contractUrl) ? JSON.parse(readFileSync(contractUrl, 'utf8')) : null;
|
|
const skip = contract ? false : 'contract/auth-contract.json is not part of this checkout';
|
|
|
|
test('password hashes verify as the contract says for Node', { skip }, async () => {
|
|
const { plaintext, wrongPassword, hashes } = contract.password;
|
|
for (const entry of hashes) {
|
|
assert.equal(verifyPassword(plaintext, entry.hash), entry.verifies.node, entry.name);
|
|
assert.equal(verifyPassword(wrongPassword, entry.hash), false, `${entry.name}: wrong password`);
|
|
if ('node' in (entry.needsUpgrade ?? {})) {
|
|
assert.equal(passwordHashNeedsUpgrade(entry.hash), entry.needsUpgrade.node, `${entry.name}: needs upgrade`);
|
|
const { valid, upgradedHash } = await verifyAndUpgradePasswordAsync(plaintext, entry.hash);
|
|
assert.equal(valid, true, `${entry.name}: valid on upgrade`);
|
|
assert.equal(upgradedHash !== null, entry.needsUpgrade.node, `${entry.name}: upgraded hash`);
|
|
if (upgradedHash) assert.equal(verifyPassword(plaintext, upgradedHash), true);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('the password length contract holds for Node', { skip }, () => {
|
|
const { bytes, accepted } = contract.password.tooLongToHash;
|
|
let didHash = true;
|
|
try {
|
|
hashPassword('a'.repeat(bytes), { iterations: 1000 });
|
|
} catch {
|
|
didHash = false;
|
|
}
|
|
assert.equal(didHash, accepted.node);
|
|
});
|
|
|
|
test('TOTP verifies as the contract says', { skip }, () => {
|
|
for (const entry of contract.totp.cases) {
|
|
const secret = entry.secret ?? contract.totp.secret;
|
|
assert.equal(verifyTotp(secret, entry.code, entry.unix * 1000), entry.valid, entry.name);
|
|
}
|
|
});
|