forked from Yara724/api
Enforce claim beneficiary access and retire unused legacy APIs
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import "reflect-metadata";
|
||||
import { RequestMethod } from "@nestjs/common";
|
||||
import {
|
||||
GUARDS_METADATA,
|
||||
METHOD_METADATA,
|
||||
PATH_METADATA,
|
||||
} from "@nestjs/common/constants";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
type RouteContract = {
|
||||
controller: string;
|
||||
method: string;
|
||||
path: string;
|
||||
guards: string[];
|
||||
roles: string[];
|
||||
};
|
||||
function controllerFiles(dir: string): string[] {
|
||||
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
||||
const file = join(dir, entry.name);
|
||||
return entry.isDirectory()
|
||||
? controllerFiles(file)
|
||||
: entry.name.endsWith(".controller.ts")
|
||||
? [file]
|
||||
: [];
|
||||
});
|
||||
}
|
||||
|
||||
describe("retained API surface after legacy retirement", () => {
|
||||
it("keeps every retained route, authentication guard and role contract", () => {
|
||||
// Captured from the pre-cleanup controllers, excluding explicitly retired routes.
|
||||
// This is the public routing/access contract, not a snapshot of service implementation.
|
||||
const expected: RouteContract[] = JSON.parse(
|
||||
readFileSync(
|
||||
join(__dirname, "../../test/contracts/api-routes.json"),
|
||||
"utf8",
|
||||
),
|
||||
);
|
||||
const actual: RouteContract[] = [];
|
||||
for (const file of controllerFiles(join(__dirname, ".."))) {
|
||||
const exports = require(file) as Record<string, any>;
|
||||
for (const controller of Object.values(exports)) {
|
||||
if (typeof controller !== "function") continue;
|
||||
const prefix = Reflect.getMetadata(PATH_METADATA, controller);
|
||||
if (prefix === undefined) continue;
|
||||
for (const name of Object.getOwnPropertyNames(controller.prototype)) {
|
||||
if (name === "constructor") continue;
|
||||
const handler = controller.prototype[name];
|
||||
const method = Reflect.getMetadata(METHOD_METADATA, handler);
|
||||
if (method === undefined) continue;
|
||||
const route = Reflect.getMetadata(PATH_METADATA, handler);
|
||||
actual.push({
|
||||
controller: controller.name,
|
||||
method: RequestMethod[method],
|
||||
path:
|
||||
("/" + prefix + "/" + route)
|
||||
.replace(/\/+/g, "/")
|
||||
.replace(/\/$/, "") || "/",
|
||||
guards: [
|
||||
...(Reflect.getMetadata(GUARDS_METADATA, controller) || []),
|
||||
...(Reflect.getMetadata(GUARDS_METADATA, handler) || []),
|
||||
].map((guard) => guard.name),
|
||||
roles:
|
||||
Reflect.getMetadata("role", handler) ||
|
||||
Reflect.getMetadata("role", controller) ||
|
||||
[],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
actual.sort((a, b) =>
|
||||
(a.method + " " + a.path + " " + a.controller).localeCompare(
|
||||
b.method + " " + b.path + " " + b.controller,
|
||||
),
|
||||
);
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user