Resolves the launch-blocking 'TCG Vault vs Deck Hearth' inconsistency called out in AGENTS.md line 5 since project setup. Operator gate-0 decision: Deck Hearth wins. Two briefs applied serially. B1 (mechanical): 7-file display + comment sweep. B2 (infrastructure): Redis prefix rename in lib/rate-limit.js (5 prefixes, accept one-time counter reset), package.json + lockfile regen (STOP-on-churn confirmed only name lines changed), admin/alice/bob email rename in seed scripts + login pre-fill + NEW idempotent migration script scripts/migrations/2026-05-24-rename-admin-email.js. Risk 4 PRESERVE applied: test/lib/permission-middleware.test.js retains admin@tcgvault.com literal with 7-line architect-authored why comment (documents pre-fix-auth-bypass bug shape; preserves historical truth per project's gotcha-documentation convention). All 5 D-decisions ratified at gate-1 (Deck Hearth / deck-hearth / deckhearth / admin@deckhearth.com / full deckhearth Redis prefix). Local: lint 128 baseline (B1 + B2), vitest 21/21 (B1 + B2). CI all green: Playwright smoke 3/3 against rebranded preview in 1m4s, forbidden-cors-headers pass, forbidden-endpoints pass, Screenshot diff pass, Vercel deployment complete. Cross-validation lineage: 4th convoy where the same 3-test smoke spec defends auth surface through sweeping change (after PR #15 Layout default-user, PR #19 CORS, PR #20 rate-limit, now this PR #21 brand rename). OPERATOR POST-MERGE ACTION REQUIRED: run 'node scripts/migrations/2026-05-24-rename-admin-email.js' against prod Neon DB before next admin login (ordering: migration FIRST, then any subsequent setup-db invocation). Migration is ESM, idempotent, UNIQUE-collision-safe. PR #21 architect-commit50ce9ab, B1ac8c998, B21c18d21.
99 lines
3.5 KiB
JavaScript
99 lines
3.5 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: {} });
|
|
// Email literal is the OLD `admin@tcgvault.com` (pre-`pick-a-name`
|
|
// convoy, 2026-05-24) — preserved as the exact pre-fix-auth-bypass
|
|
// synthetic-admin shape this assertion locks against. The
|
|
// `.toBeNull()` check below is the strong contract; this soft check
|
|
// documents the historical bug. Do NOT update to
|
|
// `admin@deckhearth.com` — that would weaken the regression-lock to
|
|
// a shape that never actually existed.
|
|
expect(user).not.toEqual({
|
|
userId: 1,
|
|
email: 'admin@tcgvault.com',
|
|
role: 'admin',
|
|
});
|
|
expect(user).toBeNull();
|
|
});
|
|
});
|