peppa/packages/testkit/test/claude-auth.test.ts
Асель 3f55248150 PEP-13: connect Claude from the web for remote users
- Settings → Claude: Peppa runs 'claude setup-token' in tmux, shows the
  claude.com sign-in link, the user pastes the code into the form; the
  1-year token is stored encrypted on the user (users.claude_token_enc)
- Claude sessions of the owner's projects get CLAUDE_CODE_OAUTH_TOKEN;
  projects without an owner token keep using the machine login
- disconnect / reconnect; setup times out after 10 min and its tmux
  session (and scrollback with the token) is killed right after saving
- mock-claude setup-token; 2 new tests (56 e2e total, green)
- verified on prod: the button yields the real claude.com authorize URL
- OpenSpec: pep-13 archived into spec agent-credentials

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 22:06:37 +05:00

55 lines
2.8 KiB
TypeScript

// PEP-13: connect Claude from the web (setup-token in tmux) and use the token in sessions.
import assert from "node:assert/strict";
import { after, before, describe, test } from "node:test";
import { core, db, project, runner, teardown } from "./helpers.ts";
process.env.PEPPA_SECRET = "mock-secret-for-tests";
let user: Awaited<ReturnType<typeof core.upsertUserFromGitea>>;
before(async () => {
user = await core.upsertUserFromGitea(db, { id: 31, login: "mock-claude-user" }, { access_token: "x" });
});
after(teardown);
describe("connect Claude", () => {
test("wrong code → error, nothing stored; right code → encrypted token", async () => {
const s1 = await runner.startClaudeSetup(db, user.id);
assert.match(s1.url, /^https:\/\/claude\.com\/cai\/oauth\/authorize\?/);
assert.deepEqual((await runner.pendingClaudeSetup(db, user.id))?.url, s1.url);
await assert.rejects(runner.finishClaudeSetup(db, user.id, "mock-bad-code"), /не принял код/);
assert.equal((await core.getUser(db, user.id))?.claudeTokenEnc, null);
assert.equal(await runner.pendingClaudeSetup(db, user.id), null, "failed setup is cleaned up");
await assert.rejects(runner.finishClaudeSetup(db, user.id, "mock-good-1"), /не начато/);
await runner.startClaudeSetup(db, user.id);
await runner.finishClaudeSetup(db, user.id, "mock-good-1");
const u = (await core.getUser(db, user.id))!;
assert.ok(u.claudeTokenEnc && !u.claudeTokenEnc.includes("sk-ant"), "stored encrypted");
assert.match(core.decrypt(u.claudeTokenEnc), /^sk-ant-oat01-mock/);
assert.ok(u.claudeTokenSetAt);
assert.equal(await runner.pendingClaudeSetup(db, user.id), null);
});
test("sessions of the owner's projects get the token; other projects and disconnect don't", async () => {
const token = core.decrypt((await core.getUser(db, user.id))!.claudeTokenEnc!);
const mine = await project({ turns: [[{ say: "ok" }]] });
await core.setProjectOwner(db, (await core.getProject(db, mine.key)).id, user.id);
const sid = await mine.start();
const start = await mine.waitLog("started", (e) => e.ev === "start");
assert.equal(start.oauthToken, `…${token.slice(-4)}`);
await runner.stopSession(db, sid);
const other = await project({ turns: [[{ say: "ok" }]] });
const sid2 = await other.start();
assert.equal((await other.waitLog("started", (e) => e.ev === "start")).oauthToken, null);
await runner.stopSession(db, sid2);
await core.setClaudeToken(db, user.id, null);
const again = await project({ turns: [[{ say: "ok" }]] });
await core.setProjectOwner(db, (await core.getProject(db, again.key)).id, user.id);
const sid3 = await again.start();
assert.equal((await again.waitLog("started", (e) => e.ev === "start")).oauthToken, null);
await runner.stopSession(db, sid3);
});
});