clove/doughGit
test/ownership.test.mjs
/* test/ownership.test.mjs
* LICENCED DASL-1.0 (c) Clove Twilight
*/
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const root = mkdtempSync(join(tmpdir(), "dough-own-"));
process.env.MINIGIT_REPOS_ROOT = root;
process.env.MINIGIT_DB_PATH = join(root, "test.db");
const { rememberUser } = await import("../src/lib/server/users.ts");
const { createToken, revokeToken, listTokens } = await import("../src/lib/server/tokens.ts");
const { authenticateGit, gitActor } = await import("../src/lib/server/auth.ts");
let failures = 0;
function check(label, cond) {
console.log(`${cond ? "PASS" : "FAIL"} ${label}`);
if (!cond) failures++;
}
function basic(user, pass) {
return `Basic ${Buffer.from(`${user}:${pass}`).toString("base64")}`;
}
const alex = await rememberUser({
sub: "oidc-alex",
username: "Alex.Kim",
name: "Alex Kim",
picture: null,
});
const sam = await rememberUser({
sub: "oidc-sam",
username: "sam",
name: "Sam",
picture: null,
});
check("username is slugified into an owner segment", alex.slug === "alex.kim");
const impostor = await rememberUser({
sub: "oidc-impostor",
username: "alex.kim",
name: "Not Alex",
picture: null,
});
check("colliding usernames get distinct slugs", impostor.slug === "alex.kim-2");
const alexToken = await createToken("laptop", alex.slug, alex.sub);
const samToken = await createToken("laptop", sam.slug, sam.sub);
check(
"no credentials is anonymous",
(await authenticateGit(undefined)).kind === "anonymous",
);
check(
"a garbage token is rejected",
(await authenticateGit(basic("alex.kim", "nope"))).kind === "rejected",
);
const asAlex = await authenticateGit(basic("alex.kim", alexToken));
check(
"a token resolves to its owner",
asAlex.kind === "user" && asAlex.owner === "alex.kim",
);
const asAlexRaw = await authenticateGit(basic("Alex.Kim", alexToken));
check(
"the un-slugified username is accepted too",
asAlexRaw.kind === "user" && asAlexRaw.owner === "alex.kim",
);
check(
"a token with the wrong username is rejected",
(await authenticateGit(basic("sam", alexToken))).kind === "rejected",
);
check(
"a token with no username is rejected",
(await authenticateGit(basic("", alexToken))).kind === "rejected",
);
check(
"there is no instance-wide credential any more",
(await authenticateGit(basic("anything", "static-instance-token"))).kind === "rejected",
);
check("a user token acts as its owner", gitActor(asAlex) === "alex.kim");
check("anonymous acts as nobody", gitActor({ kind: "anonymous" }) === null);
check(
"rejected credentials act as nobody",
gitActor({ kind: "rejected", message: "no" }) === null,
);
check(
"a user only sees their own tokens",
(await listTokens("alex.kim")).length === 1 && (await listTokens("sam")).length === 1,
);
check(
"revoking someone else's token is refused",
await revokeToken((await listTokens("sam"))[0].id, "alex.kim") === false,
);
check(
"revoking your own token works",
await revokeToken((await listTokens("sam"))[0].id, "sam") === true &&
(await listTokens("sam")).length === 0,
);
check(
"a revoked token stops authenticating",
(await authenticateGit(basic("sam", samToken))).kind === "rejected",
);
rmSync(root, { recursive: true, force: true });
console.log(failures ? `\n${failures} FAILED` : "\nALL PASSED");
process.exit(failures ? 1 : 0);