Files
abode/test/shared/apikeys.ts
T
codingetandClaude da4e597f73 feat(tests): add unit and integration test suite (node:test)
Adds 215 tests across three tiers using node:test + node:assert/strict
(no new test framework dependencies):

- test/tools/ — middleware and utility tests (token, hash, authenticate,
  jsonBody, convertError, validators)
- test/shared/ — DbInterface/BackendDbInterface contract suites reusable
  across backends (users, abodes, residents, apikeys, sessions, auth)
- test/backends/sqlite/ — SQLite-private tests (sql builder, WrappedDb,
  migrator) + shared suites via SqliteInterface
- test/backends/api/ — ApiInterface unit tests + shared suites via a
  live Koa server backed by SQLite

Also fixes four bugs uncovered by the tests:
- ApiInterface: path params leaked into query string (slice(1) fix)
- ApiInterface: calling res.json() on 204 No Content responses
- SqliteInterface.updateResident: missing comma in SET clause
- WrappedBetterSqlite3Db: readonly:undefined rejected by better-sqlite3

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 11:33:32 +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;
}
);
});
});
}