Files
compor-academy/tests/auth.test.ts
Cauê Faleiros 50905eefd7
All checks were successful
CI / Validate frontend and API (push) Successful in 3m5s
CI / Build and publish Docker images (push) Successful in 1m42s
feat: add 2FA, secure cookies and publishing tools
2026-11-06 10:29:37 -03:00

34 lines
1.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { createRawToken, hashToken } from '../server/src/auth/account-tokens';
import { hashPassword, verifyPassword } from '../server/src/auth/passwords';
describe('account tokens', () => {
it('produces unique URL-safe tokens', () => {
const first = createRawToken();
const second = createRawToken();
expect(first).not.toBe(second);
expect(first).toMatch(/^[A-Za-z0-9_-]+$/);
expect(first.length).toBeGreaterThan(30);
});
it('hashes deterministically without exposing the token', () => {
const token = 'example-token';
expect(hashToken(token)).toBe(hashToken(token));
expect(hashToken(token)).not.toBe(token);
expect(hashToken(token)).toMatch(/^[a-f0-9]{64}$/);
});
});
describe('password hashing', () => {
it('accepts the correct password and rejects others', async () => {
const hash = await hashPassword('Compor@123');
expect(hash).not.toContain('Compor@123');
await expect(verifyPassword('Compor@123', hash)).resolves.toBe(true);
await expect(verifyPassword('wrong-password', hash)).resolves.toBe(false);
});
it('rejects malformed hashes', async () => {
await expect(verifyPassword('Compor@123', 'not-a-hash')).resolves.toBe(false);
});
});