Files
OmniRoute/tests/unit/shared/components/KiroAuthModal.test.tsx
Thiago Reis 3a28b3b5e8 feat: add Kiro API key authentication (#6587)
* feat(oauth): add Kiro long-lived API key auth (#6587)

New /api/oauth/kiro/api-key route + KiroService.validateApiKey let a
Kiro account be linked with a long-lived AWS CodeWhisperer/Kiro API
key instead of the interactive OAuth device flow, with live
per-account model discovery (ListAvailableModels, 5-minute cache)
layered over the existing static registry fallback.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(changelog): re-restore #6587 bullet after release sync

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(merge): restore #6126 clinepass files reverted by release auto-resolve + baseline re-merge

The release sync's auto-resolve reverted sibling PR #6126's clinepass work
(registry, catalog, oauth constants, clineAuth.ts, token-refresh case, tests)
and the file-size baseline — all outside this PR's scope. Restored to the
release versions, re-applied only this PR's own baseline entries, restored the
#6126 CHANGELOG bullet (re-inserting only this PR's own).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* chore(quality): freeze public-creds FP — AWS region default in validateApiKey signature

Same class as the existing minimax fn-param FPs: CRED_KEY_RE matches the
apiKey: param annotation and captures the region default "us-east-1",
which is not a credential.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(kiro): keep hard-failure reject semantics + kill public-creds fn-param FP at the source

- getKiroUsage: exhausted non-auth attempts now REJECT with the last HTTP-status
  failure in the pre-#6587 format (usage-service-hardening relies on it); auth
  failures keep the soft social-auth message.
- validateApiKey: region default moved out of the parameter list (the
  check-public-creds CRED_KEY_RE matches the apiKey: annotation and flags any
  literal in the signature); drops the brittle line-keyed allowlist entry.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: strangersp <strangersp@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
2026-07-09 22:49:06 -03:00

113 lines
3.5 KiB
TypeScript

// @vitest-environment jsdom
import React from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const cleanupCallbacks: Array<() => void> = [];
function makeContainer(): HTMLElement {
const container = document.createElement("div");
document.body.appendChild(container);
cleanupCallbacks.push(() => {
container.remove();
});
return container;
}
function setInputValue(input: HTMLInputElement, value: string): void {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
setter?.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
}
describe("KiroAuthModal", () => {
beforeEach(() => {
(
globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
});
afterEach(() => {
while (cleanupCallbacks.length > 0) {
cleanupCallbacks.pop()?.();
}
document.body.innerHTML = "";
vi.restoreAllMocks();
});
it("shows Google Account and starts Google social login when clicked", async () => {
const { default: KiroAuthModal } = await import("@/shared/components/KiroAuthModal");
const container = makeContainer();
const root = createRoot(container);
const onMethodSelect = vi.fn();
await act(async () => {
root.render(<KiroAuthModal isOpen onClose={vi.fn()} onMethodSelect={onMethodSelect} />);
});
const googleButton = Array.from(container.querySelectorAll("button")).find((button) =>
button.textContent?.includes("Google Account")
);
expect(googleButton).toBeTruthy();
expect(googleButton?.className).not.toContain("hidden");
await act(async () => {
googleButton?.click();
});
expect(onMethodSelect).toHaveBeenCalledWith("social", { provider: "google" });
});
it("notifies API key import success before closing the modal", async () => {
const { default: KiroAuthModal } = await import("@/shared/components/KiroAuthModal");
const container = makeContainer();
const root = createRoot(container);
const calls: string[] = [];
const onMethodSelect = vi.fn(() => calls.push("select"));
const onClose = vi.fn(() => calls.push("close"));
const originalFetch = globalThis.fetch;
globalThis.fetch = vi.fn(async () => {
return new Response(JSON.stringify({ success: true, connection: { id: "conn-1" } }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}) as typeof fetch;
try {
await act(async () => {
root.render(<KiroAuthModal isOpen onClose={onClose} onMethodSelect={onMethodSelect} />);
});
const apiKeyButton = Array.from(container.querySelectorAll("button")).find(
(button) => button.querySelector("h3")?.textContent === "API Key"
);
await act(async () => {
apiKeyButton?.click();
});
const apiKeyInput = container.querySelector("input") as HTMLInputElement;
const saveButton = Array.from(container.querySelectorAll("button")).find((button) =>
button.textContent?.includes("Validate and Save API Key")
);
await act(async () => {
setInputValue(apiKeyInput, "ksk_test_key");
});
await act(async () => {
saveButton?.click();
});
expect(onMethodSelect).toHaveBeenCalledWith("api-key");
expect(onClose).toHaveBeenCalled();
expect(calls).toEqual(["select", "close"]);
} finally {
globalThis.fetch = originalFetch;
}
});
});