|
| 1 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +// The API-key policy methods are OPTIONAL on RoleBaseAccessController so a |
| 5 | +// plugin compiled against an older OSS commit still satisfies the contract |
| 6 | +// (the plugin is built against whichever OSS source its base image carries). |
| 7 | +// LazyController is what turns that partial surface into a total one, and these |
| 8 | +// tests pin the defaults it substitutes — in particular that a missing |
| 9 | +// prepareApiKeyPolicy fails CLOSED rather than resolving to full access. |
| 10 | +// |
| 11 | +// A stand-in for the cloud plugin, which isn't installed in this repo. The |
| 12 | +// factory supplies the specifier, so no real module has to resolve. |
| 13 | +vi.mock("@triggerdotdev/plugins/rbac", () => ({ |
| 14 | + default: { |
| 15 | + create: () => ({ |
| 16 | + // Deliberately omits apiKeyPresets / prepareApiKeyPolicy / |
| 17 | + // describeApiKeyPolicy — this is a pre-contract plugin. |
| 18 | + isUsingPlugin: async () => true, |
| 19 | + }), |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +const prismaPlaceholder = {} as unknown as PrismaClient; |
| 24 | + |
| 25 | +describe("LazyController API-key policy defaults (plugin predates the contract)", () => { |
| 26 | + async function controller() { |
| 27 | + const loader = (await import("./index.js")).default; |
| 28 | + const instance = loader.create(prismaPlaceholder); |
| 29 | + // Guard against a silent fallback: if the mock didn't take, these |
| 30 | + // assertions would be checking the fallback's real implementations. |
| 31 | + await expect(instance.isUsingPlugin()).resolves.toBe(true); |
| 32 | + return instance; |
| 33 | + } |
| 34 | + |
| 35 | + it("reports no preset catalogue rather than throwing", async () => { |
| 36 | + await expect((await controller()).apiKeyPresets("org_123")).resolves.toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("refuses to prepare a policy — including FULL_ACCESS", async () => { |
| 40 | + const result = await ( |
| 41 | + await controller() |
| 42 | + ).prepareApiKeyPolicy({ |
| 43 | + organizationId: "org_123", |
| 44 | + presetId: "FULL_ACCESS", |
| 45 | + }); |
| 46 | + |
| 47 | + // The critical assertion: absence must never resolve to `{ ok: true }` with |
| 48 | + // an admin scope. A plugin below the contract cannot mint any credential. |
| 49 | + expect(result.ok).toBe(false); |
| 50 | + expect(result).not.toHaveProperty("policy"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("describes a policy as having nothing extra to show", async () => { |
| 54 | + await expect( |
| 55 | + (await controller()).describeApiKeyPolicy({ presetId: "TRIGGER_ONLY", scopes: ["read:runs"] }) |
| 56 | + ).resolves.toEqual({}); |
| 57 | + }); |
| 58 | +}); |
0 commit comments