Versions 59 to 318
new file, 171 lines
1+// abso init and join sign in with the box sign-in, and use this machine's ~/.abso/token only with --token-file. A 2+// project is never made or joined with a shared credential that happened to be in a file, and both say what they sign 3+// in with. The client runs as its own process with its own home, so nothing on this machine is read. 5+import assert from "node:assert/strict"; 6+import { execFile } from "node:child_process"; 7+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; 8+import { createServer, type IncomingHttpHeaders, type Server } from "node:http"; 9+import type { AddressInfo } from "node:net"; 10+import { tmpdir } from "node:os"; 11+import { join, resolve } from "node:path"; 12+import { after, before, describe, it } from "node:test"; 13+import { parseArgs } from "../src/args.ts"; 14+import { writeBox } from "../src/box.ts"; 15+import { tokenCredential } from "../src/join-commands.ts"; 16+import { type Fake, startFake } from "./fake-server.ts"; 18+const CLIENT = resolve(import.meta.dirname, "..", "abso.ts"); 19+const TOKEN = "file-token-not-secret"; 20+const CREDENTIAL = "abso_bx_box-credential-not-secret"; 22+// Stands in for a server that takes box sign-ins: it notes each request, then answers 500 so the run stops there. 25+const heard: { url: string; headers: IncomingHttpHeaders }[] = []; 26+const dirs: string[] = []; 28+ const dir = mkdtempSync(join(tmpdir(), "abso-test-init-credential-")); 34+ fake = await startFake(0, TOKEN); 35+ boxes = createServer((req, res) => { 36+ heard.push({ url: req.url ?? "", headers: req.headers }); 37+ res.writeHead(500, { "content-type": "application/json" }).end('{"error":"stop here"}'); 39+ await new Promise<void>((done) => boxes.listen(0, "127.0.0.1", done)); 40+ boxesUrl = `http://127.0.0.1:${(boxes.address() as AddressInfo).port}`; 45+ for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); 48+/** A home holding a token file, and a box sign-in for `boxFor` when given. */ 49+function home(boxFor?: string): string { 51+ mkdirSync(join(dir, ".abso"), { recursive: true }); 52+ writeFileSync(join(dir, ".abso", "token"), `${TOKEN}\n`); 58+ credential: CREDENTIAL, 59+ grant: { projects: [], scopes: [{ kind: "member-all", role: "owner" }] }, 60+ expiresAt: Date.now() + 86_400_000, 71+): Promise<{ code: number; stdout: string; stderr: string }> { 72+ const env: Record<string, string> = { 73+ PATH: process.env.PATH ?? "", 75+ USERPROFILE: homeDir, 76+ AVCS_SERVERS_FILE: "no-servers.json", 77+ ABSO_NO_BROWSER: "1", 79+ if (process.env.CLAUDE_CONFIG_DIR) env.CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR; 80+ if (process.env.SystemRoot) env.SystemRoot = process.env.SystemRoot; 81+ return new Promise((done) => { 82+ execFile(process.execPath, [CLIENT, ...args], { cwd, env }, (error, stdout, stderr) => 83+ done({ code: typeof error?.code === "number" ? error.code : 0, stdout, stderr }), 88+function folder(): string { 90+ writeFileSync(join(dir, "a.txt"), "hi\n"); 94+const FLAGS = { "token-file": "boolean" } as const; 96+describe("what init and join sign in with", () => { 97+ it("the token file only with --token-file, never into a folder with a key; ABSO_TOKEN as given", () => { 98+ const server = "http://127.0.0.1:1"; 99+ const file = () => TOKEN; 100+ const none = parseArgs("init", [], FLAGS); 101+ const asked = parseArgs("init", ["--token-file"], FLAGS); 102+ assert.equal(tokenCredential(none, {}, server, temp(), file), undefined, "a token file alone is not used"); 103+ assert.deepEqual(tokenCredential(asked, {}, server, temp(), file), { 105+ words: "the token file, ~/.abso/token, as --token-file asks", 107+ assert.throws(() => tokenCredential(asked, {}, server, temp(), () => undefined), /this machine has none/); 108+ const keyed = temp(); 109+ mkdirSync(join(keyed, ".abso")); 110+ writeFileSync(join(keyed, ".abso", "key"), "{}"); 111+ assert.throws(() => tokenCredential(asked, {}, server, keyed, file), /already has a key/); 112+ assert.equal(tokenCredential(none, { ABSO_TOKEN: "env-token" }, server, temp(), file)?.words, "ABSO_TOKEN"); 115+ it("init with a token file and a box sign-in, no flag: signs in with the box, and says so", async () => { 116+ const run = await client(home(boxesUrl), folder(), "init", "--project", "made", "--server", boxesUrl); 117+ assert.match(run.stderr, /Signing in with this box's sign-in, b_0123abcd\./); 118+ assert.ok(heard.length > 0); 120+ heard.every((h) => h.headers.authorization === `Box ${CREDENTIAL}`), 121+ "every request carried the box's sign-in", 124+ heard.every((h) => !String(h.headers.authorization).includes(TOKEN)), 125+ "the token file was never sent", 129+ it("init with only a token file, no flag: refused before anything is made, naming --token-file", async () => { 130+ const run = await client(home(), folder(), "init", "--project", "unasked", "--agent", "me", "--server", fake.url); 131+ assert.notEqual(run.code, 0); 132+ assert.match(run.stderr, /abso login signs it in\. ~\/\.abso\/token is used only with --token-file\./); 133+ const status = await fetch(`${fake.url}/p/unasked/status`, { headers: { authorization: `Bearer ${TOKEN}` } }); 134+ assert.equal(status.status, 404, "no project was made"); 137+ it("init and join with --token-file: sign in with the file, and say so", async () => { 138+ const homeDir = home(); 139+ const made = await client( 151+ assert.equal(made.code, 0, made.stderr); 152+ assert.match(made.stderr, /Signing in with the token file, ~\/\.abso\/token, as --token-file asks\./); 153+ assert.match(made.stdout, /Created project asked/); 154+ const unasked = await client(homeDir, temp(), "join", "asked", "--agent", "other", "--server", fake.url); 155+ assert.notEqual(unasked.code, 0, "join without the flag sends no token"); 156+ assert.match(unasked.stderr, /~\/\.abso\/token is used only with --token-file\./); 157+ const joined = await client( 168+ assert.equal(joined.code, 0, joined.stderr); 169+ assert.match(joined.stderr, /Signing in with the token file/);