mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.
One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.
Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.
139 lines
5.3 KiB
TypeScript
139 lines
5.3 KiB
TypeScript
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(), "omr-apikey-lifecycle-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
process.env.API_KEY_SECRET = "test-secret";
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
|
|
|
|
const ORIGINAL_OMNIROUTE_API_KEY = process.env.OMNIROUTE_API_KEY;
|
|
const ORIGINAL_ROUTER_API_KEY = process.env.ROUTER_API_KEY;
|
|
|
|
function reset() {
|
|
core.resetDbInstance();
|
|
apiKeysDb.resetApiKeyState();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
delete process.env.OMNIROUTE_API_KEY;
|
|
delete process.env.ROUTER_API_KEY;
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
reset();
|
|
});
|
|
|
|
test.after(() => {
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
if (ORIGINAL_OMNIROUTE_API_KEY === undefined) delete process.env.OMNIROUTE_API_KEY;
|
|
else process.env.OMNIROUTE_API_KEY = ORIGINAL_OMNIROUTE_API_KEY;
|
|
if (ORIGINAL_ROUTER_API_KEY === undefined) delete process.env.ROUTER_API_KEY;
|
|
else process.env.ROUTER_API_KEY = ORIGINAL_ROUTER_API_KEY;
|
|
});
|
|
|
|
async function makeKey(name = "lifecycle-test", machineId = "machine-lifecycle") {
|
|
const created = await apiKeysDb.createApiKey(name, machineId);
|
|
assert.ok(created?.key, "createApiKey returned a key");
|
|
return created;
|
|
}
|
|
|
|
test("validateApiKey returns true for a fresh active key", async () => {
|
|
const created = await makeKey();
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), true);
|
|
});
|
|
|
|
test("validateApiKey rejects revoked keys after revokeApiKey", async () => {
|
|
const created = await makeKey();
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), true);
|
|
|
|
const ok = await apiKeysDb.revokeApiKey(created.id);
|
|
assert.equal(ok, true);
|
|
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
|
});
|
|
|
|
test("validateApiKey rejects keys whose expires_at has passed", async () => {
|
|
const created = await makeKey();
|
|
const past = new Date(Date.now() - 60_000).toISOString();
|
|
const ok = await apiKeysDb.setApiKeyExpiry(created.id, past);
|
|
assert.equal(ok, true);
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
|
});
|
|
|
|
test("validateApiKey accepts keys with future expires_at", async () => {
|
|
const created = await makeKey();
|
|
const future = new Date(Date.now() + 60 * 60_000).toISOString();
|
|
const ok = await apiKeysDb.setApiKeyExpiry(created.id, future);
|
|
assert.equal(ok, true);
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), true);
|
|
});
|
|
|
|
test("validateApiKey rejects deactivated keys (is_active=false)", async () => {
|
|
const created = await makeKey();
|
|
const ok = await apiKeysDb.updateApiKeyPermissions(created.id, { isActive: false });
|
|
assert.equal(ok, true);
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
|
});
|
|
|
|
test("revokeApiKey is idempotent and returns false for missing id", async () => {
|
|
const created = await makeKey();
|
|
assert.equal(await apiKeysDb.revokeApiKey(created.id), true);
|
|
assert.equal(await apiKeysDb.revokeApiKey(created.id), true);
|
|
assert.equal(await apiKeysDb.revokeApiKey("00000000-0000-0000-0000-000000000000"), false);
|
|
});
|
|
|
|
test("getApiKeyMetadata exposes lifecycle and policy fields", async () => {
|
|
const created = await makeKey();
|
|
await apiKeysDb.setApiKeyExpiry(created.id, new Date(Date.now() + 86_400_000).toISOString());
|
|
|
|
const md = await apiKeysDb.getApiKeyMetadata(created.key);
|
|
assert.ok(md);
|
|
assert.equal(md!.isActive, true);
|
|
assert.equal(md!.revokedAt, null);
|
|
assert.ok(md!.expiresAt && Date.parse(md!.expiresAt) > Date.now());
|
|
assert.deepEqual(md!.ipAllowlist, []);
|
|
assert.deepEqual(md!.scopes, []);
|
|
});
|
|
|
|
test("validateApiKey accepts configured environment API keys", async () => {
|
|
process.env.OMNIROUTE_API_KEY = "sk-env-lifecycle-test";
|
|
assert.equal(await apiKeysDb.validateApiKey("sk-env-lifecycle-test"), true);
|
|
});
|
|
|
|
test("validateApiKey updates last_used_at for persisted keys", async () => {
|
|
const created = await makeKey();
|
|
|
|
assert.equal(await apiKeysDb.validateApiKey(created.key), true);
|
|
|
|
const db = core.getDbInstance() as {
|
|
prepare(sql: string): { get(id: string): { last_used_at: string | null } | undefined };
|
|
};
|
|
const row = db.prepare("SELECT last_used_at FROM api_keys WHERE id = ?").get(created.id);
|
|
|
|
assert.ok(row?.last_used_at, "last_used_at should be set on successful validation");
|
|
assert.ok(Date.parse(row.last_used_at) > 0, "last_used_at should be an ISO timestamp");
|
|
});
|
|
|
|
test("getApiKeyMetadata returns proxyId for a key with proxy_id set", async () => {
|
|
const created = await makeKey("proxy-test", "machine-proxy");
|
|
|
|
const ok = await apiKeysDb.updateApiKeyPermissions(created.id, { proxyId: "test-proxy-id" });
|
|
assert.equal(ok, true);
|
|
|
|
const metadata = await apiKeysDb.getApiKeyMetadata(created.key);
|
|
assert.ok(metadata);
|
|
assert.equal(metadata!.proxyId, "test-proxy-id");
|
|
});
|
|
|
|
test("getApiKeyMetadata returns proxyId null for a key without proxy_id", async () => {
|
|
const created = await makeKey("no-proxy-test", "machine-no-proxy");
|
|
|
|
const metadata = await apiKeysDb.getApiKeyMetadata(created.key);
|
|
assert.ok(metadata);
|
|
assert.equal(metadata!.proxyId, null);
|
|
});
|