Files
OmniRoute/tests/unit/models-catalog-envkey-6406.test.ts
Diego Rodrigues de Sa e Souza 8fb3a2c379 chore(quality): v3.8.46 pre-flight — type test anys, rebaseline cycle drift, allowlist #6303 test consolidation
- Type 12 no-explicit-any errors in 3 new test files (real types, no masking):
  chat-early-schema-validation-6412, models-catalog-envkey-6406, zed-provider.
- Suppress MitmProxyTab.tsx no-html-link-for-pages (false positive: the link is
  an /api/settings/mitm cert download, an API route not a Next page).
- Allowlist tests/integration/v1-contracts-behavior.test.ts net -2 asserts: #6303
  moved embedding/image shape coverage to models-catalog-route + specialty tests.
- Rebaseline cycle drift measured on the release tip (captain fixes are net-zero,
  verified): cognitive 877->882, cyclomatic 2035->2050, file-size proxies/chat/
  ApiManagerPageClient/ProxyRegistryManager + models-catalog-route testcap +5.
- Fix stale CLAUDE.md: no-explicit-any is error (not warn) in tests/ since #6218.
2026-07-07 07:42:42 -03:00

118 lines
4.4 KiB
TypeScript

// Regression guard for issue #6406 — `/v1/models` returned the full catalog
// unauthenticated but 0 models when an env-var master key (OMNIROUTE_API_KEY /
// ROUTER_API_KEY) was presented. Root cause: `isModelAllowedForKey` denies when
// `getApiKeyMetadata` returns null, and env-var keys have no DB row.
// Fix: skip the per-model filter when apiKey has no metadata (env-var master key).
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-envkey-6406-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "catalog-envkey-secret";
const core = await import("../../src/lib/db/core.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const v1ModelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");
// `getUnifiedModelsResponse` returns the OpenAI-shaped `{object, data}` catalog
// list (see catalog.ts's `responseBody`); only `data[].id` is asserted here.
interface ModelsCatalogResponseBody {
object: string;
data: Array<{ id: string; [key: string]: unknown }>;
}
async function resetStorage() {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function seedOpenAi() {
return providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: "openai-envkey-6406",
apiKey: "sk-test",
isActive: true,
testStatus: "active",
providerSpecificData: {},
});
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
delete process.env.OMNIROUTE_API_KEY;
});
test("#6406 env-var master key (no DB metadata) sees the full catalog, not 0 models", async () => {
await seedOpenAi();
// Baseline: unauth response.
const unauthResponse = await v1ModelsCatalog.getUnifiedModelsResponse(
new Request("http://localhost/api/v1/models")
);
const unauthBody = (await unauthResponse.json()) as ModelsCatalogResponseBody;
assert.equal(unauthResponse.status, 200);
const unauthCount = unauthBody.data.length;
assert.ok(unauthCount > 0, `unauth baseline must have models, got ${unauthCount}`);
// Env-var master key path — no DB row, so getApiKeyMetadata returns null.
const envKey = "sk-envkey-6406-master";
process.env.OMNIROUTE_API_KEY = envKey;
const authResponse = await v1ModelsCatalog.getUnifiedModelsResponse(
new Request("http://localhost/api/v1/models", {
headers: { Authorization: `Bearer ${envKey}` },
})
);
const authBody = (await authResponse.json()) as ModelsCatalogResponseBody;
assert.equal(authResponse.status, 200);
// Regression: before the fix, this collapsed to 0. Now it matches the unauth
// catalog (env-var master key has no per-key restrictions).
assert.equal(
authBody.data.length,
unauthCount,
`env-var key catalog (${authBody.data.length}) must equal unauth catalog (${unauthCount}); auth should GATE access, not FILTER inventory when the key carries no restrictions`
);
assert.notEqual(authBody.data.length, 0, "env-var master key must not collapse catalog to 0");
});
test("#6406 DB-backed key with allowedModels still filters (unchanged behavior)", async () => {
await seedOpenAi();
const key = await apiKeysDb.createApiKey("envkey-6406-filter", "machine-envkey-6406");
await apiKeysDb.updateApiKeyPermissions(key.id, { allowedModels: ["openai/*"] });
const response = await v1ModelsCatalog.getUnifiedModelsResponse(
new Request("http://localhost/api/v1/models", {
headers: { Authorization: `Bearer ${key.key}` },
})
);
const body = (await response.json()) as ModelsCatalogResponseBody;
const ids: string[] = body.data.map((item) => item.id);
assert.equal(response.status, 200);
assert.ok(
ids.some((id) => id.startsWith("openai/")),
"DB-backed key with allowedModels=['openai/*'] must still see openai/* models"
);
assert.equal(
ids.some((id) => id.startsWith("claude/") || id.startsWith("cc/")),
false,
"DB-backed allowedModels filter must still exclude non-matching families"
);
});