244 lines
6.7 KiB
JavaScript
244 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("deletewatchedcases_api rejects missing watchedCaseID with 400", async () => {
|
|
let axiosCalls = 0;
|
|
const mod = loadModule("pages/api/endpoint/deletewatchedcases_api.js", {
|
|
axios: async () => {
|
|
axiosCalls += 1;
|
|
return { data: {} };
|
|
},
|
|
getToken: async () => ({ access_token: "token" })
|
|
});
|
|
|
|
const req = { query: {} };
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
assert.strictEqual(axiosCalls, 0);
|
|
});
|
|
|
|
test("getaccounts_api rejects missing emailAddress with 400", async () => {
|
|
const mod = loadModule("pages/api/endpoint/getaccounts_api.js", {
|
|
_: { isEmpty: () => true },
|
|
axios: { get: async () => ({ data: { value: [] } }) },
|
|
getToken: async () => ({ access_token: "token" }),
|
|
hashAPIPath: () => "&hash=expected",
|
|
azureHeaders: () => ({})
|
|
});
|
|
|
|
const req = { query: {} };
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
});
|
|
|
|
test("getbasicsearchpaged_api rejects missing searchString with 400", async () => {
|
|
const mod = loadModule("pages/api/endpoint/getbasicsearchpaged_api.js", {
|
|
axios: { get: async () => ({ data: { value: [] } }) },
|
|
getToken: async () => ({ access_token: "token" }),
|
|
hashAPIPath: () => "&hash=expected",
|
|
azureHeadersPagedCustom: () => ({})
|
|
});
|
|
|
|
const req = { query: { pageNumber: "1" } };
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
});
|
|
|
|
test("getappealpdfdocuments_api rejects missing incidentid with 400", async () => {
|
|
const mod = loadModule("pages/api/endpoint/getappealpdfdocuments_api.js", {
|
|
axios: { get: async () => ({ data: { value: [] } }) },
|
|
getToken: async () => ({ access_token: "token" }),
|
|
hashAPIPath: () => "&hash=expected",
|
|
azureHeaders: () => ({})
|
|
});
|
|
|
|
const req = { query: {} };
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
});
|
|
|
|
test("createcase_api rejects missing required query params with 400", async () => {
|
|
const blobCalls = [];
|
|
const mod = loadModule("pages/api/file/createcase_api.js", {
|
|
_: { isEmpty: () => true },
|
|
getToken: async () => ({ access_token: "token" }),
|
|
getTempCaseRef: () => "TMP-1",
|
|
uuidv4: () => "uuid-1",
|
|
getCaseBlob: (...args) => blobCalls.push(args),
|
|
createBlob: (...args) => blobCalls.push(args)
|
|
});
|
|
|
|
const req = { query: {}, body: {} };
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
assert.strictEqual(blobCalls.length, 0);
|
|
});
|
|
|
|
test("updatecase_api rejects missing required inputs with 400", async () => {
|
|
const mod = loadModule("pages/api/file/updatecase_api.js", {
|
|
getToken: async () => ({ access_token: "token" }),
|
|
axios: async () => ({ data: { ok: true } })
|
|
});
|
|
|
|
const req = {
|
|
query: { incident: "inc-1", appealObj: "appeal-1" },
|
|
body: { title: "x" }
|
|
};
|
|
const res = createRes();
|
|
await mod.default(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 400);
|
|
});
|
|
|
|
test("deleteblob valid hash still returns 200", async () => {
|
|
const mod = loadModule("pages/api/file/deleteblob.js", {
|
|
hashAPIPath: () => "&hash=expected",
|
|
deleteBlob: async () => true,
|
|
nextConnect: createNextConnectMock(),
|
|
middleware: () => {}
|
|
});
|
|
|
|
const req = {
|
|
query: {
|
|
container: "c1",
|
|
casefolderID: "case-1",
|
|
blobname: "f.pdf",
|
|
hash: "expected"
|
|
}
|
|
};
|
|
const res = createRes();
|
|
await mod.default.handler(req, res);
|
|
|
|
assert.strictEqual(res.state.statusCode, 200);
|
|
});
|
|
|
|
test("getprogressobjblob valid hash still returns 200", async () => {
|
|
const mod = loadModule("pages/api/file/getprogressobjblob.js", {
|
|
hashAPIPath: () => "&hash=expected",
|
|
getProgressBlobs: async () => ({ path: "p1" }),
|
|
downloadProgressFile: async () => ({ filesList: [] }),
|
|
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);
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
|
|
filesList: []
|
|
});
|
|
});
|
|
|
|
const run = async () => {
|
|
let passed = 0;
|
|
|
|
for (const currentTest of tests) {
|
|
await currentTest.fn();
|
|
passed += 1;
|
|
}
|
|
|
|
console.log(
|
|
`Phase 18 behavioural tests passed (${passed}/${tests.length}).`
|
|
);
|
|
};
|
|
|
|
run().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|