password.test.js (1307B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import { hashPassword, verifyDummyPassword, verifyPassword } from '../src/lib/password.js'; 4 5 test('password hashes verify and reject the wrong password', async () => { 6 const encoded = await hashPassword('correct horse battery staple'); 7 assert.match(encoded, /^scrypt\$/); 8 assert.equal(await verifyPassword('correct horse battery staple', encoded), true); 9 assert.equal(await verifyPassword('wrong', encoded), false); 10 }); 11 12 test('the same password produces different hashes', async () => { 13 const first = await hashPassword('same password'); 14 const second = await hashPassword('same password'); 15 assert.notEqual(first, second); 16 assert.equal(await verifyPassword('same password', first), true); 17 assert.equal(await verifyPassword('same password', second), true); 18 }); 19 20 test('malformed hashes and oversized passwords fail closed', async () => { 21 assert.equal(await verifyPassword('password', 'not-a-hash'), false); 22 assert.equal(await verifyPassword('password', 'scrypt$1$2$3$bad$bad'), false); 23 assert.equal(await verifyPassword('x'.repeat(1025), await hashPassword('password')), false); 24 }); 25 26 test('the dummy hash rejects arbitrary passwords', async () => { 27 assert.equal(await verifyDummyPassword('anything'), false); 28 });