- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/box.test.ts?version=79&format=json
340 linesFirst import 55% · trials 20% · speed 17% · pinch 7% · review 1%Last change trials in version 64, 7 versions changed it
You're reading version 79. The newest is 318, and 5 versions since then changed this file.
1// Signing in once per box (abso issue 108): login waits for approval and keeps
2// the credential outside every project folder; it never shows up in output and
3// only goes to the server that issued it.
5import assert from "node:assert/strict";
6import { mkdtempSync, readdirSync, readFileSync } from "node:fs";
7import { tmpdir } from "node:os";
8import { join } from "node:path";
9import { test } from "node:test";
10import {
11 boxPublic,
12 boxText,
13 forgetBox,
14 login,
15 logout,
16 mintFreeName,
17 mintKey,
18 readBox,
19 signedInServers,
20 writeBox,
21} from "../src/box.ts";
22import { serverFrom } from "../src/join-commands.ts";
24const SERVER = "https://staging.abso.example";
25const CREDENTIAL = "abso_bx_the-secret-credential";
26const grant = {
27 projects: [
28 { project: "abso", role: "writer" },
29 { project: "game", role: "reader" },
30 ],
31 max_agents: 5,
32};
33const EXPIRES = "2026-10-15T00:00:00.000Z";
34const record = { server: SERVER, id: "b_0123abcd", credential: CREDENTIAL, grant, expiresAt: Date.parse(EXPIRES) };
36const home = () => mkdtempSync(join(tmpdir(), "abso-box-"));
38type Seen = { url: string; auth: string | null; body: unknown }[];
39function server(answers: [number, unknown][], seen: Seen): typeof fetch {
40 return (async (url, init) => {
41 seen.push({
42 url: String(url),
43 auth: new Headers(init?.headers).get("authorization"),
44 body: JSON.parse(String(init?.body ?? "null")),
45 });
46 const [status, body] = answers.shift() ?? [500, { error: "no more answers" }];
47 return new Response(JSON.stringify(body), { status });
48 }) as typeof fetch;
49}
51const started = {
52 device_code: "device-code-long-enough",
53 user_code: "WXYZ-BCDF",
54 verification_uri_complete: "https://abso.dev/login/box?code=WXYZ-BCDF",
55 expires_in: 60,
56 interval: 2,
57};
59function clock() {
60 let t = 0;
61 return {
62 sleep: async (ms: number) => {
63 t += ms;
64 },
65 now: () => t,
66 slept: () => t,
67 };
68}
70test("login shows the code, waits while it's pending, and keeps the credential in the user folder", async () => {
71 const h = home();
72 const seen: Seen = [];
73 const said: string[] = [];
74 const c = clock();
75 const got = await login(
76 `${SERVER}/some/path`,
77 {
78 fetch: server(
79 [
80 [200, started],
81 [400, { error: "authorization_pending" }],
82 [400, { error: "slow_down" }],
83 [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],
84 ],
85 seen,
86 ),
87 sleep: c.sleep,
88 now: c.now,
89 say: (line) => said.push(line),
90 },
91 h,
92 );
93 assert.equal(got.server, SERVER);
94 assert.deepEqual(
95 seen.map((s) => s.url),
96 [`${SERVER}/login/device`, `${SERVER}/login/token`, `${SERVER}/login/token`, `${SERVER}/login/token`],
97 );
98 assert.deepEqual(seen[1]?.body, { device_code: "device-code-long-enough" });
99 assert.equal(c.slept(), 2000 + 2000 + 7000, "slow_down adds 5 s to the wait");
100 assert.ok(
101 seen.every((s) => s.auth === null),
102 "nothing is sent with login's calls",
103 );
104 assert.deepEqual(said, [
105 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.",
106 "Waiting for approval. Ctrl-C stops waiting, and abso stops by itself when the code runs out.",
107 ]);
108 assert.equal(readBox(SERVER, h)?.credential, CREDENTIAL);
109 const files = readdirSync(join(h, ".abso", "boxes"));
110 assert.equal(files.length, 1);
111 assert.ok(readFileSync(join(h, ".abso", "boxes", files[0]!), "utf8").includes(CREDENTIAL));
112 assert.equal(boxText(got, SERVER, 0).includes(CREDENTIAL), false);
113 assert.match(boxText(got, SERVER, 0), /abso \(writer\), game \(reader\)\. Up to 5 agents\./);
114 assert.equal(JSON.stringify(boxPublic(got)).includes(CREDENTIAL), false);
115});
117test("login opens the approve page with the code in it, and prints the link and code all the same", async () => {
118 const said: string[] = [];
119 const opened: string[] = [];
120 const c = clock();
121 await login(
122 SERVER,
123 {
124 fetch: server(
125 [
126 [200, started],
127 [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],
128 ],
129 [],
130 ),
131 sleep: c.sleep,
132 now: c.now,
133 say: (line) => said.push(line),
134 open: (url) => (opened.push(url), true),
135 },
136 home(),
137 );
138 assert.deepEqual(opened, ["https://abso.dev/login/box?code=WXYZ-BCDF"]);
139 assert.deepEqual(said.slice(0, 2), [
140 "Opening the approve page in your browser.",
141 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.",
142 ]);
143});
145test("an open that doesn't start says nothing about a browser, and the printed link stands", async () => {
146 const said: string[] = [];
147 const c = clock();
148 await login(
149 SERVER,
150 {
151 fetch: server(
152 [
153 [200, started],
154 [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],
155 ],
156 [],
157 ),
158 sleep: c.sleep,
159 now: c.now,
160 say: (line) => said.push(line),
161 open: () => false,
162 },
163 home(),
164 );
165 assert.equal(
166 said[0],
167 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.",
168 );
169 assert.equal(
170 said.some((l) => l.includes("browser")),
171 false,
172 );
173});
175test("a code that's turned down or runs out says so and keeps nothing", async () => {
176 const h = home();
177 const c = clock();
178 const io = { sleep: c.sleep, now: c.now, say: () => {} };
179 await assert.rejects(
180 login(
181 SERVER,
182 {
183 ...io,
184 fetch: server(
185 [
186 [200, started],
187 [400, { error: "access_denied" }],
188 ],
189 [],
190 ),
191 },
192 h,
193 ),
194 /was turned down/,
195 );
196 await assert.rejects(
197 login(
198 SERVER,
199 {
200 ...io,
201 fetch: server(
202 [
203 [200, started],
204 [400, { error: "expired_token" }],
205 ],
206 [],
207 ),
208 },
209 h,
210 ),
211 /ran out\. Run abso login again/,
212 );
213 await assert.rejects(
214 login(
215 SERVER,
216 {
217 ...io,
218 fetch: server(
219 [
220 [200, { ...started, expires_in: 3 }],
221 [400, { error: "authorization_pending" }],
222 [400, { error: "authorization_pending" }],
223 ],
224 [],
225 ),
226 },
227 h,
228 ),
229 /in time\. Run abso login again/,
230 );
231 assert.equal(readBox(SERVER, h), undefined);
232});
234test("login says it's still waiting about once a minute, and stops in the end whatever the server says", async () => {
235 const c = clock();
236 const said: string[] = [];
237 const pending = Array.from({ length: 2000 }, (): [number, unknown] => [400, { error: "authorization_pending" }]);
238 await assert.rejects(
239 login(
240 SERVER,
241 {
242 fetch: server([[200, { ...started, expires_in: 10 ** 9, interval: 5 }], ...pending], []),
243 sleep: c.sleep,
244 now: c.now,
245 say: (line) => said.push(line),
246 },
247 home(),
248 ),
249 /nobody approved the code WXYZ-BCDF in time\. Run abso login again\./,
250 );
251 assert.equal(c.slept(), 30 * 60 * 1000, "a server that says never still gets an end");
252 const still = said.filter((line) => line === "Still waiting for the code WXYZ-BCDF to be approved.");
253 assert.equal(still.length, 30);
254 assert.equal(said.length, 2 + 30);
255});
257test("a key is minted with the box credential, sent only to the box's own server", async () => {
258 const seen: Seen = [];
259 const minted = await mintKey(
260 record,
261 "abso",
262 "tester-1",
263 server([[200, { key: "abso_sk_x", id: "k_0123abcd" }]], seen),
264 );
265 assert.deepEqual(minted, { key: "abso_sk_x", id: "k_0123abcd" });
266 assert.equal(seen[0]?.url.startsWith(`${SERVER}/`), true);
267 assert.equal(seen[0]?.auth, `Box ${CREDENTIAL}`);
268 await assert.rejects(
269 mintKey(record, "secret-project", "a", server([[403, { error: `outside the grant ${CREDENTIAL}` }]], [])),
270 (e: Error) => /refused \(403\)/.test(e.message) && !e.message.includes(CREDENTIAL),
271 );
272});
274test("a box's sign-in is kept per server, and logout forgets it even when the server is down", async () => {
275 const h = home();
276 writeBox(record, h);
277 assert.equal(readBox("https://abso.example", h), undefined, "another server's box isn't this one");
278 assert.match(boxText(undefined, SERVER), /isn't signed in/);
279 const down = (async () => {
280 throw new Error("offline");
281 }) as typeof fetch;
282 assert.equal(await logout(readBox(SERVER, h)!, down, h), false);
283 assert.equal(readBox(SERVER, h), undefined);
284 assert.equal(forgetBox(SERVER, h), false);
285});
287test("join and init pick the server: --server, ABSO_SERVER, the box's sign-in, then api.abso.dev", () => {
288 const h = home();
289 const args = (server?: string) => ({ positionals: [], flags: new Map(server ? [["server", server]] : []) });
290 const none = { env: {} };
291 assert.equal(serverFrom(args(), none, h), "https://api.abso.dev", "nothing signed in: the abso API");
292 writeBox({ ...record, expiresAt: Date.now() + 1e6 }, h);
293 assert.deepEqual(signedInServers(h), [SERVER]);
294 assert.equal(serverFrom(args(), none, h), SERVER, "the server abso login signed this box in to");
295 assert.deepEqual(signedInServers(h, Date.now() + 2e6), [], "an expired sign-in doesn't count");
296 assert.equal(serverFrom(args(), { env: { ABSO_SERVER: "https://env.example" } }, h), "https://env.example");
297 assert.equal(
298 serverFrom(args("https://flag.example"), { env: { ABSO_SERVER: "https://env.example" } }, h),
299 "https://flag.example",
300 );
301 writeBox({ ...record, server: "https://other.abso.example", expiresAt: Date.now() + 1e6 }, h);
302 writeBox({ ...record, expiresAt: Date.now() + 1e6 }, h);
303 assert.throws(
304 () => serverFrom(args(), none, h),
305 /signed in to https:\/\/other\.abso\.example and https:\/\/staging\.abso\.example: say which with --server/,
306 );
307 assert.equal(serverFrom(args(SERVER), none, h), SERVER, "--server settles it");
308});
310test("join with no --agent takes the box's name, skips names this box holds, and moves past a taken one", async () => {
311 const seen: Seen = [];
312 const fetcher = server(
313 [
314 [200, { box_id: record.id, agents: [{ project: "abso", agent: "snow", key_id: "k_0" }] }],
315 [409, { error: "that agent name is already in use in this project; pick a new one" }],
316 [200, { key: "abso_sk_new", id: "k_3" }],
317 ],
318 seen,
319 );
320 const got = await mintFreeName(record, "abso", "snow", fetcher);
321 assert.deepEqual(got, { key: "abso_sk_new", id: "k_3", agent: "snow-3" });
322 assert.deepEqual(
323 seen.map((s) => [new URL(s.url).pathname, (s.body as { agent?: string } | null)?.agent ?? null]),
324 [
325 ["/box", null],
326 ["/box/agent-key", "snow-2"],
327 ["/box/agent-key", "snow-3"],
328 ],
329 "snow is this box's already: never minted again, which would replace its key",
330 );
332 const limit = server(
333 [
334 [200, { box_id: record.id, agents: [] }],
335 [409, { error: "this box is at its agent limit" }],
336 ],
337 [],
338 );
339 await assert.rejects(mintFreeName(record, "abso", "snow", limit), /this box is at its agent limit \(5\)/);
340});