TASK22019: phase 13 harden delete and involvement guards

This commit is contained in:
2026-03-13 13:02:49 +00:00
parent 0ca63d0228
commit 846cba1645
5 changed files with 231 additions and 10 deletions
+197
View File
@@ -0,0 +1,197 @@
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const assert = require("assert");
const rootDir = path.resolve(__dirname, "..", "..");
const loadModule = (relativePath, injected = {}) => {
const filePath = path.join(rootDir, relativePath);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
source = source.replace(
/export default async function\s+(\w+)\s*\(/,
"async function $1("
);
source = source.replace(/export const\s+/g, "const ");
source = source.replace(
/export default\s+(\w+);/g,
"module.exports.default = $1;"
);
source +=
'\nif (typeof ApiProxy !== "undefined" && !module.exports.default) module.exports.default = ApiProxy;\n';
const context = {
module: { exports: {} },
exports: {},
require,
process,
console: { log: () => {}, error: () => {} },
...injected
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const createNextConnectMock = () => {
const router = {
handler: null,
use: () => {},
get(fn) {
this.handler = fn;
}
};
return () => router;
};
const createRes = () => {
const state = { statusCode: null, jsonBody: undefined };
return {
state,
status(code) {
state.statusCode = code;
return this;
},
json(payload) {
state.jsonBody = payload;
return payload;
}
};
};
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
test("deleteblobcase rejects missing hash with 400", async () => {
const calls = [];
const mod = loadModule("pages/api/file/deleteblobcase.js", {
hashAPIPath: () => "&hash=expected",
deleteBlobCase: async (...args) => {
calls.push(args);
return true;
},
nextConnect: createNextConnectMock(),
middleware: () => {}
});
const req = { query: { container: "c1", casefolderID: "case-1" } };
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(calls.length, 0);
});
test("deleteblobrep rejects missing hash with 400", async () => {
const calls = [];
const mod = loadModule("pages/api/file/deleteblobrep.js", {
hashAPIPath: () => "&hash=expected",
deleteBlobRep: async (...args) => {
calls.push(args);
return true;
},
nextConnect: createNextConnectMock(),
middleware: () => {}
});
const req = {
query: { container: "c1", casefolderID: "case-1", repfile: "r.pdf" }
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(calls.length, 0);
});
test("createcaseinvolvement_api rejects missing required body values with 400", async () => {
const mod = loadModule("pages/api/file/createcaseinvolvement_api.js", {
getToken: async () => ({ access_token: "token" }),
axios: async () => ({ data: { ok: true } }),
consoleLogger: () => {}
});
const req = { body: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
});
test("createrepinvolvement_api rejects missing required body values with 400", async () => {
const mod = loadModule("pages/api/file/createrepinvolvement_api.js", {
getToken: async () => ({ access_token: "token" }),
axios: async () => ({ data: { ok: true } }),
consoleLogger: () => {}
});
const req = { body: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
});
test("deleteblobcase valid hash returns 200", async () => {
const mod = loadModule("pages/api/file/deleteblobcase.js", {
hashAPIPath: () => "&hash=expected",
deleteBlobCase: async () => ({ ok: true }),
nextConnect: createNextConnectMock(),
middleware: () => {}
});
const req = {
query: { container: "c1", casefolderID: "case-1", hash: "expected" }
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 200);
});
test("createcaseinvolvement_api with valid body returns 200", async () => {
const mod = loadModule("pages/api/file/createcaseinvolvement_api.js", {
getToken: async () => ({ access_token: "token" }),
CryptoJS: {
HmacSHA256: () => ({ toString: () => "signed" }),
enc: { Hex: { parse: () => "" } }
},
axios: async () => ({ data: { ok: true } }),
consoleLogger: () => {}
});
const req = { body: { contactid: "c1", incidentid: "i1" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 200);
});
test("createrepinvolvement_api with valid body returns 200", async () => {
const mod = loadModule("pages/api/file/createrepinvolvement_api.js", {
getToken: async () => ({ access_token: "token" }),
CryptoJS: {
HmacSHA256: () => ({ toString: () => "signed" }),
enc: { Hex: { parse: () => "" } }
},
axios: async () => ({ data: { ok: true } }),
consoleLogger: () => {}
});
const req = { body: { contactid: "c1", incidentid: "i1" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 200);
});
const run = async () => {
let passed = 0;
for (const t of tests) {
await t.fn();
passed += 1;
}
console.log(
`Phase 13 behavioural tests passed (${passed}/${tests.length}).`
);
};
run().catch((error) => {
console.error(error);
process.exit(1);
});