243 lines
6.9 KiB
JavaScript
243 lines
6.9 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
const assert = require("assert");
|
|
|
|
const rootDir = path.resolve(__dirname, "..", "..");
|
|
|
|
const loadEndpointClientModule = (injected = {}) => {
|
|
const filePath = path.join(
|
|
rootDir,
|
|
"actions",
|
|
"clients",
|
|
"endpointClient.js"
|
|
);
|
|
let source = fs.readFileSync(filePath, "utf8");
|
|
|
|
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
|
source = source.replace(/export const\s+/g, "const ");
|
|
source += "\nmodule.exports = { getJson, requestJson };\n";
|
|
|
|
const context = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
require,
|
|
axios: () => {
|
|
throw new Error("axios not injected");
|
|
},
|
|
...injected
|
|
};
|
|
|
|
vm.runInNewContext(source, context, { filename: filePath });
|
|
return context.module.exports;
|
|
};
|
|
|
|
const loadRelayClientModule = (injected = {}) => {
|
|
const filePath = path.join(rootDir, "actions", "clients", "relayClient.js");
|
|
let source = fs.readFileSync(filePath, "utf8");
|
|
|
|
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
|
source = source.replace(/export const\s+/g, "const ");
|
|
source += "\nmodule.exports = { buildHashedQueryUrl };\n";
|
|
|
|
const context = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
require,
|
|
axios: {
|
|
get: async () => {
|
|
throw new Error("axios.get not injected");
|
|
}
|
|
},
|
|
hashAPIPath: () => "&hash=fallback",
|
|
process: { env: {} },
|
|
...injected
|
|
};
|
|
|
|
vm.runInNewContext(source, context, { filename: filePath });
|
|
return context.module.exports;
|
|
};
|
|
|
|
const loadFileRouteBuilderModule = (injected = {}) => {
|
|
const filePath = path.join(
|
|
rootDir,
|
|
"actions",
|
|
"clients",
|
|
"fileRouteBuilder.js"
|
|
);
|
|
let source = fs.readFileSync(filePath, "utf8");
|
|
|
|
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
|
source = source.replace(/export const\s+/g, "const ");
|
|
source +=
|
|
"\nmodule.exports = { buildFileQuery, withBaseUrl, appendQuerySuffix, appendHashSuffix };\n";
|
|
|
|
const context = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
require,
|
|
encodeURIComponent,
|
|
...injected
|
|
};
|
|
|
|
vm.runInNewContext(source, context, { filename: filePath });
|
|
return context.module.exports;
|
|
};
|
|
|
|
const tests = [];
|
|
const test = (name, fn) => tests.push({ name, fn });
|
|
|
|
test("clients/endpointClient getJson returns response.data from axios.get", async () => {
|
|
const calls = [];
|
|
const axios = {
|
|
get: async (url, config) => {
|
|
calls.push({ url, config });
|
|
return { data: { ok: true } };
|
|
}
|
|
};
|
|
|
|
const mod = loadEndpointClientModule({ axios });
|
|
const result = await mod.getJson("/x", { headers: { a: 1 } });
|
|
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { ok: true });
|
|
assert.strictEqual(calls.length, 1);
|
|
assert.strictEqual(calls[0].url, "/x");
|
|
assert.strictEqual(calls[0].config.headers.a, 1);
|
|
});
|
|
|
|
test("clients/endpointClient requestJson returns response.data from axios(config)", async () => {
|
|
const calls = [];
|
|
const axios = async (config) => {
|
|
calls.push(config);
|
|
return { data: { saved: true } };
|
|
};
|
|
|
|
const mod = loadEndpointClientModule({ axios });
|
|
const result = await mod.requestJson({ method: "post", url: "/y" });
|
|
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(result)), { saved: true });
|
|
assert.strictEqual(calls.length, 1);
|
|
assert.strictEqual(calls[0].method, "post");
|
|
});
|
|
|
|
test("clients/relayClient buildHashedQueryUrl appends browser hash response", async () => {
|
|
const calls = [];
|
|
|
|
const mod = loadRelayClientModule({
|
|
axios: {
|
|
get: async (url) => {
|
|
calls.push(url);
|
|
return { data: { hash: "&hash=abc" } };
|
|
}
|
|
},
|
|
process: { env: {} }
|
|
});
|
|
|
|
const result = await mod.buildHashedQueryUrl("/api/file/deleteblob?x=1");
|
|
|
|
assert.strictEqual(result, "/api/file/deleteblob?x=1&hash=abc");
|
|
assert.strictEqual(calls.length, 1);
|
|
assert.strictEqual(
|
|
calls[0],
|
|
"/api/endpoint/gethash_api?path=%2Fapi%2Ffile%2Fdeleteblob%3Fx%3D1"
|
|
);
|
|
});
|
|
|
|
test("clients/relayClient falls back to hashAPIPath on server when HASHKEY present", async () => {
|
|
const mod = loadRelayClientModule({
|
|
axios: {
|
|
get: async () => {
|
|
throw new Error("network unavailable");
|
|
}
|
|
},
|
|
hashAPIPath: () => "&hash=server-fallback",
|
|
process: { env: { HASHKEY: "secret" } }
|
|
});
|
|
|
|
const result = await mod.buildHashedQueryUrl("/api/file/deleteblob?x=1");
|
|
assert.strictEqual(result, "/api/file/deleteblob?x=1&hash=server-fallback");
|
|
});
|
|
|
|
test("clients/relayClient rethrows when browser hash call fails and no HASHKEY", async () => {
|
|
const expected = new Error("hash service down");
|
|
|
|
const mod = loadRelayClientModule({
|
|
axios: {
|
|
get: async () => {
|
|
throw expected;
|
|
}
|
|
},
|
|
process: { env: {} }
|
|
});
|
|
|
|
await assert.rejects(
|
|
() => mod.buildHashedQueryUrl("/api/file/deleteblob?x=1"),
|
|
(error) => error === expected
|
|
);
|
|
});
|
|
|
|
test("clients/fileRouteBuilder builds query with optional encoding and suffix helpers", async () => {
|
|
const mod = loadFileRouteBuilderModule();
|
|
|
|
const unencoded = mod.buildFileQuery("/api/file/getbloblist", {
|
|
container: "abc",
|
|
casefolderID: "x/y"
|
|
});
|
|
const encoded = mod.buildFileQuery(
|
|
"/api/file/deleteblob",
|
|
{
|
|
container: "abc",
|
|
casefolderID: "x/y",
|
|
blobname: "doc one.pdf"
|
|
},
|
|
{
|
|
encode: true
|
|
}
|
|
);
|
|
|
|
assert.strictEqual(
|
|
unencoded,
|
|
"/api/file/getbloblist?container=abc&casefolderID=x/y"
|
|
);
|
|
assert.strictEqual(
|
|
encoded,
|
|
"/api/file/deleteblob?container=abc&casefolderID=x%2Fy&blobname=doc%20one.pdf"
|
|
);
|
|
assert.strictEqual(
|
|
mod.withBaseUrl("http://example.local", unencoded),
|
|
"http://example.local/api/file/getbloblist?container=abc&casefolderID=x/y"
|
|
);
|
|
assert.strictEqual(
|
|
mod.appendQuerySuffix(unencoded, "&hash=123"),
|
|
"/api/file/getbloblist?container=abc&casefolderID=x/y&hash=123"
|
|
);
|
|
assert.strictEqual(
|
|
mod.appendHashSuffix(unencoded, (route) =>
|
|
route.includes("getbloblist") ? "&hash=abc" : ""
|
|
),
|
|
"/api/file/getbloblist?container=abc&casefolderID=x/y&hash=abc"
|
|
);
|
|
});
|
|
|
|
const run = async () => {
|
|
let passed = 0;
|
|
|
|
for (const currentTest of tests) {
|
|
await currentTest.fn();
|
|
passed += 1;
|
|
}
|
|
|
|
console.log(
|
|
`Phase 22 client-utils tests passed (${passed}/${tests.length}).`
|
|
);
|
|
};
|
|
|
|
module.exports = run;
|
|
|
|
if (require.main === module) {
|
|
run().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|
|
}
|