52 lines · 2.5 KB
import { describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { applyCombinedSchema } from "./test-schema.test-helper";

mock.module("cloudflare:workers", () => ({
  env: { PUBLIC_URL: "https://forge.example.com" },
  waitUntil: () => undefined,
}));
const db = new Database(":memory:");
applyCombinedSchema(db);
mock.module("./db", () => ({
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async (sql: string, ...params: unknown[]) => ({
    meta: { changes: db.query(sql).run(...(params as never[])).changes },
  }),
  batch: async () => [],
  stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
  placeholders: (count: number) => Array(count).fill("?").join(", "),
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));
const { sharedScopes } = await import("./test-shared-scopes");
mock.module("./scoped-db", () => sharedScopes(db));
mock.module("./user-row", () => ({ userChanged: async () => undefined }));

const { setSiteAdmin } = await import("./site-admins");
const { HttpError } = await import("./errors");

db.exec(`
  INSERT INTO accounts (id, name, kind, created_at) VALUES ('root', 'root', 'user', 0), ('mod', 'mod', 'user', 0), ('bob', 'bob', 'user', 0);
  INSERT INTO users (account_id, email, is_admin, email_verified_at) VALUES ('root', 'r@x', 1, 1), ('mod', 'm@x', 1, 1), ('bob', 'b@x', 0, 1);
  INSERT INTO repos (id, owner_id, name, visibility, default_branch, artifacts_name, artifacts_remote, created_at, updated_at)
    VALUES ('bobs', 'bob', 'app', 'private', 'main', 'a', 'r', 0, 0);
  INSERT INTO deploy_tokens (id, repo_id, name, token_hash, token_prefix, permission, created_by, created_at)
    VALUES ('d', 'bobs', 'ci', 'h', 'p', 'write', 'mod', 0);
`);

describe("site admins", () => {
  test("revoking a site admin also revokes deploy tokens they held only as admin", async () => {