320 lines
9.3 KiB
JavaScript
320 lines
9.3 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 loadUpdateLinks = () => {
|
|
const filePath = path.join(rootDir, "components", "utils", "index.js");
|
|
const source = fs.readFileSync(filePath, "utf8");
|
|
|
|
const start = source.indexOf("export const updateLinks =");
|
|
const end = source.indexOf("\nexport const whichRepType", start);
|
|
|
|
assert.ok(start >= 0, "updateLinks export not found");
|
|
assert.ok(end > start, "updateLinks boundary not found");
|
|
|
|
const implementation = source
|
|
.slice(start, end)
|
|
.replace("export const updateLinks =", "const updateLinks =");
|
|
|
|
const context = { module: { exports: {} } };
|
|
|
|
vm.runInNewContext(
|
|
`${implementation}\nmodule.exports = updateLinks;`,
|
|
context,
|
|
{ filename: filePath }
|
|
);
|
|
|
|
return context.module.exports;
|
|
};
|
|
|
|
test("updateLinks removes Quill video iframe", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
const input =
|
|
'<p>Before</p><iframe class="ql-video" src="https://example.com/video"></iframe><p>After</p>';
|
|
|
|
assert.strictEqual(updateLinks(input), "<p>Before</p><p>After</p>");
|
|
});
|
|
|
|
test("updateLinks removes HTML video elements", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
const input =
|
|
'<p>Before</p><video controls src="https://example.com/video"><source src="https://example.com/file.mp4"></video><p>After</p>';
|
|
|
|
assert.strictEqual(updateLinks(input), "<p>Before</p><p>After</p>");
|
|
});
|
|
|
|
test("updateLinks converts plain URLs", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
assert.strictEqual(
|
|
updateLinks("<p>Visit https://example.com.</p>"),
|
|
'<p>Visit <a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a>.</p>'
|
|
);
|
|
});
|
|
|
|
test("updateLinks preserves existing anchors", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
const input = '<p><a href="https://example.com">Existing link</a></p>';
|
|
|
|
assert.strictEqual(updateLinks(input), input);
|
|
});
|
|
|
|
test("updateLinks preserves standard rich-text formatting", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
const input = "<p><strong>Bold</strong> and <em>italic</em></p>";
|
|
|
|
assert.strictEqual(updateLinks(input), input);
|
|
});
|
|
|
|
test("updateLinks removes empty Quill paragraphs", () => {
|
|
const updateLinks = loadUpdateLinks();
|
|
|
|
assert.strictEqual(
|
|
updateLinks("<p>Content</p><p><br></p><p> </p>"),
|
|
"<p>Content</p>"
|
|
);
|
|
});
|
|
|
|
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);
|
|
});
|
|
}
|