Files
ApiCore/ApiCore-Backend/test/authorization-coverage.e2e-spec.ts
T
Nick SzaunigandClaude Sonnet 5 0284077d3b feat: RBAC Phase 2e - Verwaltung auf @RequirePermission umgestellt, Coverage-Test aktiviert
Users, Webhooks, Automation, Import, AuditLog: die klassenweiten
@Roles(Role.ADMIN)-Blankets (die gefaehrlichsten der Migration, da sie
JEDE Route der Klasse abdecken) durch pro-Route @RequirePermission
ersetzt. Effektiver Zugriff unveraendert: ADMIN besitzt weiterhin alle
betroffenen Permission-Keys, jede andere Rolle bleibt ausgeschlossen.

Damit ist kein @Roles(...) mehr in der Codebase in Benutzung - alle
23 Controller laufen jetzt vollstaendig ueber @RequirePermission.
test/authorization-coverage.e2e-spec.ts (bisher it.skip mit TODO fuer
"nach Phase 2") ist jetzt scharf geschaltet und bestaetigt: 0 Routen
ohne Rechte-Pruefung, 0 ungueltige Permission-Keys.

Verifiziert: tsc, lint, build, unit (222/222), volle e2e-Suite
159/159 gruen - keine uebersprungenen Tests mehr.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-26 07:13:04 +02:00

98 lines
3.4 KiB
TypeScript

import { Test } from '@nestjs/testing';
import {
DiscoveryModule,
DiscoveryService,
MetadataScanner,
Reflector,
} from '@nestjs/core';
import { PATH_METADATA } from '@nestjs/common/constants';
import { AppModule } from '../src/app.module';
import { IS_PUBLIC_KEY } from '../src/auth/decorators/public.decorator';
import { ROLES_KEY } from '../src/auth/decorators/roles.decorator';
import { REQUIRE_PERMISSION_KEY } from '../src/auth/decorators/require-permission.decorator';
import { PERMISSION_CATALOG } from '../src/auth/permissions/permission-catalog';
// Routes intentionally left without a role/permission check: they act on the
// caller's own data (notifications) or are the auth bootstrap endpoint that
// every authenticated user must be able to reach regardless of permissions.
const ALLOWLIST = new Set([
'NotificationsController.findAll',
'NotificationsController.markRead',
'AuthController.me',
]);
describe('Authorization coverage (e2e)', () => {
it('every non-public route requires a role or a catalog-valid permission', async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule, DiscoveryModule],
}).compile();
const discovery = moduleRef.get(DiscoveryService);
const reflector = moduleRef.get(Reflector);
const scanner = moduleRef.get(MetadataScanner);
const catalogKeys = new Set<string>(
PERMISSION_CATALOG.map((entry) => entry.key),
);
const uncoveredRoutes: string[] = [];
const invalidPermissionKeys: string[] = [];
for (const wrapper of discovery.getControllers()) {
const instance = wrapper.instance as object | undefined;
if (!instance) {
continue;
}
const constructor = instance.constructor;
const controllerName = constructor.name;
const prototype = Object.getPrototypeOf(instance) as object;
const isPublicClass =
reflector.get<boolean>(IS_PUBLIC_KEY, constructor) === true;
for (const methodName of scanner.getAllMethodNames(prototype)) {
const handler = (prototype as Record<string, () => unknown>)[
methodName
];
const isRouteHandler =
Reflect.getMetadata(PATH_METADATA, handler) !== undefined;
if (!isRouteHandler) {
continue;
}
const routeKey = `${controllerName}.${methodName}`;
const isPublicMethod =
reflector.get<boolean>(IS_PUBLIC_KEY, handler) === true;
if (isPublicClass || isPublicMethod || ALLOWLIST.has(routeKey)) {
continue;
}
const roles = reflector.getAllAndOverride<unknown[]>(ROLES_KEY, [
handler,
constructor,
]);
const permissions = reflector.getAllAndOverride<string[]>(
REQUIRE_PERMISSION_KEY,
[handler, constructor],
);
const hasRoles = Array.isArray(roles) && roles.length > 0;
const hasPermissions =
Array.isArray(permissions) && permissions.length > 0;
if (!hasRoles && !hasPermissions) {
uncoveredRoutes.push(routeKey);
}
if (hasPermissions) {
for (const key of permissions) {
if (!catalogKeys.has(key)) {
invalidPermissionKeys.push(`${routeKey} -> ${key}`);
}
}
}
}
}
expect(uncoveredRoutes).toEqual([]);
expect(invalidPermissionKeys).toEqual([]);
await moduleRef.close();
});
});