Replaces the no-op fastify.authenticate stub and the never-implemented
identity/auth login with real bcrypt password verification, JWT session
issuance/verification (reusing the existing JWT_SECRET), and a Redis-backed
revocation denylist for logout. Adds requireRole('ADMIN') to admin-only
configuration writes across 002-009 that previously relied on a decorator
that never actually checked anything. Adds self-identity (GET /auth/me,
re-validated against live account state) and admin-provisioned accounts
(POST /admin/users).
Making the auth check genuinely reject invalid/missing tokens exposed that
~18 pre-existing integration test files called already-gated routes with no
Authorization header (safe against the old no-op stub, broken against a real
one) — fixed via a shared tests/helpers/auth.ts (loginAs/authHeader) and a
file-by-file pass, plus two related SLA-run cleanup races exposed once admin
setup calls in those files' own beforeAll blocks started actually succeeding.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
190 lines
6.3 KiB
TypeScript
190 lines
6.3 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { buildApp } from '@/app';
|
|
import { prismaClient } from '@/infrastructure/database';
|
|
import { FastifyInstance } from 'fastify';
|
|
import { loginAs, authHeader } from '../helpers/auth';
|
|
|
|
/** Covers specs/006-support-organization/quickstart.md Scenario 3 against a real Postgres. */
|
|
describe('Support organization — dynamic hierarchy (User Story 3)', () => {
|
|
let app: FastifyInstance;
|
|
let token: string;
|
|
const rootName = `Test Root ${Date.now()}`;
|
|
let rootId: string;
|
|
let childId: string;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildApp();
|
|
token = await loginAs(app, 'ADMIN');
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await prismaClient.hierarchyNode.deleteMany({
|
|
where: { name: { startsWith: 'Test' }, id: { in: [rootId, childId].filter(Boolean) } },
|
|
});
|
|
await prismaClient.auditLog.deleteMany({
|
|
where: { entityType: 'HierarchyNode', entityId: { in: [rootId, childId].filter(Boolean) } },
|
|
});
|
|
await app.close();
|
|
});
|
|
|
|
it('Scenario 3: parent/child creation, order, nonexistent parent rejected, cycle rejected, deactivation does not cascade', async () => {
|
|
const createRoot = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: { name: rootName, order: 0, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
expect(createRoot.statusCode).toBe(201);
|
|
rootId = createRoot.json().data.id;
|
|
|
|
const createChild = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: {
|
|
name: `${rootName} Child`,
|
|
parentId: rootId,
|
|
order: 0,
|
|
assignmentStrategy: 'ROUND_ROBIN',
|
|
},
|
|
});
|
|
expect(createChild.statusCode).toBe(201);
|
|
childId = createChild.json().data.id;
|
|
|
|
const children = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/hierarchy-nodes/${rootId}/children`,
|
|
headers: authHeader(token),
|
|
});
|
|
expect(children.json().data.map((n: { id: string }) => n.id)).toContain(childId);
|
|
|
|
const badParent = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: {
|
|
name: 'Orphan',
|
|
parentId: 'nonexistent-node-id',
|
|
order: 0,
|
|
assignmentStrategy: 'ROUND_ROBIN',
|
|
},
|
|
});
|
|
expect(badParent.statusCode).toBe(404);
|
|
|
|
const selfCycle = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/hierarchy-nodes/${childId}`,
|
|
headers: authHeader(token),
|
|
payload: {
|
|
name: `${rootName} Child`,
|
|
parentId: childId,
|
|
order: 0,
|
|
assignmentStrategy: 'ROUND_ROBIN',
|
|
},
|
|
});
|
|
expect(selfCycle.statusCode).toBe(400);
|
|
expect(selfCycle.json().error.code).toBe('CYCLE_DETECTED');
|
|
|
|
const transitiveCycle = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/hierarchy-nodes/${rootId}`,
|
|
headers: authHeader(token),
|
|
payload: { name: rootName, parentId: childId, order: 0, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
expect(transitiveCycle.statusCode).toBe(400);
|
|
expect(transitiveCycle.json().error.code).toBe('CYCLE_DETECTED');
|
|
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/hierarchy-nodes/${rootId}/deactivate`,
|
|
headers: authHeader(token),
|
|
});
|
|
const activeTree = await app.inject({
|
|
method: 'GET',
|
|
url: '/admin/hierarchy-nodes?active=true',
|
|
headers: authHeader(token),
|
|
});
|
|
const activeIds = activeTree.json().data.map((n: { id: string }) => n.id);
|
|
expect(activeIds).not.toContain(rootId);
|
|
|
|
const childAfterParentDeactivation = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/hierarchy-nodes/${childId}`,
|
|
headers: authHeader(token),
|
|
});
|
|
expect(childAfterParentDeactivation.json().data.active).toBe(true);
|
|
});
|
|
|
|
it('preserves sibling order under the same parent', async () => {
|
|
const parent = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: { name: `${rootName} Order Parent`, order: 0, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
const parentId = parent.json().data.id;
|
|
|
|
const second = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: { name: 'Second', parentId, order: 2, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
const first = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: { name: 'First', parentId, order: 1, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
|
|
const children = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/hierarchy-nodes/${parentId}/children`,
|
|
headers: authHeader(token),
|
|
});
|
|
const orderedIds = children.json().data.map((n: { id: string }) => n.id);
|
|
expect(orderedIds).toEqual([first.json().data.id, second.json().data.id]);
|
|
|
|
await prismaClient.hierarchyNode.deleteMany({
|
|
where: { id: { in: [parentId, first.json().data.id, second.json().data.id] } },
|
|
});
|
|
await prismaClient.auditLog.deleteMany({
|
|
where: {
|
|
entityType: 'HierarchyNode',
|
|
entityId: { in: [parentId, first.json().data.id, second.json().data.id] },
|
|
},
|
|
});
|
|
});
|
|
|
|
it('every create/edit/activate/deactivate writes exactly one audit log row', async () => {
|
|
const created = await app.inject({
|
|
method: 'POST',
|
|
url: '/admin/hierarchy-nodes',
|
|
headers: authHeader(token),
|
|
payload: { name: `${rootName} Audited`, order: 0, assignmentStrategy: 'ROUND_ROBIN' },
|
|
});
|
|
const nodeId = created.json().data.id;
|
|
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/hierarchy-nodes/${nodeId}/deactivate`,
|
|
headers: authHeader(token),
|
|
});
|
|
await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/hierarchy-nodes/${nodeId}/activate`,
|
|
headers: authHeader(token),
|
|
});
|
|
|
|
const auditRows = await prismaClient.auditLog.findMany({
|
|
where: { entityType: 'HierarchyNode', entityId: nodeId },
|
|
});
|
|
expect(auditRows).toHaveLength(3); // created, deactivated, activated
|
|
|
|
await prismaClient.hierarchyNode.deleteMany({ where: { id: nodeId } });
|
|
await prismaClient.auditLog.deleteMany({
|
|
where: { entityType: 'HierarchyNode', entityId: nodeId },
|
|
});
|
|
});
|
|
});
|