Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

test(e2e): fake the license per org instead of per instance

Calvin56cfe13d3749
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 4a84892a40..c1a5bc9182 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -54,9 +54,10 @@ E2E tests live in `e2e-test/routes/`. The custom Vitest environment (`e2e-test/v
`isTestMode` branches. Replace the module instead, from `test.alias` in
`vitest.e2e.config.mts`, with a double under `e2e-test/fakes/`. Production code stays unaware
a fake exists. `e2e-test/fakes/aws-parameter-store-sync-fns.ts` and its connection counterpart
-are the worked examples. `e2e-test/fakes/license-fns.ts` does the same for the plan: a spec calls
-`fakeLicense.setFeatures({ ... })` to turn plan features on over HTTP, and
-`e2e-test/setup/reset-fakes.ts` restores the unlicensed defaults at the start of every file.
+are the worked examples. `e2e-test/fakes/license-service.ts` does the same for the plan: a spec
+calls `fakeLicense.setFeatures(orgId, { ... })` to turn plan features on over HTTP for an org it
+created (the seeded org is refused, since every spec shares it), and
+`e2e-test/setup/reset-fakes.ts` clears the overrides at the start of every file.
Four things decide whether this works:
diff --git a/backend/e2e-test/fakes/license-fns.ts b/backend/e2e-test/fakes/license-fns.ts
deleted file mode 100644
index 3c411ce65b..0000000000
--- a/backend/e2e-test/fakes/license-fns.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import { TFeatureSet } from "@app/ee/services/license/license-types";
-
-import { getDefaultOnPremFeatures as getMockDefaults } from "../../src/ee/services/license/__mocks__/license-fns";
-import type * as RealLicenseFns from "../../src/ee/services/license/license-fns";
-
-// The e2e instance runs on-prem with no license key, so getPlan answers with the feature set the
-// license service took from getDefaultOnPremFeatures when it booted, by reference. Handing out one
-// shared object means a spec can change the plan the running server sees without a restart.
-// Wired up by test.alias in vitest.e2e.config.mts. Nothing under src/ references this file.
-//
-// The relative path to the unit-test mock is deliberate: the alias keys on the specifier
-// "./license-fns", so this path is not itself aliased and does not resolve back to this file.
-export { getLicenseKeyConfig } from "../../src/ee/services/license/__mocks__/license-fns";
-
-// Shared through globalThis for the same reason as the AWS fakes: the alias makes this module
-// reachable by more than one specifier, so it can be instantiated twice, and a spec would then
-// configure a copy the server never reads.
-const globalScope = globalThis as typeof globalThis & {
- infisicalFakeLicenseFeatures?: TFeatureSet;
-};
-
-globalScope.infisicalFakeLicenseFeatures ??= getMockDefaults() as TFeatureSet;
-
-const features = globalScope.infisicalFakeLicenseFeatures;
-
-export const getDefaultOnPremFeatures = (): TFeatureSet => features;
-
-// Nothing forces a module replaced by an alias to match the module it replaces, so a change to the
-// real feature set would otherwise leave this fake quietly wrong. This assignment fails
-// type-checking instead.
-export const assertFakeMatchesRealLicenseFns: Pick<typeof RealLicenseFns, "getDefaultOnPremFeatures"> = {
- getDefaultOnPremFeatures
-};
-
-// TFeatureSet types several flags as the literal `false` (the unlicensed default), so a plain
-// Partial<TFeatureSet> would refuse `true`. Widen those literals to boolean.
-type TFeatureOverrides = {
- [K in keyof TFeatureSet]?: TFeatureSet[K] extends boolean ? boolean : TFeatureSet[K];
-};
-
-export const fakeLicense = {
- // Mutates in place rather than replacing the object, since the license service holds a reference
- // to it from boot.
- reset: () => {
- Object.keys(features).forEach((key) => delete (features as Record<string, unknown>)[key]);
- Object.assign(features, getMockDefaults());
- },
-
- // Turns plan features on (or off) for the whole instance until the next reset.
- setFeatures: (overrides: TFeatureOverrides) => {
- Object.assign(features, overrides);
- }
-};
diff --git a/backend/e2e-test/fakes/license-service.ts b/backend/e2e-test/fakes/license-service.ts
new file mode 100644
index 0000000000..1b8a93ccf1
--- /dev/null
+++ b/backend/e2e-test/fakes/license-service.ts
@@ -0,0 +1,66 @@
+import { seedData1 } from "@app/db/seed-data";
+import { TFeatureSet } from "@app/ee/services/license/license-types";
+
+import * as RealLicenseService from "../../src/ee/services/license/license-service";
+
+// Plans belong to an organization, so the fake keys them by org: a spec turns features on for an
+// org it created, and no other org's plan moves. Specs stay isolated whatever order they run in.
+// Wired up by test.alias in vitest.e2e.config.mts. Nothing under src/ references this file.
+//
+// The e2e server runs on-prem, where the real getPlan ignores the org and answers with one
+// instance-wide plan, so the override sits here rather than in license-fns. Only getPlan is
+// replaced; the rest of the service, including the instance-wide onPremFeatures, stays real.
+//
+// The relative path to the real module is deliberate: the alias keys on the specifier
+// "@app/ee/services/license/license-service", so this path is not itself aliased and does not
+// resolve back to this file.
+export type { TLicenseServiceFactory } from "../../src/ee/services/license/license-service";
+
+// TFeatureSet types several flags as the literal `false` (the unlicensed default), so a plain
+// Partial<TFeatureSet> would refuse `true`. Widen those literals to boolean.
+type TFeatureOverrides = {
+ [K in keyof TFeatureSet]?: TFeatureSet[K] extends boolean ? boolean : TFeatureSet[K];
+};
+
+// Shared through globalThis for the same reason as the AWS fakes: the alias makes this module
+// reachable by more than one specifier, so it can be instantiated twice, and a spec would then
+// configure a copy the server never reads.
+const globalScope = globalThis as typeof globalThis & {
+ infisicalFakeLicenseOverrides?: Map<string, TFeatureOverrides>;
+};
+
+globalScope.infisicalFakeLicenseOverrides ??= new Map();
+
+const overridesByOrg = globalScope.infisicalFakeLicenseOverrides;
+
+export const licenseServiceFactory: typeof RealLicenseService.licenseServiceFactory = (deps) => {
+ const service = RealLicenseService.licenseServiceFactory(deps);
+ const realGetPlan = service.getPlan;
+
+ // Assigned onto the real service rather than spread into a copy, because the service exposes
+ // onPremFeatures as a getter and a spread would freeze it at its boot value.
+ service.getPlan = async (orgId, projectId) => {
+ const plan = await realGetPlan(orgId, projectId);
+ const overrides = overridesByOrg.get(orgId);
+ return overrides ? ({ ...plan, ...overrides } as TFeatureSet) : plan;
+ };
+
+ return service;
+};
+
+export const fakeLicense = {
+ reset: () => {
+ overridesByOrg.clear();
+ },
+
+ // Turns plan features on (or off) for one org until the next reset. The seeded org is shared by
+ // every spec that logs in as the seeded user, so a spec must create its own org to change a plan.
+ setFeatures: (orgId: string, overrides: TFeatureOverrides) => {
+ if (orgId === seedData1.organization.id) {
+ throw new Error(
+ "fakeLicense.setFeatures: the seeded org is shared across specs. Create an org for this spec (eg createIsolatedOrgAndProject) and change its plan instead."
+ );
+ }
+ overridesByOrg.set(orgId, { ...overridesByOrg.get(orgId), ...overrides });
+ }
+};
diff --git a/backend/e2e-test/routes/v3/secrets-management.spec.ts b/backend/e2e-test/routes/v3/secrets-management.spec.ts
index b63b9f5fe5..3a54db18ae 100644
--- a/backend/e2e-test/routes/v3/secrets-management.spec.ts
+++ b/backend/e2e-test/routes/v3/secrets-management.spec.ts
@@ -1,4 +1,4 @@
-import { fakeLicense } from "e2e-test/fakes/license-fns";
+import { fakeLicense } from "e2e-test/fakes/license-service";
import { createFakeWebhookServer, TFakeWebhookServer } from "e2e-test/fakes/webhook-destination";
import { createIsolatedOrgAndProject, createProject } from "e2e-test/testUtils/fixtures";
import { createFolder } from "e2e-test/testUtils/folders";
@@ -100,11 +100,7 @@ describe("Secrets management", () => {
describe("Finding a secret by its value", () => {
// Value search sits behind the insights plan, and the e2e instance runs unlicensed.
beforeAll(() => {
- fakeLicense.setFeatures({ secretAccessInsights: true });
- });
-
- afterAll(() => {
- fakeLicense.reset();
+ fakeLicense.setFeatures(orgId, { secretAccessInsights: true });
});
test("a rotated value stops being found and the new one starts", async () => {
@@ -210,6 +206,7 @@ describe("Secrets management", () => {
// The org digest is keyed by the org data key, so one org's values must never match another's.
test("a value held in another organization is not found", async () => {
const other = await createIsolatedOrgAndProject("secrets-management-e2e-other");
+ fakeLicense.setFeatures(other.orgId, { secretAccessInsights: true });
try {
const value = "value-in-a-different-org";
await createSecretV2({
diff --git a/backend/e2e-test/setup/reset-fakes.ts b/backend/e2e-test/setup/reset-fakes.ts
index e46c3abb20..305c5f26fc 100644
--- a/backend/e2e-test/setup/reset-fakes.ts
+++ b/backend/e2e-test/setup/reset-fakes.ts
@@ -1,7 +1,7 @@
import { fakeAwsConnection } from "e2e-test/fakes/aws-connection-fns";
import { fakeIamUserSecret } from "e2e-test/fakes/aws-iam-user-secret-rotation-fns";
import { fakeParameterStore } from "e2e-test/fakes/aws-parameter-store-sync-fns";
-import { fakeLicense } from "e2e-test/fakes/license-fns";
+import { fakeLicense } from "e2e-test/fakes/license-service";
import { fakeS3Bucket } from "e2e-test/fakes/s3";
// The fakes are module singletons shared with the server booted in this same process, and the
diff --git a/backend/vitest.e2e.config.mts b/backend/vitest.e2e.config.mts
index 616137d226..8020922ddb 100644
--- a/backend/vitest.e2e.config.mts
+++ b/backend/vitest.e2e.config.mts
@@ -52,8 +52,9 @@ export default defineConfig({
// AWS Parameter Store, the AWS IAM user secret rotation provider and the AWS app connection
// are replaced by fakes for the whole e2e run, so the secret sync and secret rotation specs
// can assert what Infisical hands a destination or a rotation provider without reaching AWS.
- // Nothing under src/ knows the fakes exist (see e2e-test/fakes/). The license functions are
- // replaced the same way, so a spec can turn plan features on with fakeLicense.setFeatures.
+ // Nothing under src/ knows the fakes exist (see e2e-test/fakes/). The license service is
+ // replaced the same way, so a spec can turn plan features on for its own org with
+ // fakeLicense.setFeatures.
//
// Entries match the *import specifier*, which is what keeps this surgical rather than
// sweeping: "./aws-parameter-store-sync-fns" is imported only by its own barrel,
@@ -68,7 +69,11 @@ export default defineConfig({
alias: [
{
find: "./license-fns",
- replacement: path.resolve(__dirname, "./e2e-test/fakes/license-fns")
+ replacement: path.resolve(__dirname, "./src/ee/services/license/__mocks__/license-fns")
+ },
+ {
+ find: /^@app\/ee\/services\/license\/license-service$/,
+ replacement: path.resolve(__dirname, "./e2e-test/fakes/license-service")
},
{
find: "./aws-parameter-store-sync-fns",