dc0c5fd289
- Add rate limiting (5/hr public, 100/min admin endpoints) - Add honeypot bot protection on public POST - Add audit logging for status changes and deletions - Add input validation (email, phone, date, length limits) - Fix missing auth on private event GET endpoints - Add audit_log table to schema - Fix failing tests (auth.test, rooms-route.test)
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { authenticateUser, createUser, hashPassword, verifyPassword } from '@/lib/auth';
|
|
|
|
const { query } = vi.hoisted(() => ({ query: vi.fn() }));
|
|
|
|
vi.mock('@/lib/db', () => ({
|
|
db: {
|
|
query,
|
|
},
|
|
}));
|
|
|
|
describe('auth helpers', () => {
|
|
beforeEach(() => {
|
|
query.mockReset();
|
|
});
|
|
|
|
it('hashes passwords before storing a user', async () => {
|
|
query.mockResolvedValueOnce({ rows: [{ id: 1, username: 'alice', role: 'user' }] });
|
|
|
|
const user = await createUser('alice', 's3cret', 'user');
|
|
|
|
expect(user).toEqual({ id: 1, username: 'alice', role: 'user' });
|
|
expect(query).toHaveBeenCalledTimes(1);
|
|
|
|
const [, params] = query.mock.calls[0];
|
|
expect(params[0]).toBe('alice');
|
|
expect(params[1]).not.toBe('s3cret');
|
|
expect(await verifyPassword('s3cret', params[1] as string)).toBe(true);
|
|
});
|
|
|
|
it('authenticates a user with the stored password hash', async () => {
|
|
const passwordHash = await hashPassword('secret-password');
|
|
query.mockResolvedValueOnce({
|
|
rows: [{ id: 2, username: 'bob', password_hash: passwordHash, role: 'admin', first_name: null, last_name: null, preferred_language: 'es', terms_accepted_at: null }],
|
|
});
|
|
|
|
await expect(authenticateUser('bob', 'secret-password')).resolves.toEqual({
|
|
id: 2,
|
|
username: 'bob',
|
|
role: 'admin',
|
|
first_name: null,
|
|
last_name: null,
|
|
preferred_language: 'es',
|
|
terms_accepted_at: null,
|
|
});
|
|
});
|
|
|
|
it('rejects an invalid password', async () => {
|
|
const passwordHash = await hashPassword('secret-password');
|
|
query.mockResolvedValueOnce({
|
|
rows: [{ id: 2, username: 'bob', password_hash: passwordHash, role: 'admin' }],
|
|
});
|
|
|
|
await expect(authenticateUser('bob', 'wrong-password')).resolves.toBeNull();
|
|
});
|
|
});
|