Files
pedwfrontend/tests/phase15/service-behaviour.test.cjs
T

246 lines
6.7 KiB
JavaScript

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: () => {},
info: () => {},
warn: () => {},
error: () => {}
},
...injected
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const createNextConnectMock = () => {
const router = {
handler: null,
use: () => {},
get(fn) {
this.handler = fn;
},
post(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("upload rejects missing hash with 400", async () => {
const calls = [];
const nextConnect = createNextConnectMock();
const mod = loadModule("pages/api/file/upload.js", {
hashAPIPath: () => "?hash=expected",
createBlob: async (...args) => {
calls.push(args);
return { ok: true };
},
createRepBlob: async (...args) => {
calls.push(args);
return { ok: true };
},
uploadFile: async () => {},
nextConnect,
middleware: () => {}
});
const req = {
query: {},
body: { appealData: {}, containerID: ["c1"], casefolderID: ["case-1"] },
files: {}
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(calls.length, 0);
});
test("uploadsinglefile rejects missing hash with 400", async () => {
const uploadCalls = [];
const nextConnect = createNextConnectMock();
const mod = loadModule("pages/api/file/uploadsinglefile.js", {
hashAPIPath: () => "?hash=expected",
uploadSingleFile: async (...args) => {
uploadCalls.push(args);
return { ok: true };
},
consoleLogger: () => {},
fileTypeFromBuffer: async () => ({ mime: "application/pdf" }),
fs: { readFileSync: () => Buffer.from("file") },
path: { basename: (value) => value },
nextConnect,
middleware: () => {}
});
const req = {
query: {},
body: { containerID: ["c1"], casefolderID: ["case-1"] },
files: {}
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(uploadCalls.length, 0);
});
test("createappealcompletemessage_api rejects missing hash with 400", async () => {
const calls = [];
const nextConnect = createNextConnectMock();
const mod = loadModule(
"pages/api/file/createappealcompletemessage_api.js",
{
hashAPIPath: () => "&hash=expected",
getProgressBlobs: async (...args) => {
calls.push(args);
return { path: "p1" };
},
downloadProgressFile: async () => ({}),
createBlob: async () => ({}),
getCaseBlob: async () => ({}),
createCaseCompleteMessage: async () => ({}),
updateAccount: async () => ({}),
consoleLogger: () => {},
_: { isEmpty: (value) => !value },
nextConnect,
middleware: () => {}
}
);
const req = {
query: { container: "c1", tempcaseref: "tmp-1", inv: 846040001 }
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(calls.length, 0);
});
test("setupcontainer rejects missing hash with 400", async () => {
const createCalls = [];
const nextConnect = createNextConnectMock();
const mod = loadModule("pages/api/file/setupcontainer.js", {
hashAPIPath: () => "&hash=expected",
createContainer: async (...args) => {
createCalls.push(args);
return { ok: true };
},
createContainerSas: async () => {},
getContainers: async () => {},
getBlobs: async () => {},
uploadFile: async () => {},
consoleLogger: () => {},
nextConnect,
middleware: () => {}
});
const req = { query: { ident: "container-1" } };
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(createCalls.length, 0);
});
test("setupcontainer valid hash returns 200 with expected response shape", async () => {
const nextConnect = createNextConnectMock();
const mod = loadModule("pages/api/file/setupcontainer.js", {
hashAPIPath: () => "&hash=expected",
createContainer: async () => ({ container: "container-1" }),
createContainerSas: async () => {},
getContainers: async () => {},
getBlobs: async () => {},
uploadFile: async () => {},
consoleLogger: () => {},
nextConnect,
middleware: () => {}
});
const req = { query: { ident: "container-1", hash: "expected" } };
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 200);
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
data: "success",
output: { container: "container-1" }
});
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 15 behavioural tests passed (${passed}/${tests.length}).`
);
};
run().catch((error) => {
console.error(error);
process.exit(1);
});