Files
ARTEX/web/scripts/auth.test.mjs
T

45 lines
1.6 KiB
JavaScript

import ts from "typescript";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";
import vm from "node:vm";
const output = ts.transpileModule(readFileSync(new URL("../src/lib/auth.ts", import.meta.url), "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText;
test("HTTPS marks the session cookie Secure while local HTTP remains usable", () => {
for (const protocol of ["https:", "http:"]) {
const storage = new Map();
const document = { cookie: "" };
const exports = {};
vm.runInNewContext(output, {
exports,
document,
window: { location: { protocol } },
localStorage: { setItem: (k, v) => storage.set(k, v) },
});
exports.auth.setToken("synthetic-session");
assert.equal(storage.get("artex_token"), "synthetic-session");
assert.equal(document.cookie.includes("; Secure"), protocol === "https:");
assert.ok(document.cookie.includes("SameSite=Lax"));
}
});
test("the route-guard cookie is a marker and never carries the session token", () => {
const storage = new Map();
const document = { cookie: "" };
const exports = {};
vm.runInNewContext(output, {
exports,
document,
window: { location: { protocol: "https:" } },
localStorage: { setItem: (k, v) => storage.set(k, v) },
});
exports.auth.setToken("synthetic.session.token");
assert.equal(storage.get("artex_token"), "synthetic.session.token");
assert.ok(document.cookie.startsWith("artex_token=1;"), document.cookie);
assert.ok(!document.cookie.includes("synthetic"), document.cookie);
});