1
0
Fork 0
forked from svrjs/svrjs

test: mock "path" module in forbidden path utility function unit tests

This commit is contained in:
Dorian Niemiec 2024-09-02 13:08:05 +02:00
parent 0fe9a505a6
commit fe6853524e
2 changed files with 14 additions and 3 deletions

View file

@ -4,17 +4,16 @@ const path = require("path");
// Function to get URL path for use in forbidden path adding. // Function to get URL path for use in forbidden path adding.
function getInitializePath(to) { function getInitializePath(to) {
const isWin32 = os.platform() == "win32"; const isWin32 = os.platform() == "win32";
const pathModS = isWin32 ? path.win32 : path.posix; // pathModS needed just for the test suite
const cwd = process.cwd(); const cwd = process.cwd();
if (isWin32) { if (isWin32) {
to = to.replace(/\//g, "\\"); to = to.replace(/\//g, "\\");
if (to[0] == "\\") to = cwd.split("\\")[0] + to; if (to[0] == "\\") to = cwd.split("\\")[0] + to;
} }
const absoluteTo = pathModS.isAbsolute(to) const absoluteTo = path.isAbsolute(to)
? to ? to
: process.dirname + (isWin32 ? "\\" : "/") + to; : process.dirname + (isWin32 ? "\\" : "/") + to;
if (isWin32 && cwd[0] != absoluteTo[0]) return ""; if (isWin32 && cwd[0] != absoluteTo[0]) return "";
const relative = pathModS.relative(cwd, absoluteTo); const relative = path.relative(cwd, absoluteTo);
if (isWin32) { if (isWin32) {
return "/" + relative.replace(/\\/g, "/"); return "/" + relative.replace(/\\/g, "/");
} else { } else {

View file

@ -9,6 +9,18 @@ const os = require("os");
jest.mock("os", () => ({ jest.mock("os", () => ({
platform: jest.fn() platform: jest.fn()
})); }));
jest.mock("path", () => {
const path = jest.requireActual("path");
const os = require("os");
return {
isAbsolute: (...params) =>
(os.platform() == "win32" ? path.win32 : path.posix).isAbsolute(
...params
),
relative: (...params) =>
(os.platform() == "win32" ? path.win32 : path.posix).relative(...params)
};
});
describe("Forbidden paths handling", () => { describe("Forbidden paths handling", () => {
beforeEach(() => { beforeEach(() => {