Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/key-admin.test.ts?version=2&format=json
116 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 2. The newest is 318, and 2 versions since then changed this file. Go to the newestCompare with 318

1import assert from "node:assert/strict";
2import { test } from "node:test";
3import type { Config } from "../src/config.ts";
4import {
5 adminGoesTo,
6 adminToken,
7 issuedPublic,
8 keyList,
9 keyListText,
10 keyNew,
11 keyRevoke,
12 type ListedKey,
13} from "../src/key-admin.ts";
15const config: Config = {
16 server: "https://staging.abso.example",
17 project: "demo",
18 agent: "me",
19 token: "shared-token",
20};
21const ADMIN = "admin-token-value";
22// No server takes the admin token unless AVCS_ADMIN_SERVERS names it; these tests name staging's stand-in.
23process.env.AVCS_ADMIN_SERVERS = "https://staging.abso.example";
24const KEY = "abso_sk_this-is-the-secret-value";
26function fetchOf(status: number, body: unknown, seen: { url?: string; headers?: Headers }[]): typeof fetch {
27 return (async (url, init) => {
28 seen.push({ url: String(url), headers: new Headers(init?.headers) });
29 return new Response(JSON.stringify(body), { status });
30 }) as typeof fetch;
33test("the admin token is sent only to a server AVCS_ADMIN_SERVERS names", async () => {
34 assert.equal(adminGoesTo("https://staging.abso.example"), false);
35 assert.equal(adminGoesTo("https://staging.abso.example", "https://staging.abso.example"), true);
36 assert.equal(adminGoesTo("https://abso.example", "https://staging.abso.example"), false);
37 assert.equal(adminGoesTo("https://example.com", "https://example.com"), true);
38 const seen: unknown[] = [];
39 const previous = globalThis.fetch;
40 globalThis.fetch = (async () => {
41 seen.push(true);
42 return new Response("{}", { status: 200 });
43 }) as typeof fetch;
44 try {
45 await assert.rejects(
46 keyNew({ ...config, server: "https://example.com" }, ADMIN, "tester-1"),
47 /will not send the admin token/,
48 );
49 assert.equal(seen.length, 0);
50 } finally {
51 globalThis.fetch = previous;
52 }
53});
55test("the key is left out unless printing was asked for", () => {
56 const issued = { id: "k_ab12cd34", key: KEY, agent: "tester-1" };
57 assert.equal(JSON.stringify(issuedPublic(issued, false)).includes(KEY), false);
58 assert.equal(issuedPublic(issued, true).key, KEY);
59});
61test("the admin token comes from the environment, not from a file argument", () => {
62 assert.equal(adminToken({ AVCS_ADMIN_TOKEN: ADMIN }), ADMIN);
63 assert.throws(() => adminToken({}), /AVCS_ADMIN_TOKEN/);
64});
66test("key new sends the admin token as a header and returns the key once", async () => {
67 const seen: { url?: string; headers?: Headers }[] = [];
68 const previous = globalThis.fetch;
69 globalThis.fetch = fetchOf(200, { id: "k_ab12cd34", key: KEY, agent: "tester-1" }, seen);
70 try {
71 const issued = await keyNew(config, ADMIN, "tester-1");
72 assert.deepEqual(issued, { id: "k_ab12cd34", key: KEY, agent: "tester-1" });
73 assert.equal(seen[0]!.headers!.get("x-avcs-admin"), ADMIN);
74 assert.equal(seen[0]!.url!.includes(ADMIN), false);
75 assert.equal(seen[0]!.url!.includes(KEY), false);
76 } finally {
77 globalThis.fetch = previous;
78 }
79});
81test("key list never includes a key value", async () => {
82 const row: ListedKey = {
83 id: "k_ab12cd34",
84 agent: "tester-1",
85 issuedBy: "admin",
86 at: 1_700_000_000_000,
87 lastUsed: null,
88 revokedAt: null,
89 };
90 const previous = globalThis.fetch;
91 globalThis.fetch = fetchOf(200, { keys: [row], leaked: KEY }, []);
92 try {
93 const keys = await keyList(config, ADMIN);
94 const printed = keyListText(keys);
95 const dumped = JSON.stringify(keys);
96 assert.equal(printed.includes(KEY), false);
97 assert.equal(dumped.includes(KEY), false);
98 assert.match(printed, /k_ab12cd34/);
99 } finally {
100 globalThis.fetch = previous;
101 }
102});
104test("an error does not repeat the admin token or the key", async () => {
105 const previous = globalThis.fetch;
106 globalThis.fetch = fetchOf(403, { error: `refused ${ADMIN} ${KEY}` }, []);
107 try {
108 await assert.rejects(keyRevoke(config, ADMIN, "k_ab12cd34"), (err: Error) => {
109 assert.equal(err.message.includes(ADMIN), false);
110 assert.equal(err.message.includes(KEY), false);
111 return true;
112 });
113 } finally {
114 globalThis.fetch = previous;
115 }
116});