Skip to content
refs
20 lines · 1.7 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { cliApi } from "../cli.ts";
4import { d1, migrated } from "./d1.ts";
5
6test("the CLI's repository routes act as the person only for a sign-in with the git scope", async () => {
7 const sqlite = migrated();
8 sqlite.exec("INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0); INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@refs.test', 'Aman', '', 0); INSERT INTO memberships (namespace_id, user_id, role, created_at) VALUES ('ns', 'u1', 'owner', 0)");
9 sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('r1', 'ns', 'demo', 'private', 'demo', 'u1', 0)");
10 sqlite.exec("INSERT INTO pulls (id, repo_id, number, title, author_id, base_ref, head_ref, created_at, updated_at) VALUES (1, 'r1', 3, 'Fix login', 'u1', 'main', 'fix-login', 0, 0)");
11 const env = { DB: d1(sqlite), ADMIN_EMAILS: "" } as unknown as Env;
12 const call = (path: string, scope: string[]) => cliApi.fetch(new Request(`https://refs.test${path}`), env, { waitUntil() {}, props: { userId: "u1" }, auth: { scope } } as unknown as ExecutionContext);
13
14 assert.equal((await call("/api/cli/repos/refs/demo/pulls", ["agents"])).status, 403);
15 const listed = await call("/api/cli/repos/refs/demo/pulls?state=open", ["agents", "git"]);
16 assert.equal(listed.status, 200);
17 assert.deepEqual(((await listed.json()) as { items: { number: number }[] }).items.map((pull) => pull.number), [3]);
18 assert.equal((await call("/api/cli/repos/refs/missing/pulls", ["agents", "git"])).status, 404);
19 assert.equal((await call("/api/cli/whoami", ["agents"])).status, 200);
20});