Files
crosscode-platform/apps/events/backend/tests/passwords.test.js
T
joshuaandClaude Opus 5.5 3f59164ab2 WIP 2.0.0: v1 removed; Argon2id passwords; node-fetch gone
- /api (v1) answers 410 API_V1_REMOVED, except provider webhooks
  (/api/webhooks/*, /api/whatsapp/webhook) and links in sent messages
  (308s, one-click POST). 43 v1 route files and deprecation.ts removed;
  /docs redirects to /docs/v2; API_DOCUMENTATION.md points there.
- Read-only and impersonation lists name v2 paths only. The campus
  scope route test now runs over v2 routes (v2CampusScope.test.ts).
- Passwords hashed with Argon2id (@node-rs/argon2); bcrypt hashes still
  sign in and are re-hashed at the next sign-in.
- e2e on /api/v2 only (v2() helper); Playwright waits on /api/v2.
- MIGRATING-TO-2.md with the full v1 -> v2 table.
- Two documented response shapes corrected.

Backend 690 tests and e2e 102/102 (PayFast sandbox skipped) pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 16:35:06 +02:00

38 lines
1.7 KiB
JavaScript

// 2.0: new passwords are hashed with Argon2id; bcrypt hashes from before still work and are
// re-hashed the next time the person signs in (services/auth.ts logIn).
const bcrypt = require('bcryptjs');
const { hashPassword, comparePassword, needsRehash } = require('../src/config/auth');
describe('password hashing', () => {
test('new hashes are Argon2id and check out', async () => {
const hash = await hashPassword('Correct-horse-1');
expect(hash.startsWith('$argon2id$')).toBe(true);
expect(await comparePassword('Correct-horse-1', hash)).toBe(true);
expect(await comparePassword('wrong', hash)).toBe(false);
expect(needsRehash(hash)).toBe(false);
});
test('two hashes of the same password differ (salted)', async () => {
expect(await hashPassword('same')).not.toBe(await hashPassword('same'));
});
test('old bcrypt hashes still sign in, and are marked for re-hashing', async () => {
const old = await bcrypt.hash('Legacy-pass-1', 10);
expect(await comparePassword('Legacy-pass-1', old)).toBe(true);
expect(await comparePassword('nope', old)).toBe(false);
expect(needsRehash(old)).toBe(true);
});
test('anything else never matches', async () => {
for (const stored of ['', null, undefined, 'plain-text', '$argon2id$broken']) {
expect(await comparePassword('plain-text', stored)).toBe(false);
}
expect(await comparePassword(undefined, await hashPassword('x'))).toBe(false);
});
test('weaker Argon2 settings are re-hashed too', () => {
expect(needsRehash('$argon2id$v=19$m=4096,t=3,p=1$c2FsdHNhbHQ$aGFzaGhhc2g')).toBe(true);
expect(needsRehash('$argon2i$v=19$m=19456,t=2,p=1$c2FsdHNhbHQ$aGFzaGhhc2g')).toBe(true);
});
});