Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+133
@@ -0,0 +1,133 @@
|
||||
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,
|
||||
createLegacyEntityFixture,
|
||||
createLegacyRelationshipRowFixture,
|
||||
createLegacyRowFixture,
|
||||
createLegacyRowRelationshipFixture,
|
||||
createStoredApiKeyFixture,
|
||||
installApiIsolationTestHooks,
|
||||
patchApiIsolationConfig,
|
||||
resetApiIsolationFixturesForTests,
|
||||
} from "./fixtures/api-key.js";
|
||||
|
||||
installApiIsolationTestHooks();
|
||||
|
||||
describe("api-key isolation fixtures", () => {
|
||||
test("exposes stable default runtime and row fixtures", () => {
|
||||
const apiKey = createStoredApiKeyFixture();
|
||||
const entity = createLegacyEntityFixture();
|
||||
const row = createLegacyRowFixture();
|
||||
const relationshipRow = createLegacyRelationshipRowFixture();
|
||||
const rowRelationship = createLegacyRowRelationshipFixture();
|
||||
|
||||
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, entities: [] });
|
||||
expect(entity).toMatchObject({ id: "entity_1", name: "contact" });
|
||||
expect(row).toMatchObject({ id: "row_1", entityId: "entity_1", tenantId: "tenant_1", parentRows: [], childRows: [] });
|
||||
expect(relationshipRow).toEqual({ id: "row_1", entityId: "entity_1", tenantId: "tenant_1" });
|
||||
expect(rowRelationship).toMatchObject({
|
||||
id: "row_relationship_1",
|
||||
parentId: "parent_row_1",
|
||||
childId: "child_row_1",
|
||||
relationship: { parentId: "parent_entity_1", childId: "child_entity_1" },
|
||||
});
|
||||
});
|
||||
|
||||
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 } });
|
||||
const row = createLegacyRowFixture("row_tenant_2", { tenantId: "tenant_2" });
|
||||
|
||||
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(row).toMatchObject({ id: "row_tenant_2", tenantId: "tenant_2" });
|
||||
});
|
||||
|
||||
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", protocol: "https" },
|
||||
origins: { web: "https://web.tenant-2.test" },
|
||||
analytics: { token: "analytics_tenant2" },
|
||||
mailjet: { webhookSecret: "mailer_tenant2_whsec" },
|
||||
stripe: { secretKey: "sk_test_tenant2", webhookSigningSecret: "whsec_tenant2" },
|
||||
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", appBaseDomain: "tenant-2.test", protocol: "https" },
|
||||
origins: { web: "https://web.tenant-2.test" },
|
||||
analytics: { token: "analytics_tenant2" },
|
||||
mailjet: { webhookSecret: "mailer_tenant2_whsec" },
|
||||
stripe: { secretKey: "sk_test_tenant2", webhookSigningSecret: "whsec_tenant2" },
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user