Skip to content

Open-source status page, incident response and uptime monitoring

1.6 KB
Raw Blame
import { describe, expect, test } from "vitest";

import { validateKey } from "./auth";

/**
 * `validateKey` has three production branches (custom DB key, Unkey
 * fallback, super-admin) and one dev fallback. Tests here cover the
 * dev fallback exhaustively. The production branches require flipping
 * `NODE_ENV` at module-load time, which reroutes other middleware in
 * this app (see `routes/mcp/handler.test.ts` for the same caveat) — a
 * dedicated production-mode test harness is out of scope.
 *
 * What we DO assert structurally even without that harness:
 *   - dev fallback returns scopes `['*']` (so local dev mirrors super-
 *     admin and isn't accidentally locked out by scope checks)
 *   - the `keyId` round-trips so audit attribution holds
 */
describe("validateKey (dev fallback)", () => {
  test("returns scopes ['*'] so local dev mirrors super-admin", async () => {
    const { result, error } = await validateKey("anything");
    expect(error).toBeUndefined();
    expect(result.valid).toBe(true);
    expect(result.scopes).toEqual(["*"]);
  });

  test("uses the input string as both ownerId and keyId", async () => {
    const { result } = await validateKey("dev-key-42");
    expect(result.ownerId).toBe("dev-key-42");
    expect(result.keyId).toBe("dev-key-42");
    expect(result.authMethod).toBe("dev");
  });
});

describe("validateKey (oauth access tokens)", () => {
  test("rejects an unknown os_oat_ token before the dev fallback", async () => {
    const { result, error } = await validateKey("os_oat_not-a-real-token");
    expect(result.valid).toBe(false);
    expect(error?.message).toContain("access token");
  });
});