Files
Moltbot/src/agents/model-auth.test.ts
Vincent Koc 42e3d8d693 Secrets: add inline allowlist review set (#38314)
* Secrets: add inline allowlist review set

* Secrets: narrow detect-secrets file exclusions

* Secrets: exclude Docker fingerprint false positive

* Secrets: allowlist test and docs false positives

* Secrets: refresh baseline after allowlist updates

* Secrets: fix gateway chat fixture pragma

* Secrets: format pre-commit config

* Android: keep talk mode fixture JSON valid

* Feishu: rely on client timeout injection

* Secrets: allowlist provider auth test fixtures

* Secrets: allowlist onboard search fixtures

* Secrets: allowlist onboard mode fixture

* Secrets: allowlist gateway auth mode fixture

* Secrets: allowlist APNS wake test key

* Secrets: allowlist gateway reload fixtures

* Secrets: allowlist moonshot video fixture

* Secrets: allowlist auto audio fixture

* Secrets: allowlist tiny audio fixture

* Secrets: allowlist embeddings fixtures

* Secrets: allowlist resolve fixtures

* Secrets: allowlist target registry pattern fixtures

* Secrets: allowlist gateway chat env fixture

* Secrets: refresh baseline after fixture allowlists

* Secrets: reapply gateway chat env allowlist

* Secrets: reapply gateway chat env allowlist

* Secrets: stabilize gateway chat env allowlist

* Secrets: allowlist runtime snapshot save fixture

* Secrets: allowlist oauth profile fixtures

* Secrets: allowlist compaction identifier fixture

* Secrets: allowlist model auth fixture

* Secrets: allowlist model status fixtures

* Secrets: allowlist custom onboarding fixture

* Secrets: allowlist mattermost token summary fixtures

* Secrets: allowlist gateway auth suite fixtures

* Secrets: allowlist channel summary fixture

* Secrets: allowlist provider usage auth fixtures

* Secrets: allowlist media proxy fixture

* Secrets: allowlist secrets audit fixtures

* Secrets: refresh baseline after final fixture allowlists

* Feishu: prefer explicit client timeout

* Feishu: test direct timeout precedence
2026-03-06 19:35:26 -05:00

120 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { AuthProfileStore } from "./auth-profiles.js";
import { requireApiKey, resolveAwsSdkEnvVarName, resolveModelAuthMode } from "./model-auth.js";
describe("resolveAwsSdkEnvVarName", () => {
it("prefers bearer token over access keys and profile", () => {
const env = {
AWS_BEARER_TOKEN_BEDROCK: "bearer",
AWS_ACCESS_KEY_ID: "access",
AWS_SECRET_ACCESS_KEY: "secret", // pragma: allowlist secret
AWS_PROFILE: "default",
} as NodeJS.ProcessEnv;
expect(resolveAwsSdkEnvVarName(env)).toBe("AWS_BEARER_TOKEN_BEDROCK");
});
it("uses access keys when bearer token is missing", () => {
const env = {
AWS_ACCESS_KEY_ID: "access",
AWS_SECRET_ACCESS_KEY: "secret", // pragma: allowlist secret
AWS_PROFILE: "default",
} as NodeJS.ProcessEnv;
expect(resolveAwsSdkEnvVarName(env)).toBe("AWS_ACCESS_KEY_ID");
});
it("uses profile when no bearer token or access keys exist", () => {
const env = {
AWS_PROFILE: "default",
} as NodeJS.ProcessEnv;
expect(resolveAwsSdkEnvVarName(env)).toBe("AWS_PROFILE");
});
it("returns undefined when no AWS auth env is set", () => {
expect(resolveAwsSdkEnvVarName({} as NodeJS.ProcessEnv)).toBeUndefined();
});
});
describe("resolveModelAuthMode", () => {
it("returns mixed when provider has both token and api key profiles", () => {
const store: AuthProfileStore = {
version: 1,
profiles: {
"openai:token": {
type: "token",
provider: "openai",
token: "token-value",
},
"openai:key": {
type: "api_key",
provider: "openai",
key: "api-key",
},
},
};
expect(resolveModelAuthMode("openai", undefined, store)).toBe("mixed");
});
it("returns aws-sdk when provider auth is overridden", () => {
expect(
resolveModelAuthMode(
"amazon-bedrock",
{
models: {
providers: {
"amazon-bedrock": {
baseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com",
models: [],
auth: "aws-sdk",
},
},
},
},
{ version: 1, profiles: {} },
),
).toBe("aws-sdk");
});
it("returns aws-sdk for bedrock alias without explicit auth override", () => {
expect(resolveModelAuthMode("bedrock", undefined, { version: 1, profiles: {} })).toBe(
"aws-sdk",
);
});
it("returns aws-sdk for aws-bedrock alias without explicit auth override", () => {
expect(resolveModelAuthMode("aws-bedrock", undefined, { version: 1, profiles: {} })).toBe(
"aws-sdk",
);
});
});
describe("requireApiKey", () => {
it("normalizes line breaks in resolved API keys", () => {
const key = requireApiKey(
{
apiKey: "\n sk-test-abc\r\n",
source: "env: OPENAI_API_KEY",
mode: "api-key",
},
"openai",
);
expect(key).toBe("sk-test-abc");
});
it("throws when no API key is present", () => {
expect(() =>
requireApiKey(
{
source: "env: OPENAI_API_KEY",
mode: "api-key",
},
"openai",
),
).toThrow('No API key resolved for provider "openai"');
});
});