@langwatch/server 3.1.0-beta.4 → 3.1.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
- package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
- package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
- package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
- package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
- package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
- package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
- package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
- package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
- package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
- package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
- package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
- package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
- package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
- package/langwatch/ee/admin/impersonation.service.ts +170 -0
- package/langwatch/ee/admin/isAdmin.ts +9 -0
- package/langwatch/ee/admin/routes/admin.ts +531 -0
- package/langwatch/ee/admin/safeSelects.ts +64 -0
- package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
- package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
- package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
- package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
- package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
- package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
- package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
- package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
- package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
- package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
- package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
- package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
- package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
- package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
- package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
- package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
- package/langwatch/ee/billing/currencyRouter.ts +18 -0
- package/langwatch/ee/billing/errors.ts +129 -0
- package/langwatch/ee/billing/index.ts +67 -0
- package/langwatch/ee/billing/middleware.ts +17 -0
- package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
- package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
- package/langwatch/ee/billing/notifications/types.ts +9 -0
- package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
- package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
- package/langwatch/ee/billing/nurturing/types.ts +125 -0
- package/langwatch/ee/billing/planLimits.ts +190 -0
- package/langwatch/ee/billing/planProvider.ts +119 -0
- package/langwatch/ee/billing/planTypes.ts +53 -0
- package/langwatch/ee/billing/pricing.ts +57 -0
- package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
- package/langwatch/ee/billing/services/customerService.ts +90 -0
- package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
- package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
- package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
- package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
- package/langwatch/ee/billing/services/subscription.service.ts +549 -0
- package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
- package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
- package/langwatch/ee/billing/services/webhookService.ts +858 -0
- package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
- package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
- package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
- package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
- package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
- package/langwatch/ee/billing/types.ts +102 -0
- package/langwatch/ee/billing/utils/currency.ts +99 -0
- package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
- package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
- package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
- package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
- package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
- package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
- package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
- package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
- package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
- package/langwatch/ee/licensing/client.ts +29 -0
- package/langwatch/ee/licensing/constants.ts +195 -0
- package/langwatch/ee/licensing/defaults.ts +55 -0
- package/langwatch/ee/licensing/errors.ts +11 -0
- package/langwatch/ee/licensing/index.ts +52 -0
- package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
- package/langwatch/ee/licensing/licenseHandler.ts +308 -0
- package/langwatch/ee/licensing/planInfo.ts +46 -0
- package/langwatch/ee/licensing/planMapping.ts +37 -0
- package/langwatch/ee/licensing/planTemplates.ts +109 -0
- package/langwatch/ee/licensing/server.ts +43 -0
- package/langwatch/ee/licensing/signing.ts +43 -0
- package/langwatch/ee/licensing/types.ts +156 -0
- package/langwatch/ee/licensing/validation.ts +107 -0
- package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
- package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
- package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
- package/langwatch/package.json +1 -1
- package/package.json +1 -1
- package/packages/server/dist/cli.cjs +1 -1
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { generateLicenseKey } from "../licenseGenerationService";
|
|
3
|
+
import { validateLicense } from "../validation";
|
|
4
|
+
import { TEST_PRIVATE_KEY, TEST_PUBLIC_KEY } from "./fixtures/testKeys";
|
|
5
|
+
import { DEFAULT_LIMIT } from "../constants";
|
|
6
|
+
|
|
7
|
+
const baseParams = {
|
|
8
|
+
organizationName: "Acme Corp",
|
|
9
|
+
email: "buyer@acme.com",
|
|
10
|
+
planType: "GROWTH",
|
|
11
|
+
maxMembers: 5,
|
|
12
|
+
privateKey: TEST_PRIVATE_KEY,
|
|
13
|
+
now: new Date("2025-06-15T12:00:00Z"),
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("generateLicenseKey", () => {
|
|
17
|
+
describe("when generating a GROWTH license", () => {
|
|
18
|
+
it("generates a valid license key that passes round-trip validation", () => {
|
|
19
|
+
const { licenseKey } = generateLicenseKey(baseParams);
|
|
20
|
+
const result = validateLicense(licenseKey, TEST_PUBLIC_KEY);
|
|
21
|
+
|
|
22
|
+
expect(result.valid).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("sets maxMembers from the provided seat count", () => {
|
|
26
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
27
|
+
|
|
28
|
+
expect(licenseData.plan.maxMembers).toBe(5);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("sets all other limits to unlimited (DEFAULT_LIMIT)", () => {
|
|
32
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
33
|
+
const { plan } = licenseData;
|
|
34
|
+
|
|
35
|
+
expect(plan.maxMembersLite).toBe(DEFAULT_LIMIT);
|
|
36
|
+
expect(plan.maxTeams).toBe(DEFAULT_LIMIT);
|
|
37
|
+
expect(plan.maxProjects).toBe(DEFAULT_LIMIT);
|
|
38
|
+
expect(plan.maxMessagesPerMonth).toBe(DEFAULT_LIMIT);
|
|
39
|
+
expect(plan.maxWorkflows).toBe(DEFAULT_LIMIT);
|
|
40
|
+
expect(plan.maxPrompts).toBe(DEFAULT_LIMIT);
|
|
41
|
+
expect(plan.maxEvaluators).toBe(DEFAULT_LIMIT);
|
|
42
|
+
expect(plan.maxScenarios).toBe(DEFAULT_LIMIT);
|
|
43
|
+
expect(plan.maxAgents).toBe(DEFAULT_LIMIT);
|
|
44
|
+
expect(plan.maxExperiments).toBe(DEFAULT_LIMIT);
|
|
45
|
+
expect(plan.maxOnlineEvaluations).toBe(DEFAULT_LIMIT);
|
|
46
|
+
expect(plan.maxDatasets).toBe(DEFAULT_LIMIT);
|
|
47
|
+
expect(plan.maxDashboards).toBe(DEFAULT_LIMIT);
|
|
48
|
+
expect(plan.maxCustomGraphs).toBe(DEFAULT_LIMIT);
|
|
49
|
+
expect(plan.maxAutomations).toBe(DEFAULT_LIMIT);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("sets plan type to GROWTH and name to Growth", () => {
|
|
53
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
54
|
+
|
|
55
|
+
expect(licenseData.plan.type).toBe("GROWTH");
|
|
56
|
+
expect(licenseData.plan.name).toBe("Growth");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("sets canPublish to true and usageUnit to events", () => {
|
|
60
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
61
|
+
|
|
62
|
+
expect(licenseData.plan.canPublish).toBe(true);
|
|
63
|
+
expect(licenseData.plan.usageUnit).toBe("events");
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("when generating a PRO license", () => {
|
|
68
|
+
it("generates a valid license with PRO template limits", () => {
|
|
69
|
+
const { licenseKey, licenseData } = generateLicenseKey({
|
|
70
|
+
...baseParams,
|
|
71
|
+
planType: "PRO",
|
|
72
|
+
maxMembers: 10,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const result = validateLicense(licenseKey, TEST_PUBLIC_KEY);
|
|
76
|
+
expect(result.valid).toBe(true);
|
|
77
|
+
expect(licenseData.plan.type).toBe("PRO");
|
|
78
|
+
expect(licenseData.plan.maxMembers).toBe(10);
|
|
79
|
+
expect(licenseData.plan.maxProjects).toBe(20);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("when generating an ENTERPRISE license", () => {
|
|
84
|
+
it("generates a valid license with ENTERPRISE template limits", () => {
|
|
85
|
+
const { licenseKey, licenseData } = generateLicenseKey({
|
|
86
|
+
...baseParams,
|
|
87
|
+
planType: "ENTERPRISE",
|
|
88
|
+
maxMembers: 50,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const result = validateLicense(licenseKey, TEST_PUBLIC_KEY);
|
|
92
|
+
expect(result.valid).toBe(true);
|
|
93
|
+
expect(licenseData.plan.type).toBe("ENTERPRISE");
|
|
94
|
+
expect(licenseData.plan.maxMembers).toBe(50);
|
|
95
|
+
expect(licenseData.plan.maxProjects).toBe(500);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("when expiration is calculated", () => {
|
|
100
|
+
it("expires exactly 1 year from the generation date", () => {
|
|
101
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
102
|
+
|
|
103
|
+
expect(licenseData.expiresAt).toBe("2026-06-15T12:00:00.000Z");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("handles leap year boundary correctly", () => {
|
|
107
|
+
const { licenseData } = generateLicenseKey({
|
|
108
|
+
...baseParams,
|
|
109
|
+
now: new Date("2024-02-29T12:00:00Z"),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Feb 29 2024 + 1 year → Mar 1 2025 (no Feb 29 in 2025)
|
|
113
|
+
expect(licenseData.expiresAt).toBe("2025-03-01T12:00:00.000Z");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("when organization name is empty", () => {
|
|
118
|
+
it("falls back to email as organization name", () => {
|
|
119
|
+
const { licenseData } = generateLicenseKey({
|
|
120
|
+
...baseParams,
|
|
121
|
+
organizationName: "",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
expect(licenseData.organizationName).toBe("buyer@acme.com");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("falls back to email when name is whitespace-only", () => {
|
|
128
|
+
const { licenseData } = generateLicenseKey({
|
|
129
|
+
...baseParams,
|
|
130
|
+
organizationName: " ",
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(licenseData.organizationName).toBe("buyer@acme.com");
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe("when maxMembers is zero or negative", () => {
|
|
138
|
+
it("defaults to 1 seat when maxMembers is 0", () => {
|
|
139
|
+
const { licenseData } = generateLicenseKey({
|
|
140
|
+
...baseParams,
|
|
141
|
+
maxMembers: 0,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
expect(licenseData.plan.maxMembers).toBe(1);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("defaults to 1 seat when maxMembers is negative", () => {
|
|
148
|
+
const { licenseData } = generateLicenseKey({
|
|
149
|
+
...baseParams,
|
|
150
|
+
maxMembers: -3,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(licenseData.plan.maxMembers).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("when plan type is unknown", () => {
|
|
158
|
+
it("throws an error for unknown plan types", () => {
|
|
159
|
+
expect(() =>
|
|
160
|
+
generateLicenseKey({
|
|
161
|
+
...baseParams,
|
|
162
|
+
planType: "CUSTOM",
|
|
163
|
+
})
|
|
164
|
+
).toThrow("Unknown plan type: CUSTOM");
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("when license metadata is set", () => {
|
|
169
|
+
it("sets version to 1", () => {
|
|
170
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
171
|
+
|
|
172
|
+
expect(licenseData.version).toBe(1);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("sets issuedAt to the provided now date", () => {
|
|
176
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
177
|
+
|
|
178
|
+
expect(licenseData.issuedAt).toBe("2025-06-15T12:00:00.000Z");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("generates a unique licenseId with lic- prefix", () => {
|
|
182
|
+
const { licenseData: first } = generateLicenseKey(baseParams);
|
|
183
|
+
const { licenseData: second } = generateLicenseKey(baseParams);
|
|
184
|
+
|
|
185
|
+
expect(first.licenseId).toMatch(/^lic-/);
|
|
186
|
+
expect(second.licenseId).toMatch(/^lic-/);
|
|
187
|
+
expect(first.licenseId).not.toBe(second.licenseId);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("stores email and organization name in license data", () => {
|
|
191
|
+
const { licenseData } = generateLicenseKey(baseParams);
|
|
192
|
+
|
|
193
|
+
expect(licenseData.email).toBe("buyer@acme.com");
|
|
194
|
+
expect(licenseData.organizationName).toBe("Acme Corp");
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe("when validating round-trip integrity", () => {
|
|
199
|
+
it("produces a license that can be parsed and verified", () => {
|
|
200
|
+
const { licenseKey, licenseData } = generateLicenseKey(baseParams);
|
|
201
|
+
const result = validateLicense(licenseKey, TEST_PUBLIC_KEY);
|
|
202
|
+
|
|
203
|
+
expect(result.valid).toBe(true);
|
|
204
|
+
if (result.valid) {
|
|
205
|
+
expect(result.licenseData.licenseId).toBe(licenseData.licenseId);
|
|
206
|
+
expect(result.licenseData.email).toBe("buyer@acme.com");
|
|
207
|
+
expect(result.licenseData.organizationName).toBe("Acme Corp");
|
|
208
|
+
expect(result.licenseData.plan.type).toBe("GROWTH");
|
|
209
|
+
expect(result.licenseData.plan.maxMembers).toBe(5);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("fails validation with a different public key", () => {
|
|
214
|
+
const { licenseKey } = generateLicenseKey(baseParams);
|
|
215
|
+
const result = validateLicense(
|
|
216
|
+
licenseKey,
|
|
217
|
+
// Use a dummy key that doesn't match
|
|
218
|
+
`-----BEGIN PUBLIC KEY-----
|
|
219
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq4utbj0BDQlwUcQ2gNar
|
|
220
|
+
8vT0JYj24i6xoIGLBmwkY/D9vxU4FbFdiLPddiv+Mv5KAPVvNXbLy8zkbsK74BT7
|
|
221
|
+
ye7Od2bJILMiZGMRKj7t1lx3ClthIZKUWjGMiWY0FyOHon+vrz81QireVd1QQuYh
|
|
222
|
+
zLk5oLpttCXLIUqatQ+w6M9oHz8Ru+qy6thFEe29lqGCczRmpCtXmGr5R22UVUp7
|
|
223
|
+
OLqhJ/73aa+nso54jUvMTUYt8k0kbOhvSY9EhwrsvCxeJcNCl3vYf4Cphpqf9OF0
|
|
224
|
+
sUIBcjrzUh14Z/RxKyun5Ld12xGVuhSzVf0xnWar338N9WKgaFOW+zgRchBGdXFD
|
|
225
|
+
dQIDAQAB
|
|
226
|
+
-----END PUBLIC KEY-----`,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
expect(result.valid).toBe(false);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import {
|
|
2
|
+
beforeAll,
|
|
3
|
+
afterAll,
|
|
4
|
+
afterEach,
|
|
5
|
+
describe,
|
|
6
|
+
expect,
|
|
7
|
+
it,
|
|
8
|
+
} from "vitest";
|
|
9
|
+
import { prisma } from "../../../src/server/db";
|
|
10
|
+
import { LicenseHandler, type ITraceUsageService } from "../licenseHandler";
|
|
11
|
+
import { OrganizationNotFoundError } from "../errors";
|
|
12
|
+
import {
|
|
13
|
+
BASE_LICENSE,
|
|
14
|
+
ENTERPRISE_LICENSE,
|
|
15
|
+
VALID_LICENSE_KEY,
|
|
16
|
+
EXPIRED_LICENSE_KEY,
|
|
17
|
+
TAMPERED_LICENSE_KEY,
|
|
18
|
+
ENTERPRISE_LICENSE_KEY,
|
|
19
|
+
GARBAGE_DATA,
|
|
20
|
+
} from "./fixtures/testLicenses";
|
|
21
|
+
import { TEST_PUBLIC_KEY } from "./fixtures/testKeys";
|
|
22
|
+
import { FREE_PLAN } from "../constants";
|
|
23
|
+
import { LicenseEnforcementRepository } from "../../../src/server/license-enforcement/license-enforcement.repository";
|
|
24
|
+
|
|
25
|
+
// Mock TraceUsageService for testing - returns 0 for all counts
|
|
26
|
+
const mockTraceUsageService: ITraceUsageService = {
|
|
27
|
+
getCurrentMonthCount: async () => 0,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe("LicenseHandler Integration", () => {
|
|
31
|
+
let organizationId: string;
|
|
32
|
+
let handler: LicenseHandler;
|
|
33
|
+
|
|
34
|
+
beforeAll(async () => {
|
|
35
|
+
// Create handler with test public key, repository, and trace service
|
|
36
|
+
handler = new LicenseHandler({
|
|
37
|
+
prisma,
|
|
38
|
+
publicKey: TEST_PUBLIC_KEY,
|
|
39
|
+
repository: new LicenseEnforcementRepository(prisma),
|
|
40
|
+
traceUsageService: mockTraceUsageService,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Create test organization
|
|
44
|
+
const organization = await prisma.organization.upsert({
|
|
45
|
+
where: { slug: "license-handler-test-org" },
|
|
46
|
+
update: {
|
|
47
|
+
license: null,
|
|
48
|
+
licenseExpiresAt: null,
|
|
49
|
+
licenseLastValidatedAt: null,
|
|
50
|
+
},
|
|
51
|
+
create: {
|
|
52
|
+
name: "License Handler Test Org",
|
|
53
|
+
slug: "license-handler-test-org",
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
organizationId = organization.id;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterAll(async () => {
|
|
60
|
+
// Cleanup - delete in correct order for foreign key constraints
|
|
61
|
+
const org = await prisma.organization.findUnique({
|
|
62
|
+
where: { slug: "license-handler-test-org" },
|
|
63
|
+
});
|
|
64
|
+
if (org) {
|
|
65
|
+
await prisma.organizationUser.deleteMany({
|
|
66
|
+
where: { organizationId: org.id },
|
|
67
|
+
});
|
|
68
|
+
await prisma.organization.delete({
|
|
69
|
+
where: { id: org.id },
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
afterEach(async () => {
|
|
75
|
+
// Reset license after each test
|
|
76
|
+
await prisma.organization.update({
|
|
77
|
+
where: { id: organizationId },
|
|
78
|
+
data: {
|
|
79
|
+
license: null,
|
|
80
|
+
licenseExpiresAt: null,
|
|
81
|
+
licenseLastValidatedAt: null,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// getLicenseStatus Tests
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
|
|
90
|
+
describe("getLicenseStatus", () => {
|
|
91
|
+
it("returns hasLicense=false when org has no license", async () => {
|
|
92
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
93
|
+
|
|
94
|
+
expect(status.hasLicense).toBe(false);
|
|
95
|
+
expect(status.valid).toBe(false);
|
|
96
|
+
expect("plan" in status).toBe(false);
|
|
97
|
+
expect("expiresAt" in status).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("returns valid=true with metadata for valid license", async () => {
|
|
101
|
+
await prisma.organization.update({
|
|
102
|
+
where: { id: organizationId },
|
|
103
|
+
data: { license: VALID_LICENSE_KEY },
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
107
|
+
|
|
108
|
+
expect(status.hasLicense).toBe(true);
|
|
109
|
+
expect(status.valid).toBe(true);
|
|
110
|
+
if (!status.hasLicense || !("plan" in status)) {
|
|
111
|
+
throw new Error("Expected license with plan metadata");
|
|
112
|
+
}
|
|
113
|
+
expect(status.plan).toBe(BASE_LICENSE.plan.type);
|
|
114
|
+
expect(status.planName).toBe(BASE_LICENSE.plan.name);
|
|
115
|
+
expect(status.organizationName).toBe(BASE_LICENSE.organizationName);
|
|
116
|
+
expect(status.maxMembers).toBe(BASE_LICENSE.plan.maxMembers);
|
|
117
|
+
expect(status.expiresAt).toBeDefined();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("returns valid=false with metadata for expired license", async () => {
|
|
121
|
+
await prisma.organization.update({
|
|
122
|
+
where: { id: organizationId },
|
|
123
|
+
data: { license: EXPIRED_LICENSE_KEY },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
127
|
+
|
|
128
|
+
expect(status.hasLicense).toBe(true);
|
|
129
|
+
expect(status.valid).toBe(false);
|
|
130
|
+
// Metadata is still returned for UI display (e.g., "license expired" message)
|
|
131
|
+
if (!status.hasLicense || !("plan" in status)) {
|
|
132
|
+
throw new Error("Expected license with plan metadata");
|
|
133
|
+
}
|
|
134
|
+
expect(status.plan).toBe(BASE_LICENSE.plan.type);
|
|
135
|
+
expect(status.planName).toBe(BASE_LICENSE.plan.name);
|
|
136
|
+
expect(status.organizationName).toBe(BASE_LICENSE.organizationName);
|
|
137
|
+
expect(status.expiresAt).toBeDefined();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("returns valid=false for tampered license", async () => {
|
|
141
|
+
await prisma.organization.update({
|
|
142
|
+
where: { id: organizationId },
|
|
143
|
+
data: { license: TAMPERED_LICENSE_KEY },
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
147
|
+
|
|
148
|
+
expect(status.hasLicense).toBe(true);
|
|
149
|
+
expect(status.valid).toBe(false);
|
|
150
|
+
// Metadata may still be present from parsed data (tampered has "Hacker Corp")
|
|
151
|
+
if (!status.hasLicense || !("organizationName" in status)) {
|
|
152
|
+
throw new Error("Expected license with organizationName metadata");
|
|
153
|
+
}
|
|
154
|
+
expect(status.organizationName).toBe("Hacker Corp");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("returns valid=false for malformed license string", async () => {
|
|
158
|
+
await prisma.organization.update({
|
|
159
|
+
where: { id: organizationId },
|
|
160
|
+
data: { license: "not-a-valid-license-string" },
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
164
|
+
|
|
165
|
+
expect(status.hasLicense).toBe(true);
|
|
166
|
+
expect(status.valid).toBe(false);
|
|
167
|
+
// No metadata when format is completely invalid
|
|
168
|
+
expect("plan" in status).toBe(false);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("includes current member count in status", async () => {
|
|
172
|
+
// Add a member to the organization
|
|
173
|
+
const user = await prisma.user.create({
|
|
174
|
+
data: {
|
|
175
|
+
email: `test-member-${Date.now()}@example.com`,
|
|
176
|
+
name: "Test Member",
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
await prisma.organizationUser.create({
|
|
181
|
+
data: {
|
|
182
|
+
organizationId,
|
|
183
|
+
userId: user.id,
|
|
184
|
+
role: "MEMBER",
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
await prisma.organization.update({
|
|
189
|
+
where: { id: organizationId },
|
|
190
|
+
data: { license: VALID_LICENSE_KEY },
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const status = await handler.getLicenseStatus(organizationId);
|
|
194
|
+
|
|
195
|
+
if (!status.hasLicense || !("currentMembers" in status)) {
|
|
196
|
+
throw new Error("Expected license with member count metadata");
|
|
197
|
+
}
|
|
198
|
+
expect(status.currentMembers).toBeGreaterThanOrEqual(1);
|
|
199
|
+
expect(status.maxMembers).toBe(BASE_LICENSE.plan.maxMembers);
|
|
200
|
+
|
|
201
|
+
// Cleanup member
|
|
202
|
+
await prisma.organizationUser.deleteMany({
|
|
203
|
+
where: { organizationId, userId: user.id },
|
|
204
|
+
});
|
|
205
|
+
await prisma.user.delete({ where: { id: user.id } });
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// ==========================================================================
|
|
210
|
+
// validateAndStoreLicense Tests
|
|
211
|
+
// ==========================================================================
|
|
212
|
+
|
|
213
|
+
describe("validateAndStoreLicense", () => {
|
|
214
|
+
it("stores valid license and returns success with planInfo", async () => {
|
|
215
|
+
const result = await handler.validateAndStoreLicense(
|
|
216
|
+
organizationId,
|
|
217
|
+
ENTERPRISE_LICENSE_KEY
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
expect(result.success).toBe(true);
|
|
221
|
+
if (result.success) {
|
|
222
|
+
expect(result.planInfo.type).toBe(ENTERPRISE_LICENSE.plan.type);
|
|
223
|
+
expect(result.planInfo.maxMembers).toBe(
|
|
224
|
+
ENTERPRISE_LICENSE.plan.maxMembers
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Verify stored in database
|
|
229
|
+
const org = await prisma.organization.findUnique({
|
|
230
|
+
where: { id: organizationId },
|
|
231
|
+
select: {
|
|
232
|
+
license: true,
|
|
233
|
+
licenseExpiresAt: true,
|
|
234
|
+
licenseLastValidatedAt: true,
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
expect(org?.license).toBe(ENTERPRISE_LICENSE_KEY);
|
|
239
|
+
expect(org?.licenseExpiresAt).not.toBeNull();
|
|
240
|
+
expect(org?.licenseLastValidatedAt).not.toBeNull();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("returns error for invalid license format", async () => {
|
|
244
|
+
const result = await handler.validateAndStoreLicense(
|
|
245
|
+
organizationId,
|
|
246
|
+
GARBAGE_DATA
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
expect(result.success).toBe(false);
|
|
250
|
+
if (!result.success) {
|
|
251
|
+
expect(result.error).toBe("Invalid license format");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Verify not stored
|
|
255
|
+
const org = await prisma.organization.findUnique({
|
|
256
|
+
where: { id: organizationId },
|
|
257
|
+
select: { license: true },
|
|
258
|
+
});
|
|
259
|
+
expect(org?.license).toBeNull();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("returns error for invalid signature", async () => {
|
|
263
|
+
const result = await handler.validateAndStoreLicense(
|
|
264
|
+
organizationId,
|
|
265
|
+
TAMPERED_LICENSE_KEY
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
expect(result.success).toBe(false);
|
|
269
|
+
if (!result.success) {
|
|
270
|
+
expect(result.error).toBe("Invalid signature");
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("returns error for expired license", async () => {
|
|
275
|
+
const result = await handler.validateAndStoreLicense(
|
|
276
|
+
organizationId,
|
|
277
|
+
EXPIRED_LICENSE_KEY
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
expect(result.success).toBe(false);
|
|
281
|
+
if (!result.success) {
|
|
282
|
+
expect(result.error).toBe("License expired");
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("throws OrganizationNotFoundError for non-existent org", async () => {
|
|
287
|
+
await expect(
|
|
288
|
+
handler.validateAndStoreLicense("non-existent-org-id", VALID_LICENSE_KEY)
|
|
289
|
+
).rejects.toThrow(OrganizationNotFoundError);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("updates existing license when storing new one", async () => {
|
|
293
|
+
// First store PRO license
|
|
294
|
+
await handler.validateAndStoreLicense(organizationId, VALID_LICENSE_KEY);
|
|
295
|
+
|
|
296
|
+
// Then store ENTERPRISE license
|
|
297
|
+
const result = await handler.validateAndStoreLicense(
|
|
298
|
+
organizationId,
|
|
299
|
+
ENTERPRISE_LICENSE_KEY
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
expect(result.success).toBe(true);
|
|
303
|
+
if (result.success) {
|
|
304
|
+
expect(result.planInfo.type).toBe(ENTERPRISE_LICENSE.plan.type);
|
|
305
|
+
expect(result.planInfo.maxMembers).toBe(
|
|
306
|
+
ENTERPRISE_LICENSE.plan.maxMembers
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Verify updated in database
|
|
311
|
+
const org = await prisma.organization.findUnique({
|
|
312
|
+
where: { id: organizationId },
|
|
313
|
+
select: { license: true },
|
|
314
|
+
});
|
|
315
|
+
expect(org?.license).toBe(ENTERPRISE_LICENSE_KEY);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// ==========================================================================
|
|
320
|
+
// removeLicense Tests
|
|
321
|
+
// ==========================================================================
|
|
322
|
+
|
|
323
|
+
describe("removeLicense", () => {
|
|
324
|
+
it("removes existing license and returns removed=true", async () => {
|
|
325
|
+
// First store a license
|
|
326
|
+
await prisma.organization.update({
|
|
327
|
+
where: { id: organizationId },
|
|
328
|
+
data: {
|
|
329
|
+
license: VALID_LICENSE_KEY,
|
|
330
|
+
licenseExpiresAt: new Date(),
|
|
331
|
+
licenseLastValidatedAt: new Date(),
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const result = await handler.removeLicense(organizationId);
|
|
336
|
+
|
|
337
|
+
expect(result.removed).toBe(true);
|
|
338
|
+
|
|
339
|
+
// Verify cleared in database
|
|
340
|
+
const org = await prisma.organization.findUnique({
|
|
341
|
+
where: { id: organizationId },
|
|
342
|
+
select: {
|
|
343
|
+
license: true,
|
|
344
|
+
licenseExpiresAt: true,
|
|
345
|
+
licenseLastValidatedAt: true,
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
expect(org?.license).toBeNull();
|
|
350
|
+
expect(org?.licenseExpiresAt).toBeNull();
|
|
351
|
+
expect(org?.licenseLastValidatedAt).toBeNull();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("is idempotent - removing when no license exists returns removed=true", async () => {
|
|
355
|
+
// Ensure no license
|
|
356
|
+
await prisma.organization.update({
|
|
357
|
+
where: { id: organizationId },
|
|
358
|
+
data: { license: null },
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const result = await handler.removeLicense(organizationId);
|
|
362
|
+
|
|
363
|
+
expect(result.removed).toBe(true);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("throws OrganizationNotFoundError for non-existent org", async () => {
|
|
367
|
+
await expect(
|
|
368
|
+
handler.removeLicense("non-existent-org-id")
|
|
369
|
+
).rejects.toThrow(OrganizationNotFoundError);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// ==========================================================================
|
|
374
|
+
// getActivePlan Tests (ensuring no regression)
|
|
375
|
+
// ==========================================================================
|
|
376
|
+
|
|
377
|
+
describe("getActivePlan", () => {
|
|
378
|
+
it("returns FREE_PLAN when no license exists", async () => {
|
|
379
|
+
const plan = await handler.getActivePlan(organizationId);
|
|
380
|
+
|
|
381
|
+
expect(plan.type).toBe(FREE_PLAN.type);
|
|
382
|
+
expect(plan.maxMembers).toBe(FREE_PLAN.maxMembers);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("returns license plan when valid license exists", async () => {
|
|
386
|
+
await prisma.organization.update({
|
|
387
|
+
where: { id: organizationId },
|
|
388
|
+
data: { license: ENTERPRISE_LICENSE_KEY },
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
const plan = await handler.getActivePlan(organizationId);
|
|
392
|
+
|
|
393
|
+
expect(plan.type).toBe(ENTERPRISE_LICENSE.plan.type);
|
|
394
|
+
expect(plan.maxMembers).toBe(ENTERPRISE_LICENSE.plan.maxMembers);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("returns FREE_PLAN when license is expired", async () => {
|
|
398
|
+
await prisma.organization.update({
|
|
399
|
+
where: { id: organizationId },
|
|
400
|
+
data: { license: EXPIRED_LICENSE_KEY },
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
const plan = await handler.getActivePlan(organizationId);
|
|
404
|
+
|
|
405
|
+
expect(plan.type).toBe(FREE_PLAN.type);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("returns FREE_PLAN when license is tampered", async () => {
|
|
409
|
+
await prisma.organization.update({
|
|
410
|
+
where: { id: organizationId },
|
|
411
|
+
data: { license: TAMPERED_LICENSE_KEY },
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const plan = await handler.getActivePlan(organizationId);
|
|
415
|
+
|
|
416
|
+
expect(plan.type).toBe(FREE_PLAN.type);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
});
|