Closes AGENTS.md gotcha #11 (well, the relevant half of it — "Testing: None yet" line in §6 is now stale). Installs vitest@^3.2.4 (single devDep, no UI / coverage / jsdom) and adds 16 unit tests across 3 files that lock in post-Brief-1/2/4 behavior: test/lib/auth-secret.test.js (3 tests) - JWT_SECRET exports the env value - JWT_TOKEN_TTL is canonical 24h - Module throws at load when JWT_SECRET is empty test/lib/permission-middleware.test.js (8 tests) - getUserFromRequest returns null for: missing header, non-Bearer scheme, malformed token, wrong-secret token, expired token, valid-token-no-user-row - Returns user object for valid token + user row - Brief 2 regression lock: does NOT return the synthetic admin shape { userId: 1, email: 'admin@tcgvault.com', role: 'admin' } when no Authorization header is present test/api/auth-utils.test.js (5 tests) - generateToken issues 24h JWT (exp - iat === 86400) - Payload includes userId, email, role - verifyToken round-trips valid tokens - Returns null for malformed / wrong-secret tokens CI: re-enabled the previously commented-out test: job in .github/workflows/ci.yml. Blocking (no || true wrapper) — vitest is the first runner in this repo and we want CI red on test regression. JWT_SECRET is set via a CI-only fake; production secret is unaffected. Rate-limit (Brief 4) coverage deferred to a future expand-auth-tests convoy per architect's call (R11). package.json has "type": "module" so vitest's default Vite-based transform handles .js ESM out of the box — no transform config needed. Convoy: fix-auth-bypass / Brief 5 (last brief) Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
3.1 KiB
JavaScript
92 lines
3.1 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
vi.mock('@vercel/postgres', () => ({ sql: vi.fn() }));
|
|
|
|
import { sql } from '@vercel/postgres';
|
|
import { JWT_SECRET } from '../../lib/auth-secret.js';
|
|
import { getUserFromRequest } from '../../lib/permission-middleware.js';
|
|
|
|
function makeToken(payload, opts = {}) {
|
|
return jwt.sign(payload, JWT_SECRET, { expiresIn: opts.expiresIn ?? '1h' });
|
|
}
|
|
|
|
describe('getUserFromRequest', () => {
|
|
beforeEach(() => {
|
|
sql.mockReset();
|
|
sql.mockResolvedValue({ rows: [] });
|
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
});
|
|
|
|
it('returns null when the Authorization header is missing', async () => {
|
|
const user = await getUserFromRequest({ headers: {} });
|
|
expect(user).toBeNull();
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when the Authorization header is not a Bearer scheme', async () => {
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: 'Basic foo' },
|
|
});
|
|
expect(user).toBeNull();
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when the token is malformed', async () => {
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: 'Bearer not-a-jwt' },
|
|
});
|
|
expect(user).toBeNull();
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when the token signature uses a wrong secret', async () => {
|
|
const token = jwt.sign({ userId: 1 }, 'other-secret', { expiresIn: '1h' });
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
expect(user).toBeNull();
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when the token is expired', async () => {
|
|
const token = makeToken({ userId: 1 }, { expiresIn: '-1s' });
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
expect(user).toBeNull();
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when the token is valid but no user row matches', async () => {
|
|
sql.mockResolvedValue({ rows: [] });
|
|
const token = makeToken({ userId: 42 });
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
expect(user).toBeNull();
|
|
expect(sql).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('returns the user object when the token is valid and the user row exists', async () => {
|
|
sql.mockResolvedValue({
|
|
rows: [{ id: 42, email: 'a@b.c', role: 'user' }],
|
|
});
|
|
const token = makeToken({ userId: 42 });
|
|
const user = await getUserFromRequest({
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
expect(user).toEqual({ userId: 42, email: 'a@b.c', role: 'user' });
|
|
expect(sql).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('does NOT return the synthetic admin shape when no Authorization header is present (Brief 2 regression lock)', async () => {
|
|
const user = await getUserFromRequest({ headers: {} });
|
|
expect(user).not.toEqual({
|
|
userId: 1,
|
|
email: 'admin@tcgvault.com',
|
|
role: 'admin',
|
|
});
|
|
expect(user).toBeNull();
|
|
});
|
|
});
|