Files
abode/test/shared/apikeys.ts
T
codingetandCodex 31d4636dde
CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
ci: add pull request quality gates
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
2026-07-22 21:50:29 +00:00

103 lines
3.1 KiB
TypeScript

import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import type { DbInterface } from "../../src/db/types/DbInterface.js";
import { NotFoundAbodeError } from "../../src/db/types/errors.js";
import { hashPassword } from "../../src/util/hash.js";
export function runApikeyTests(
name: string,
getDb: () => Promise<{ db: DbInterface; close(): void }>,
): void {
describe(`${name}: apikeys`, async () => {
let db: DbInterface;
let close: () => void;
let uid: string;
before(async () => {
({ db, close } = await getDb());
const pw = await hashPassword("apikey-password");
const user = await db.createUser({
email: `apikey-user-${Date.now()}@test.example`,
name: "Apikey User",
password: pw,
flags: {},
});
uid = user.uid;
});
after(() => close());
it("createApikey returns [ClientApikey, at_token]", async () => {
const [apikey, token] = await db.createApikey({
uid,
name: "Test Key",
permissions: {},
});
assert.ok(apikey.kid, "has kid");
assert.equal(apikey.uid, uid);
assert.equal(apikey.name, "Test Key");
assert.ok(!("token" in apikey), "ClientApikey has no token field");
assert.ok(token.startsWith("at_"), `token starts with at_: ${token}`);
});
it("listApikeysByUser returns created key", async () => {
const [created] = await db.createApikey({
uid,
name: "List Key",
permissions: {},
});
const keys = await db.listApikeysByUser(uid);
assert.ok(Array.isArray(keys));
const found = keys.find((k) => k.kid === created.kid);
assert.ok(found, "created key appears in listApikeysByUser");
});
it("getApikeyById returns the key", async () => {
const [created] = await db.createApikey({
uid,
name: "GetById Key",
permissions: {},
});
const found = await db.getApikeyById(created.kid);
assert.equal(found.kid, created.kid);
assert.equal(found.name, "GetById Key");
});
it("getApikeyById throws NotFoundAbodeError for unknown kid", async () => {
await assert.rejects(
() => db.getApikeyById("00000000-0000-0000-0000-000000000000"),
(err) => {
assert.ok(err instanceof NotFoundAbodeError);
return true;
},
);
});
it("deleteApikeyById removes the key", async () => {
const [created] = await db.createApikey({
uid,
name: "Delete Key",
permissions: {},
});
await db.deleteApikeyById(created.kid);
await assert.rejects(
() => db.getApikeyById(created.kid),
(err) => {
assert.ok(err instanceof NotFoundAbodeError);
return true;
},
);
});
it("deleteApikeyById throws NotFoundAbodeError for unknown kid", async () => {
await assert.rejects(
() => db.deleteApikeyById("00000000-0000-0000-0000-000000000000"),
(err) => {
assert.ok(err instanceof NotFoundAbodeError);
return true;
},
);
});
});
}