Files
canina/backend/src/admin/legacy-orchestration.http.spec.ts
T

53 lines
2.5 KiB
TypeScript

import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { ApprovalsController } from './approvals.controller';
import { DepartmentTasksController } from './department-tasks.controller';
import { PrismaService } from '../prisma/prisma.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('retired legacy orchestration HTTP boundary', () => {
let app: INestApplication;
const dbAccess = jest.fn();
const prisma = new Proxy({}, {get: (_target, key) => {
// Nest inspects provider thenables and lifecycle hooks during setup.
if (["then", "onModuleInit", "onApplicationBootstrap", "onModuleDestroy", "beforeApplicationShutdown", "onApplicationShutdown"].includes(String(key))) return undefined;
dbAccess(key);
throw new Error('Persistence must not be accessed');
}});
const routes = [
['post', '/admin/approvals'],
['put', '/admin/approvals/missing/decide'],
['post', '/admin/department-tasks'],
['put', '/admin/department-tasks/missing/status'],
] as const;
beforeAll(async () => {
const module = await Test.createTestingModule({
controllers: [ApprovalsController, DepartmentTasksController],
providers: [{provide: PrismaService, useValue: prisma}, RolesGuard],
}).overrideGuard(JwtAuthGuard).useValue({canActivate(context: any) {
const req = context.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = {id: 'test-admin', role: req.headers.authorization};
return true;
}}).compile();
app = module.createNestApplication();
await app.init();
dbAccess.mockClear();
});
beforeEach(() => dbAccess.mockClear());
afterAll(async () => { if (app) await app.close(); });
it.each(routes)('%s %s returns explicit 410 for authorized callers before database access', async (method, path) => {
const response = await request(app.getHttpServer())[method](path)
.set('Authorization', 'Admin').send({decision:'APPROVED',status:'DONE'}).expect(410);
expect(response.body.message).toContain('retired');
expect(dbAccess).not.toHaveBeenCalled();
});
it.each(routes)('%s %s retains anonymous and customer authorization guards', async (method, path) => {
await request(app.getHttpServer())[method](path).send({}).expect(403);
await request(app.getHttpServer())[method](path).set('Authorization','CUSTOMER').send({}).expect(403);
expect(dbAccess).not.toHaveBeenCalled();
});
});