126 lines
3.6 KiB
JavaScript
126 lines
3.6 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
const assert = require("assert");
|
|
|
|
const rootDir = path.resolve(__dirname, "..", "..");
|
|
|
|
const loadAzureStorageHelperModule = (injected = {}) => {
|
|
const filePath = path.join(rootDir, "actions", "azurestorage.js");
|
|
const source = fs.readFileSync(filePath, "utf8");
|
|
|
|
const start = source.indexOf("const buildDownloadBlobQueryPath =");
|
|
const end = source.indexOf("export const createContainerSas =");
|
|
|
|
if (start < 0 || end < 0 || end <= start) {
|
|
throw new Error("Unable to isolate azurestorage helper function block");
|
|
}
|
|
|
|
let helperSource = source.slice(start, end);
|
|
helperSource +=
|
|
"\nmodule.exports = { buildDownloadBlobQueryPath, buildDeleteBlobQueryPath, buildGetBlobListQueryPath, buildHashMetadataPaths };\n";
|
|
|
|
const context = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
require,
|
|
encodeURIComponent,
|
|
hashAPIPath: (route) => `HASH(${route})`,
|
|
...injected
|
|
};
|
|
|
|
vm.runInNewContext(helperSource, context, { filename: filePath });
|
|
return context.module.exports;
|
|
};
|
|
|
|
const tests = [];
|
|
const test = (name, fn) => tests.push({ name, fn });
|
|
|
|
test("azurestorage helper builds download path with default encoding", () => {
|
|
const mod = loadAzureStorageHelperModule();
|
|
|
|
const pathResult = mod.buildDownloadBlobQueryPath({
|
|
containerName: "alpha",
|
|
casefolderID: "A/B",
|
|
blobname: "my doc.pdf"
|
|
});
|
|
|
|
assert.strictEqual(
|
|
pathResult,
|
|
"/api/file/downloadblob?container=alpha&casefolderID=A%2FB&blobname=my%20doc.pdf"
|
|
);
|
|
});
|
|
|
|
test("azurestorage helper preserves raw values when encoding disabled", () => {
|
|
const mod = loadAzureStorageHelperModule();
|
|
|
|
const deletePath = mod.buildDeleteBlobQueryPath({
|
|
containerName: "alpha",
|
|
casefolderID: "A/B",
|
|
blobname: "my doc.pdf",
|
|
encodeCasefolderID: false,
|
|
encodeBlobname: false
|
|
});
|
|
|
|
assert.strictEqual(
|
|
deletePath,
|
|
"/api/file/deleteblob?container=alpha&casefolderID=A/B&blobname=my doc.pdf"
|
|
);
|
|
});
|
|
|
|
test("azurestorage helper builds getbloblist path without field mutation", () => {
|
|
const mod = loadAzureStorageHelperModule();
|
|
|
|
const listPath = mod.buildGetBlobListQueryPath({
|
|
containerName: "alpha",
|
|
casefolderID: "A/B"
|
|
});
|
|
|
|
assert.strictEqual(
|
|
listPath,
|
|
"/api/file/getbloblist?container=alpha&casefolderID=A/B"
|
|
);
|
|
});
|
|
|
|
test("azurestorage helper builds hash metadata map with stable keys", () => {
|
|
const mod = loadAzureStorageHelperModule({
|
|
hashAPIPath: (route) => `signed:${route}`
|
|
});
|
|
|
|
const metadata = mod.buildHashMetadataPaths({
|
|
containerName: "alpha",
|
|
casefolderID: "A/B",
|
|
blobname: "my doc.pdf"
|
|
});
|
|
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(metadata)), {
|
|
hashedfilepath:
|
|
"signed:/api/file/downloadblob?container=alpha&casefolderID=A%2FB&blobname=my%20doc.pdf",
|
|
hasheddeletepath:
|
|
"signed:/api/file/deleteblob?container=alpha&casefolderID=A%2FB&blobname=my%20doc.pdf",
|
|
hashgetblobs:
|
|
"signed:/api/file/getbloblist?container=alpha&casefolderID=A/B"
|
|
});
|
|
});
|
|
|
|
const run = async () => {
|
|
let passed = 0;
|
|
for (const currentTest of tests) {
|
|
await currentTest.fn();
|
|
passed += 1;
|
|
}
|
|
|
|
console.log(
|
|
`Phase 22 azurestorage-helper tests passed (${passed}/${tests.length}).`
|
|
);
|
|
};
|
|
|
|
module.exports = run;
|
|
|
|
if (require.main === module) {
|
|
run().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|
|
}
|