- 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=17&format=json
191 linesFirst import 99% · review 1%Last change review in version 17, 2 versions changed it
You're reading version 17. The newest is 318, and 10 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 { boxPublic, boxText, forgetBox, login, logout, mintKey, readBox, writeBox } from "../src/box.ts";
12const SERVER = "https://staging.abso.example";
13const CREDENTIAL = "abso_bx_the-secret-credential";
14const grant = {
15 projects: [
16 { project: "abso", role: "writer" },
17 { project: "game", role: "reader" },
18 ],
19 max_agents: 5,
20};
21const EXPIRES = "2026-10-15T00:00:00.000Z";
22const record = { server: SERVER, id: "b_0123abcd", credential: CREDENTIAL, grant, expiresAt: Date.parse(EXPIRES) };
24const home = () => mkdtempSync(join(tmpdir(), "abso-box-"));
26type Seen = { url: string; auth: string | null; body: unknown }[];
27function server(answers: [number, unknown][], seen: Seen): typeof fetch {
28 return (async (url, init) => {
29 seen.push({
30 url: String(url),
31 auth: new Headers(init?.headers).get("authorization"),
32 body: JSON.parse(String(init?.body ?? "null")),
33 });
34 const [status, body] = answers.shift() ?? [500, { error: "no more answers" }];
35 return new Response(JSON.stringify(body), { status });
36 }) as typeof fetch;
37}
39const started = {
40 device_code: "device-code-long-enough",
41 user_code: "WXYZ-BCDF",
42 verification_uri_complete: "https://abso.dev/login/box?code=WXYZ-BCDF",
43 expires_in: 60,
44 interval: 2,
45};
47function clock() {
48 let t = 0;
49 return {
50 sleep: async (ms: number) => {
51 t += ms;
52 },
53 now: () => t,
54 slept: () => t,
55 };
56}
58test("login shows the code, waits while it's pending, and keeps the credential in the user folder", async () => {
59 const h = home();
60 const seen: Seen = [];
61 const said: string[] = [];
62 const c = clock();
63 const got = await login(
64 `${SERVER}/some/path`,
65 {
66 fetch: server(
67 [
68 [200, started],
69 [400, { error: "authorization_pending" }],
70 [400, { error: "slow_down" }],
71 [200, { box_id: "b_0123abcd", box_token: CREDENTIAL, expires_at: EXPIRES, grant }],
72 ],
73 seen,
74 ),
75 sleep: c.sleep,
76 now: c.now,
77 say: (line) => said.push(line),
78 },
79 h,
80 );
81 assert.equal(got.server, SERVER);
82 assert.deepEqual(
83 seen.map((s) => s.url),
84 [`${SERVER}/login/device`, `${SERVER}/login/token`, `${SERVER}/login/token`, `${SERVER}/login/token`],
85 );
86 assert.deepEqual(seen[1]?.body, { device_code: "device-code-long-enough" });
87 assert.equal(c.slept(), 2000 + 2000 + 7000, "slow_down adds 5 s to the wait");
88 assert.ok(
89 seen.every((s) => s.auth === null),
90 "nothing is sent with login's calls",
91 );
92 assert.deepEqual(said, [
93 "To sign this box in, open https://abso.dev/login/box?code=WXYZ-BCDF and approve the code WXYZ-BCDF.",
94 ]);
95 assert.equal(readBox(SERVER, h)?.credential, CREDENTIAL);
96 const files = readdirSync(join(h, ".abso", "boxes"));
97 assert.equal(files.length, 1);
98 assert.ok(readFileSync(join(h, ".abso", "boxes", files[0]!), "utf8").includes(CREDENTIAL));
99 assert.equal(boxText(got, SERVER, 0).includes(CREDENTIAL), false);
100 assert.match(boxText(got, SERVER, 0), /abso \(writer\), game \(reader\)\. Up to 5 agents\./);
101 assert.equal(JSON.stringify(boxPublic(got)).includes(CREDENTIAL), false);
102});
104test("a code that's turned down or runs out says so and keeps nothing", async () => {
105 const h = home();
106 const c = clock();
107 const io = { sleep: c.sleep, now: c.now, say: () => {} };
108 await assert.rejects(
109 login(
110 SERVER,
111 {
112 ...io,
113 fetch: server(
114 [
115 [200, started],
116 [400, { error: "access_denied" }],
117 ],
118 [],
119 ),
120 },
121 h,
122 ),
123 /was turned down/,
124 );
125 await assert.rejects(
126 login(
127 SERVER,
128 {
129 ...io,
130 fetch: server(
131 [
132 [200, started],
133 [400, { error: "expired_token" }],
134 ],
135 [],
136 ),
137 },
138 h,
139 ),
140 /ran out\. Run abso login again/,
141 );
142 await assert.rejects(
143 login(
144 SERVER,
145 {
146 ...io,
147 fetch: server(
148 [
149 [200, { ...started, expires_in: 3 }],
150 [400, { error: "authorization_pending" }],
151 [400, { error: "authorization_pending" }],
152 ],
153 [],
154 ),
155 },
156 h,
157 ),
158 /in time\. Run abso login again/,
159 );
160 assert.equal(readBox(SERVER, h), undefined);
161});
163test("a key is minted with the box credential, sent only to the box's own server", async () => {
164 const seen: Seen = [];
165 const minted = await mintKey(
166 record,
167 "abso",
168 "tester-1",
169 server([[200, { key: "abso_sk_x", id: "k_0123abcd" }]], seen),
170 );
171 assert.deepEqual(minted, { key: "abso_sk_x", id: "k_0123abcd" });
172 assert.equal(seen[0]?.url.startsWith(`${SERVER}/`), true);
173 assert.equal(seen[0]?.auth, `Box ${CREDENTIAL}`);
174 await assert.rejects(
175 mintKey(record, "secret-project", "a", server([[403, { error: `outside the grant ${CREDENTIAL}` }]], [])),
176 (e: Error) => /refused \(403\)/.test(e.message) && !e.message.includes(CREDENTIAL),
177 );
178});
180test("a box's sign-in is kept per server, and logout forgets it even when the server is down", async () => {
181 const h = home();
182 writeBox(record, h);
183 assert.equal(readBox("https://abso.example", h), undefined, "another server's box isn't this one");
184 assert.match(boxText(undefined, SERVER), /isn't signed in/);
185 const down = (async () => {
186 throw new Error("offline");
187 }) as typeof fetch;
188 assert.equal(await logout(readBox(SERVER, h)!, down, h), false);
189 assert.equal(readBox(SERVER, h), undefined);
190 assert.equal(forgetBox(SERVER, h), false);
191});