Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/api/src/isolation/api-key-fixtures.test.ts
T

128 lines
5.4 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { app } from "../app.js";
import { getApiRuntimeConfig } from "../config.js";
import {
TEST_API_CONFIG,
TEST_JWT_SECRET,
configureApiIsolationDependenciesForTests,
createStoredApiKeyFixture,
installApiIsolationTestHooks,
patchApiIsolationConfig,
resetApiIsolationFixturesForTests,
} from "./fixtures/api-key.js";
installApiIsolationTestHooks();
describe("api-key isolation fixtures", () => {
test("exposes stable default runtime and api-key fixtures", () => {
const apiKey = createStoredApiKeyFixture();
expect(TEST_API_CONFIG.apiAccessToken).toBe("service-secret");
expect(TEST_API_CONFIG.jwtSecret).toBe(TEST_JWT_SECRET);
expect(TEST_JWT_SECRET).toBe("scope-secret");
expect(getApiRuntimeConfig()).toMatchObject({ apiAccessToken: "service-secret", jwtSecret: "scope-secret" });
expect(apiKey).toMatchObject({ tenantId: "tenant_1", tenant: { id: "tenant_1" }, active: true });
});
test("patches tenant-related fixture values without hiding mismatches", () => {
patchApiIsolationConfig({ apiAccessToken: "tenant-2-service", jwtSecret: "tenant-2-secret" });
const apiKey = createStoredApiKeyFixture({ tenantId: "tenant_2" });
const mismatchedApiKey = createStoredApiKeyFixture({ tenantId: "tenant_2", tenant: { id: "tenant_3", name: "Other", slug: "other", deactivatedReason: null } });
// tenant provided without an explicit tenantId → tenantId is derived from tenant.id.
const derivedTenantApiKey = createStoredApiKeyFixture({ tenant: { id: "tenant_5", name: "Derived", slug: "derived", deactivatedReason: null } });
expect(getApiRuntimeConfig()).toMatchObject({ apiAccessToken: "tenant-2-service", jwtSecret: "tenant-2-secret" });
expect(apiKey).toMatchObject({ tenantId: "tenant_2", tenant: { id: "tenant_2" } });
expect(mismatchedApiKey).toMatchObject({ tenantId: "tenant_2", tenant: { id: "tenant_3" } });
expect(derivedTenantApiKey).toMatchObject({ tenantId: "tenant_5", tenant: { id: "tenant_5" } });
});
test("resets runtime config patches after each test via installed hooks", () => {
patchApiIsolationConfig({ apiAccessToken: "temporary-service" });
resetApiIsolationFixturesForTests();
patchApiIsolationConfig();
expect(getApiRuntimeConfig()).toMatchObject({ apiAccessToken: "service-secret", jwtSecret: "scope-secret" });
});
test("reset callback clears dependency overrides and runtime config", async () => {
patchApiIsolationConfig({ apiAccessToken: "patched-service" });
const checkReadiness = vi.fn().mockResolvedValue(undefined);
const resetDependencies = configureApiIsolationDependenciesForTests({ checkReadiness });
const patchedResponse = await app.request("/readyz");
expect(patchedResponse.status).toBe(200);
expect(checkReadiness).toHaveBeenCalledTimes(1);
expect(getApiRuntimeConfig().apiAccessToken).toBe("patched-service");
resetApiIsolationFixturesForTests();
expect(getApiRuntimeConfig).toThrow();
expect(checkReadiness).toHaveBeenCalledTimes(1);
resetDependencies();
});
test("merges nested config patches without losing other defaults", () => {
patchApiIsolationConfig({
listen: { port: 4242 },
routing: {
appBaseDomain: "tenant-2.test",
webAppHost: "app.tenant-2.test",
adminAppHost: "admin.tenant-2.test",
apiHost: "api.tenant-2.test",
portalAppHost: "portal.tenant-2.test",
protocol: "https",
},
origins: { web: "https://web.tenant-2.test" },
analytics: { token: "analytics_tenant2" },
mailjet: { webhookSecret: "mailer_tenant2_whsec" },
storage: { bucket: "tenant-2-bucket", region: "eu-central-1" },
});
expect(getApiRuntimeConfig()).toMatchObject({
apiAccessToken: "service-secret",
jwtSecret: "scope-secret",
listen: { port: 4242 },
routing: {
apiHost: "api.tenant-2.test",
portalAppHost: "portal.tenant-2.test",
appBaseDomain: "tenant-2.test",
protocol: "https",
},
origins: { web: "https://web.tenant-2.test" },
analytics: { token: "analytics_tenant2" },
mailjet: { webhookSecret: "mailer_tenant2_whsec" },
storage: { bucket: "tenant-2-bucket", region: "eu-central-1" },
});
});
test("reset closure released by configureApiIsolationDependenciesForTests clears its own override", async () => {
const checkReadiness = vi.fn().mockResolvedValue(undefined);
const release = configureApiIsolationDependenciesForTests({ checkReadiness });
const firstResponse = await app.request("/readyz");
expect(firstResponse.status).toBe(200);
expect(checkReadiness).toHaveBeenCalledTimes(1);
release();
await app.request("/readyz");
expect(checkReadiness).toHaveBeenCalledTimes(1);
});
test("configureApiIsolationDependenciesForTests called twice releases the prior override first", async () => {
const firstReadiness = vi.fn().mockResolvedValue(undefined);
const secondReadiness = vi.fn().mockResolvedValue(undefined);
configureApiIsolationDependenciesForTests({ checkReadiness: firstReadiness });
configureApiIsolationDependenciesForTests({ checkReadiness: secondReadiness });
const response = await app.request("/readyz");
expect(response.status).toBe(200);
expect(firstReadiness).not.toHaveBeenCalled();
expect(secondReadiness).toHaveBeenCalledTimes(1);
});
});