Files
elizaos--eliza/plugins/plugin-cloud-apps/__tests__/regenerate-app-api-key.test.ts
T
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

262 lines
7.8 KiB
TypeScript

/**
* REGENERATE_APP_API_KEY action tests: key rotation and the one-time reveal of the new key. The @elizaos/cloud-sdk client is faked (helpers.ts, SDK boundary only); the action runs for real.
*/
import { beforeEach, describe, expect, it, mock } from "bun:test";
import {
captureCallback,
FakeElizaCloudClient,
keyedRuntime,
makeApp,
makeMessage,
resetSdk,
setListApps,
setRegenerateAppApiKey,
unkeyedRuntime,
} from "./helpers";
mock.module("@elizaos/cloud-sdk", () => ({
ElizaCloudClient: FakeElizaCloudClient,
}));
const { regenerateAppApiKeyAction } = await import(
"../src/actions/regenerate-app-api-key.ts"
);
const APP = makeApp({ id: "id-acme", name: "Acme Bot", slug: "acme-bot" });
const NEW_KEY = "eliza_app_rotated_secret_value";
/** Track rotate calls; returns the new key once. */
function trackRotations(): { count: () => number } {
let count = 0;
setRegenerateAppApiKey(() => {
count += 1;
return Promise.resolve({ success: true, apiKey: NEW_KEY });
});
return { count: () => count };
}
describe("REGENERATE_APP_API_KEY", () => {
beforeEach(() => {
resetSdk();
setListApps(() => Promise.resolve({ success: true, apps: [APP] }));
});
it("validates only when a Cloud API key is present", async () => {
expect(
await regenerateAppApiKeyAction.validate(
keyedRuntime(),
makeMessage("x"),
),
).toBe(true);
expect(
await regenerateAppApiKeyAction.validate(
unkeyedRuntime(),
makeMessage("x"),
),
).toBe(false);
});
it("first ask: confirms and does NOT rotate", async () => {
const rotations = trackRotations();
const cb = captureCallback();
const result = await regenerateAppApiKeyAction.handler(
keyedRuntime(),
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
cb.fn,
);
expect(rotations.count()).toBe(0);
expect((result?.data as { rotated: boolean }).rotated).toBe(false);
expect(
(result?.data as { confirmationRequired: boolean }).confirmationRequired,
).toBe(true);
const prompt = cb.calls[0]?.text ?? "";
expect(prompt).toContain("Acme Bot");
expect(prompt.toLowerCase()).toContain("immediately");
// The new key must NOT leak in the confirmation prompt.
expect(prompt).not.toContain(NEW_KEY);
});
it("explicit confirmation: rotates once and shows the new key exactly once", async () => {
const rotations = trackRotations();
const runtime = keyedRuntime();
const cb = captureCallback();
await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
cb.fn,
);
const result = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("confirmo"),
undefined,
{ confirm: true },
cb.fn,
);
expect(rotations.count()).toBe(1);
expect(result?.success).toBe(true);
expect((result?.data as { rotated: boolean }).rotated).toBe(true);
// The key is shown ONCE in the user-facing reply...
const replies = cb.calls.filter((c) => (c.text ?? "").includes(NEW_KEY));
expect(replies).toHaveLength(1);
expect(cb.calls.at(-1)?.text?.toLowerCase()).toContain(
"won't be shown again",
);
// ...but never placed in the persisted `data` or summary `text`.
expect(JSON.stringify(result?.data)).not.toContain(NEW_KEY);
expect(result?.text ?? "").not.toContain(NEW_KEY);
});
it("handles a rotate that returns no key", async () => {
setRegenerateAppApiKey(() => Promise.resolve({ success: true }));
const runtime = keyedRuntime();
const cb = captureCallback();
await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
cb.fn,
);
const result = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("confirm"),
undefined,
{ confirm: true },
cb.fn,
);
expect(result?.success).toBe(false);
expect((result?.data as { reason: string }).reason).toBe("no_key_returned");
});
it("structured confirm without a pending prompt does NOT rotate", async () => {
const rotations = trackRotations();
const result = await regenerateAppApiKeyAction.handler(
keyedRuntime(),
makeMessage("confirm"),
undefined,
{ confirm: true },
captureCallback().fn,
);
expect(rotations.count()).toBe(0);
expect((result?.data as { reason: string }).reason).toBe(
"no_pending_confirmation",
);
});
it("structured cancellation consumes the pending prompt without rotating", async () => {
const rotations = trackRotations();
const runtime = keyedRuntime();
await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
captureCallback().fn,
);
const result = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("cancel"),
undefined,
{ confirm: false },
captureCallback().fn,
);
expect(rotations.count()).toBe(0);
expect((result?.data as { canceled: boolean }).canceled).toBe(true);
});
it("returns not-found for an unknown app (no rotate)", async () => {
const rotations = trackRotations();
const result = await regenerateAppApiKeyAction.handler(
keyedRuntime(),
makeMessage("rotate Zephyr"),
undefined,
undefined,
captureCallback().fn,
);
expect(rotations.count()).toBe(0);
expect((result?.data as { reason: string }).reason).toBe("not_found");
});
it("degrades gracefully with no Cloud API key", async () => {
const result = await regenerateAppApiKeyAction.handler(
unkeyedRuntime(),
makeMessage("rotate Acme Bot"),
undefined,
undefined,
captureCallback().fn,
);
expect((result?.data as { reason: string }).reason).toBe("no_key");
});
it("confirm naming a DIFFERENT app refuses, rotates nothing, and clears the pending", async () => {
const rotations = trackRotations();
const runtime = keyedRuntime();
const cb = captureCallback();
await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
cb.fn,
);
const result = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("yes — rotate the key for Beta Dashboard"),
undefined,
{ parameters: { confirm: true, appName: "Beta Dashboard" } },
cb.fn,
);
expect(rotations.count()).toBe(0);
expect(result?.success).toBe(false);
expect((result?.data as { reason: string }).reason).toBe(
"confirm_target_mismatch",
);
const reply = cb.calls.at(-1)?.text ?? "";
expect(reply).toContain("Beta Dashboard");
expect(reply).toContain("Acme Bot");
expect(reply).not.toContain(NEW_KEY);
const followUp = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("confirm"),
undefined,
{ confirm: true },
cb.fn,
);
expect(rotations.count()).toBe(0);
expect((followUp?.data as { reason: string }).reason).toBe(
"no_pending_confirmation",
);
});
it("surfaces a rotate API error", async () => {
setRegenerateAppApiKey(() => Promise.reject(new Error("boom")));
const runtime = keyedRuntime();
await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("regenerate the API key for Acme Bot"),
undefined,
undefined,
captureCallback().fn,
);
const result = await regenerateAppApiKeyAction.handler(
runtime,
makeMessage("confirm"),
undefined,
{ confirm: true },
captureCallback().fn,
);
expect(result?.success).toBe(false);
expect((result?.data as { reason: string }).reason).toBe("error");
});
});