import { describe, expect, it } from "bun:test";
import { readFileSync } from "fs";
const API_AUTH = readFileSync("src/middleware/api-auth.ts", "utf8");
const AUTH = readFileSync("src/middleware/auth.ts", "utf8");
describe("api-auth session fallback honours requires2fa", () => {
it("rejects a session still pending its second factor", () => {
expect(API_AUTH).toContain("!session.requires2fa");
});
it("checks it alongside expiry, in the same guard", () => {
const guard = API_AUTH.slice(
API_AUTH.indexOf("// Fall back to session cookie"),
API_AUTH.indexOf('c.set("authMethod", "session")')
);
expect(guard).toContain("expiresAt");
expect(guard).toContain("!session.requires2fa");
});
it("does not grant scopes before the 2FA check", () => {
const scopeIdx = API_AUTH.indexOf('c.set("tokenScopes", ["repo", "user", "admin"])');
const checkIdx = API_AUTH.indexOf("!session.requires2fa");
expect(checkIdx).toBeGreaterThan(-1);
expect(scopeIdx).toBeGreaterThan(checkIdx);
});
});
describe("the two middlewares agree", () => {
it("both consult requires2fa", () => {
expect(AUTH).toContain("session.requires2fa");
expect(API_AUTH).toContain("session.requires2fa");
});
});
|