forked from longtao.wu/droboticflow
219 lines
6.9 KiB
JavaScript
219 lines
6.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
authenticationHeaders,
|
|
booleanEnv,
|
|
buildAcceptanceHeaders,
|
|
createAcceptanceAPIClient,
|
|
csrfTokenFromContext,
|
|
isCSRFProtectedMethod,
|
|
loginIfConfigured,
|
|
positiveIntegerEnv,
|
|
validateSessionCredentials,
|
|
} from "../../scripts/lib/acceptance-client.mjs";
|
|
|
|
function jsonResponse(status, payload) {
|
|
return {
|
|
status,
|
|
async text() {
|
|
return payload === undefined ? "" : JSON.stringify(payload);
|
|
},
|
|
};
|
|
}
|
|
|
|
function textResponse(status, payload) {
|
|
return {
|
|
status,
|
|
async text() {
|
|
return payload;
|
|
},
|
|
};
|
|
}
|
|
|
|
test("builds bearer authentication headers only when token is present", () => {
|
|
assert.deepEqual(authenticationHeaders(" token "), { Authorization: "Bearer token" });
|
|
assert.equal(authenticationHeaders(""), undefined);
|
|
});
|
|
|
|
test("validates paired session credentials", () => {
|
|
assert.doesNotThrow(() => validateSessionCredentials("", ""));
|
|
assert.doesNotThrow(() => validateSessionCredentials("admin@example.test", "password"));
|
|
assert.throws(() => validateSessionCredentials("admin@example.test", ""), /must be set together/);
|
|
assert.throws(() => validateSessionCredentials("", "password"), /must be set together/);
|
|
});
|
|
|
|
test("parses positive integer acceptance environment values exactly", () => {
|
|
assert.equal(positiveIntegerEnv("SCAN_MIN_FILES", 1, {}), 1);
|
|
assert.equal(positiveIntegerEnv("SCAN_MIN_FILES", 1, { SCAN_MIN_FILES: " 42 " }), 42);
|
|
});
|
|
|
|
test("rejects lossy positive integer acceptance environment values", () => {
|
|
for (const value of ["0", "01", "1.5", "12files", String(Number.MAX_SAFE_INTEGER + 1)]) {
|
|
assert.throws(
|
|
() => positiveIntegerEnv("SCAN_MIN_FILES", 1, { SCAN_MIN_FILES: value }),
|
|
/SCAN_MIN_FILES must be a (safe )?positive integer/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test("parses boolean acceptance environment values exactly", () => {
|
|
assert.equal(booleanEnv("PLAYWRIGHT_IGNORE_HTTPS_ERRORS", false, {}), false);
|
|
assert.equal(booleanEnv("PLAYWRIGHT_IGNORE_HTTPS_ERRORS", true, {}), true);
|
|
for (const value of ["1", "true", "yes", "on", " TRUE "]) {
|
|
assert.equal(booleanEnv("PLAYWRIGHT_IGNORE_HTTPS_ERRORS", false, { PLAYWRIGHT_IGNORE_HTTPS_ERRORS: value }), true);
|
|
}
|
|
for (const value of ["0", "false", "no", "off", " OFF "]) {
|
|
assert.equal(booleanEnv("PLAYWRIGHT_IGNORE_HTTPS_ERRORS", true, { PLAYWRIGHT_IGNORE_HTTPS_ERRORS: value }), false);
|
|
}
|
|
});
|
|
|
|
test("rejects invalid boolean acceptance environment values", () => {
|
|
for (const value of ["maybe", "enabled", "2", "truthy"]) {
|
|
assert.throws(
|
|
() => booleanEnv("PLAYWRIGHT_IGNORE_HTTPS_ERRORS", false, { PLAYWRIGHT_IGNORE_HTTPS_ERRORS: value }),
|
|
/PLAYWRIGHT_IGNORE_HTTPS_ERRORS must be a boolean/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test("adds CSRF token for protected Playwright context methods", async () => {
|
|
const context = {
|
|
async cookies(baseURL) {
|
|
assert.equal(baseURL, "https://api.example.test");
|
|
return [{ name: "droboticflow_csrf", value: "csrf%2Btoken" }];
|
|
},
|
|
};
|
|
|
|
assert.equal(await csrfTokenFromContext(context, "https://api.example.test"), "csrf+token");
|
|
assert.equal(isCSRFProtectedMethod("GET"), false);
|
|
assert.equal(isCSRFProtectedMethod("POST"), true);
|
|
|
|
const headers = await buildAcceptanceHeaders({
|
|
baseURL: "https://api.example.test",
|
|
context,
|
|
method: "POST",
|
|
body: { name: "demo" },
|
|
apiToken: "secret",
|
|
});
|
|
|
|
assert.equal(headers.Authorization, "Bearer secret");
|
|
assert.equal(headers["Content-Type"], "application/json");
|
|
assert.equal(headers["X-Droboticflow-CSRF-Token"], "csrf+token");
|
|
|
|
const getHeaders = await buildAcceptanceHeaders({
|
|
baseURL: "https://api.example.test",
|
|
context,
|
|
method: "GET",
|
|
apiToken: "secret",
|
|
});
|
|
assert.equal("X-Droboticflow-CSRF-Token" in getHeaders, false);
|
|
});
|
|
|
|
test("uses fetch client with bearer auth, JSON encoding, and retry", async () => {
|
|
const calls = [];
|
|
const fetchImpl = async (url, init) => {
|
|
calls.push({ url, init });
|
|
if (calls.length === 1) {
|
|
throw new Error("temporary network failure");
|
|
}
|
|
return jsonResponse(201, { id: "project-1" });
|
|
};
|
|
const client = createAcceptanceAPIClient({
|
|
baseURL: "https://api.example.test/",
|
|
apiToken: "secret",
|
|
fetchImpl,
|
|
retryDelayMs: () => 0,
|
|
});
|
|
|
|
const result = await client.request("/api/projects", {
|
|
method: "POST",
|
|
body: { name: "真实项目" },
|
|
expected: [201],
|
|
});
|
|
|
|
assert.deepEqual(result, { id: "project-1" });
|
|
assert.equal(calls.length, 2);
|
|
assert.equal(calls[1].url, "https://api.example.test/api/projects");
|
|
assert.equal(calls[1].init.method, "POST");
|
|
assert.equal(calls[1].init.headers.Authorization, "Bearer secret");
|
|
assert.equal(calls[1].init.headers["Content-Type"], "application/json");
|
|
assert.equal(calls[1].init.body, '{"name":"真实项目"}');
|
|
});
|
|
|
|
test("uses Playwright context request with CSRF and preserves string bodies", async () => {
|
|
const requests = [];
|
|
const context = {
|
|
async cookies() {
|
|
return [{ name: "droboticflow_csrf", value: "csrf-token" }];
|
|
},
|
|
request: {
|
|
async fetch(url, init) {
|
|
requests.push({ url, init });
|
|
return {
|
|
status() {
|
|
return 200;
|
|
},
|
|
async text() {
|
|
return '{"ok":true}';
|
|
},
|
|
};
|
|
},
|
|
},
|
|
};
|
|
const client = createAcceptanceAPIClient({
|
|
baseURL: "https://api.example.test",
|
|
apiToken: "secret",
|
|
context,
|
|
});
|
|
|
|
const result = await client.request("/api/workflows/wf-1/run", {
|
|
method: "POST",
|
|
body: '{"trigger":"manual"}',
|
|
});
|
|
|
|
assert.deepEqual(result, { ok: true });
|
|
assert.equal(requests[0].url, "https://api.example.test/api/workflows/wf-1/run");
|
|
assert.equal(requests[0].init.headers.Authorization, "Bearer secret");
|
|
assert.equal(requests[0].init.headers["X-Droboticflow-CSRF-Token"], "csrf-token");
|
|
assert.equal(requests[0].init.data, '{"trigger":"manual"}');
|
|
});
|
|
|
|
test("reports response body on unexpected status", async () => {
|
|
const client = createAcceptanceAPIClient({
|
|
baseURL: "https://api.example.test",
|
|
fetchImpl: async () => textResponse(403, "forbidden"),
|
|
retryDelayMs: () => 0,
|
|
attempts: 1,
|
|
});
|
|
|
|
await assert.rejects(
|
|
() => client.request("/api/projects", { method: "DELETE" }),
|
|
/DELETE \/api\/projects failed with 403: forbidden/,
|
|
);
|
|
});
|
|
|
|
test("login helper returns authenticated identifier", async () => {
|
|
const calls = [];
|
|
const client = {
|
|
async request(pathName, options) {
|
|
calls.push({ pathName, options });
|
|
return { email: "admin@example.test" };
|
|
},
|
|
};
|
|
|
|
const identifier = await loginIfConfigured(client, {
|
|
email: "admin@example.test",
|
|
password: "password",
|
|
label: "test user",
|
|
});
|
|
|
|
assert.equal(identifier, "admin@example.test");
|
|
assert.equal(calls[0].pathName, "/api/auth/login");
|
|
assert.equal(calls[0].options.method, "POST");
|
|
assert.deepEqual(calls[0].options.body, {
|
|
email: "admin@example.test",
|
|
password: "password",
|
|
});
|
|
});
|