@donotdev/functions 0.0.20 → 0.1.1
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/LICENSE.md +3 -3
- package/guidelines/FUNCTIONS.md +9 -0
- package/guidelines/VERCEL.md +260 -0
- package/lib/firebase/auth/getCustomClaims.d.ts +2 -2
- package/lib/firebase/auth/getUserAuthStatus.d.ts +2 -2
- package/lib/firebase/auth/index.d.ts +1 -1
- package/lib/firebase/auth/removeCustomClaims.d.ts +2 -2
- package/lib/firebase/auth/setCustomClaims.d.ts +2 -2
- package/lib/firebase/baseFunction.d.ts +1 -1
- package/lib/firebase/billing/cancelSubscription.d.ts +1 -1
- package/lib/firebase/billing/changePlan.d.ts +1 -1
- package/lib/firebase/billing/createCheckoutSession.d.ts +10 -13
- package/lib/firebase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/firebase/billing/index.d.ts +1 -1
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +2 -2
- package/lib/firebase/config/constants.d.ts +4 -4
- package/lib/firebase/config/index.d.ts +1 -1
- package/lib/firebase/crud/aggregate.d.ts +2 -2
- package/lib/firebase/crud/delete.d.ts +1 -1
- package/lib/firebase/crud/get.d.ts +1 -1
- package/lib/firebase/crud/index.d.ts +1 -1
- package/lib/firebase/crud/update.d.ts +1 -1
- package/lib/firebase/helpers/githubAccessHelper.d.ts +3 -3
- package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +2 -2
- package/lib/firebase/helpers/index.d.ts +1 -1
- package/lib/firebase/index.d.ts +1 -1
- package/lib/firebase/index.js +3 -3
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/disconnect.d.ts +1 -1
- package/lib/firebase/oauth/exchangeToken.d.ts +1 -1
- package/lib/firebase/oauth/getConnections.d.ts +1 -1
- package/lib/firebase/oauth/githubAccess.d.ts +6 -6
- package/lib/firebase/oauth/index.d.ts +1 -1
- package/lib/firebase/oauth/refreshToken.d.ts +1 -1
- package/lib/firebase/scheduled/index.d.ts +1 -1
- package/lib/shared/ai/classifyAIError.d.ts +35 -0
- package/lib/shared/ai/costTracker.d.ts +20 -0
- package/lib/shared/ai/createAIRoute.d.ts +20 -0
- package/lib/shared/ai/index.d.ts +6 -0
- package/lib/shared/ai/resolveModelTier.d.ts +14 -0
- package/lib/shared/billing/createCheckout.d.ts +11 -5
- package/lib/shared/billing/helpers/index.d.ts +1 -1
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +3 -3
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +4 -4
- package/lib/shared/billing/idempotency.d.ts +2 -2
- package/lib/shared/billing/index.d.ts +1 -1
- package/lib/shared/billing/webhookHandler.d.ts +1 -1
- package/lib/shared/email/createEmailRoute.d.ts +30 -0
- package/lib/shared/email/index.d.ts +3 -0
- package/lib/shared/email/provider.d.ts +30 -0
- package/lib/shared/email/resendProvider.d.ts +22 -0
- package/lib/shared/errorHandling.d.ts +1 -1
- package/lib/shared/firebase.d.ts +7 -7
- package/lib/shared/github.d.ts +16 -11
- package/lib/shared/index.d.ts +3 -1
- package/lib/shared/index.js +4 -3
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +8 -6
- package/lib/shared/metadata.d.ts +3 -3
- package/lib/shared/oauth/exchangeToken.d.ts +3 -2
- package/lib/shared/oauth/grantAccess.d.ts +3 -2
- package/lib/shared/oauth/index.d.ts +1 -1
- package/lib/shared/schema.d.ts +2 -2
- package/lib/shared/utils/detectFirestore.d.ts +3 -3
- package/lib/shared/utils/external/date.d.ts +2 -2
- package/lib/shared/utils/external/index.d.ts +1 -1
- package/lib/shared/utils/external/references.d.ts +1 -1
- package/lib/shared/utils/external/subscription.d.ts +5 -5
- package/lib/shared/utils/firebaseHelpers.d.ts +2 -2
- package/lib/shared/utils/functionWrapper.d.ts +4 -4
- package/lib/shared/utils/index.d.ts +1 -1
- package/lib/shared/utils/internal/auth.d.ts +4 -4
- package/lib/shared/utils/internal/idempotency.d.ts +3 -2
- package/lib/shared/utils/internal/index.d.ts +1 -1
- package/lib/shared/utils/internal/monitoring.d.ts +7 -7
- package/lib/shared/utils/internal/rateLimiter.d.ts +5 -5
- package/lib/shared/utils/internal/validation.d.ts +4 -4
- package/lib/shared/utils/schemaValidation.d.ts +6 -6
- package/lib/shared/utils/validation.d.ts +9 -9
- package/lib/shared/utils/vercelHelpers.d.ts +2 -2
- package/lib/shared/utils.d.ts +19 -19
- package/lib/shared/validation.d.ts +2 -2
- package/lib/supabase/ai/createAIFunction.d.ts +30 -0
- package/lib/supabase/auth/deleteAccount.d.ts +1 -1
- package/lib/supabase/auth/getCustomClaims.d.ts +1 -1
- package/lib/supabase/auth/getUserAuthStatus.d.ts +1 -1
- package/lib/supabase/auth/removeCustomClaims.d.ts +1 -1
- package/lib/supabase/auth/setCustomClaims.d.ts +12 -4
- package/lib/supabase/baseFunction.d.ts +2 -2
- package/lib/supabase/billing/cancelSubscription.d.ts +1 -1
- package/lib/supabase/billing/changePlan.d.ts +2 -2
- package/lib/supabase/billing/createCheckoutSession.d.ts +1 -1
- package/lib/supabase/billing/createCustomerPortal.d.ts +1 -1
- package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +1 -1
- package/lib/supabase/crud/aggregate.d.ts +2 -1
- package/lib/supabase/crud/create.d.ts +2 -1
- package/lib/supabase/crud/delete.d.ts +1 -0
- package/lib/supabase/crud/get.d.ts +2 -1
- package/lib/supabase/crud/index.d.ts +1 -1
- package/lib/supabase/crud/list.d.ts +2 -1
- package/lib/supabase/crud/update.d.ts +2 -1
- package/lib/supabase/edgeFunction.d.ts +56 -0
- package/lib/supabase/email/createEmailFunction.d.ts +31 -0
- package/lib/supabase/helpers/authProvider.d.ts +2 -2
- package/lib/supabase/index.d.ts +11 -2
- package/lib/supabase/index.js +3 -2
- package/lib/supabase/index.js.map +4 -4
- package/lib/supabase/oauth/disconnect.d.ts +13 -0
- package/lib/supabase/oauth/exchangeToken.d.ts +13 -0
- package/lib/supabase/oauth/getConnections.d.ts +13 -0
- package/lib/supabase/oauth/index.d.ts +12 -0
- package/lib/supabase/oauth/refreshToken.d.ts +13 -0
- package/lib/supabase/utils/idempotency.d.ts +1 -1
- package/lib/supabase/utils/monitoring.d.ts +1 -1
- package/lib/supabase/utils/rateLimiter.d.ts +1 -1
- package/lib/vercel/api/ai/chat.d.ts +29 -0
- package/lib/vercel/api/ai/index.d.ts +1 -0
- package/lib/vercel/api/auth/index.d.ts +1 -1
- package/lib/vercel/api/billing/cancel.d.ts +1 -1
- package/lib/vercel/api/billing/change-plan.d.ts +1 -1
- package/lib/vercel/api/billing/create-checkout-session.d.ts +1 -1
- package/lib/vercel/api/billing/index.d.ts +1 -1
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +1 -1
- package/lib/vercel/api/billing/webhook-handler.d.ts +2 -2
- package/lib/vercel/api/config/constants.d.ts +9 -9
- package/lib/vercel/api/config/firebase-admin.d.ts +2 -2
- package/lib/vercel/api/config/index.d.ts +1 -1
- package/lib/vercel/api/crud/index.d.ts +1 -1
- package/lib/vercel/api/index.d.ts +2 -1
- package/lib/vercel/api/index.js +3 -2
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +1 -1
- package/lib/vercel/api/oauth/grant-github-access.d.ts +1 -1
- package/lib/vercel/api/oauth/index.d.ts +1 -1
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +1 -1
- package/lib/vercel/api/utils/cors.d.ts +4 -4
- package/lib/vercel/baseFunction.d.ts +2 -2
- package/package.json +38 -5
- package/src/firebase/auth/getCustomClaims.ts +3 -3
- package/src/firebase/auth/getUserAuthStatus.ts +3 -3
- package/src/firebase/auth/index.ts +1 -1
- package/src/firebase/auth/removeCustomClaims.ts +3 -3
- package/src/firebase/auth/setCustomClaims.ts +70 -69
- package/src/firebase/baseFunction.ts +2 -2
- package/src/firebase/billing/cancelSubscription.ts +3 -3
- package/src/firebase/billing/changePlan.ts +3 -3
- package/src/firebase/billing/createCheckoutSession.ts +79 -187
- package/src/firebase/billing/createCustomerPortal.ts +1 -1
- package/src/firebase/billing/healthCheck.ts +1 -1
- package/src/firebase/billing/index.d.ts +11 -11
- package/src/firebase/billing/index.ts +1 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -3
- package/src/firebase/billing/webhookHandler.ts +1 -1
- package/src/firebase/config/constants.ts +4 -4
- package/src/firebase/config/index.ts +1 -1
- package/src/firebase/config/secrets.ts +1 -1
- package/src/firebase/crud/aggregate.ts +2 -2
- package/src/firebase/crud/create.ts +2 -2
- package/src/firebase/crud/delete.ts +3 -3
- package/src/firebase/crud/get.ts +1 -1
- package/src/firebase/crud/index.ts +1 -1
- package/src/firebase/crud/list.ts +1 -1
- package/src/firebase/crud/update.ts +2 -2
- package/src/firebase/helpers/githubAccessHelper.ts +4 -4
- package/src/firebase/helpers/githubTeamAccessHelper.ts +3 -3
- package/src/firebase/helpers/index.ts +1 -1
- package/src/firebase/index.d.ts +9 -9
- package/src/firebase/index.ts +1 -1
- package/src/firebase/oauth/disconnect.ts +2 -2
- package/src/firebase/oauth/exchangeToken.ts +6 -6
- package/src/firebase/oauth/getConnections.ts +2 -2
- package/src/firebase/oauth/githubAccess.ts +10 -10
- package/src/firebase/oauth/index.ts +1 -1
- package/src/firebase/oauth/refreshToken.ts +6 -6
- package/src/firebase/registerCrudFunctions.ts +1 -1
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +1 -1
- package/src/firebase/scheduled/index.ts +1 -1
- package/src/shared/__tests__/auth.test.ts +46 -0
- package/src/shared/__tests__/errorHandling.test.ts +66 -1
- package/src/shared/__tests__/firebase.test.ts +293 -0
- package/src/shared/__tests__/internalValidation.test.ts +190 -0
- package/src/shared/__tests__/metadata.test.ts +66 -0
- package/src/shared/__tests__/schema.test.ts +219 -0
- package/src/shared/__tests__/schemaValidation.test.ts +72 -0
- package/src/shared/__tests__/subscription.test.ts +45 -0
- package/src/shared/__tests__/utils.test.ts +281 -0
- package/src/shared/__tests__/validation-env.test.ts +73 -0
- package/src/shared/ai/__tests__/costTracker.test.ts +121 -0
- package/src/shared/ai/classifyAIError.ts +163 -0
- package/src/shared/ai/costTracker.ts +51 -0
- package/src/shared/ai/createAIRoute.ts +397 -0
- package/src/shared/ai/index.ts +8 -0
- package/src/shared/ai/resolveModelTier.ts +58 -0
- package/src/shared/billing/__tests__/createCheckout.test.ts +1 -1
- package/src/shared/billing/__tests__/webhookHandler.test.ts +1 -1
- package/src/shared/billing/createCheckout.ts +156 -11
- package/src/shared/billing/helpers/index.ts +1 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +13 -4
- package/src/shared/billing/helpers/updateUserSubscription.ts +4 -4
- package/src/shared/billing/idempotency.ts +9 -4
- package/src/shared/billing/index.ts +1 -1
- package/src/shared/billing/webhookHandler.ts +7 -7
- package/src/shared/email/createEmailRoute.ts +81 -0
- package/src/shared/email/index.ts +5 -0
- package/src/shared/email/provider.ts +31 -0
- package/src/shared/email/resendProvider.ts +56 -0
- package/src/shared/errorHandling.ts +12 -6
- package/src/shared/firebase.ts +7 -7
- package/src/shared/github.ts +49 -11
- package/src/shared/index.d.ts +9 -9
- package/src/shared/index.ts +3 -1
- package/src/shared/logger.ts +10 -8
- package/src/shared/metadata.ts +3 -3
- package/src/shared/oauth/__tests__/exchangeToken.test.ts +1 -1
- package/src/shared/oauth/exchangeToken.ts +3 -2
- package/src/shared/oauth/grantAccess.ts +3 -2
- package/src/shared/oauth/index.ts +1 -1
- package/src/shared/schema.ts +4 -4
- package/src/shared/utils/__tests__/functionWrapper.test.ts +1 -1
- package/src/shared/utils/detectFirestore.ts +3 -3
- package/src/shared/utils/external/date.ts +5 -5
- package/src/shared/utils/external/index.ts +1 -1
- package/src/shared/utils/external/references.ts +2 -2
- package/src/shared/utils/external/subscription.ts +5 -5
- package/src/shared/utils/firebaseHelpers.ts +3 -3
- package/src/shared/utils/functionWrapper.ts +4 -4
- package/src/shared/utils/index.ts +1 -1
- package/src/shared/utils/internal/auth.ts +7 -7
- package/src/shared/utils/internal/firebase.ts +1 -1
- package/src/shared/utils/internal/idempotency.ts +4 -3
- package/src/shared/utils/internal/index.ts +1 -1
- package/src/shared/utils/internal/monitoring.ts +8 -8
- package/src/shared/utils/internal/rateLimiter.ts +12 -9
- package/src/shared/utils/internal/validation.ts +5 -5
- package/src/shared/utils/schemaValidation.ts +6 -6
- package/src/shared/utils/validation.ts +10 -10
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +19 -19
- package/src/shared/validation.ts +3 -3
- package/src/supabase/ai/createAIFunction.ts +75 -0
- package/src/supabase/auth/deleteAccount.ts +2 -2
- package/src/supabase/auth/getCustomClaims.ts +2 -2
- package/src/supabase/auth/getUserAuthStatus.ts +2 -2
- package/src/supabase/auth/removeCustomClaims.ts +38 -2
- package/src/supabase/auth/setCustomClaims.ts +66 -5
- package/src/supabase/baseFunction.ts +29 -5
- package/src/supabase/billing/cancelSubscription.ts +2 -2
- package/src/supabase/billing/changePlan.ts +8 -3
- package/src/supabase/billing/createCheckoutSession.ts +18 -4
- package/src/supabase/billing/createCustomerPortal.ts +2 -2
- package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -2
- package/src/supabase/crud/aggregate.ts +2 -1
- package/src/supabase/crud/create.ts +2 -1
- package/src/supabase/crud/delete.ts +2 -1
- package/src/supabase/crud/get.ts +2 -1
- package/src/supabase/crud/index.ts +1 -1
- package/src/supabase/crud/list.ts +2 -1
- package/src/supabase/crud/update.ts +2 -1
- package/src/supabase/edgeFunction.ts +195 -0
- package/src/supabase/email/createEmailFunction.ts +75 -0
- package/src/supabase/helpers/authProvider.ts +2 -2
- package/src/supabase/index.ts +22 -2
- package/src/supabase/oauth/disconnect.ts +104 -0
- package/src/supabase/oauth/exchangeToken.ts +183 -0
- package/src/supabase/oauth/getConnections.ts +85 -0
- package/src/supabase/oauth/index.ts +15 -0
- package/src/supabase/oauth/refreshToken.ts +130 -0
- package/src/supabase/registerCrudFunctions.ts +30 -42
- package/src/supabase/utils/idempotency.ts +7 -3
- package/src/supabase/utils/monitoring.ts +1 -1
- package/src/supabase/utils/rateLimiter.ts +1 -1
- package/src/vercel/api/ai/chat.ts +77 -0
- package/src/vercel/api/ai/index.ts +1 -0
- package/src/vercel/api/auth/get-custom-claims.ts +1 -1
- package/src/vercel/api/auth/get-user-auth-status.ts +1 -1
- package/src/vercel/api/auth/index.ts +1 -1
- package/src/vercel/api/auth/remove-custom-claims.ts +1 -1
- package/src/vercel/api/auth/set-custom-claims.ts +2 -4
- package/src/vercel/api/billing/cancel.ts +3 -3
- package/src/vercel/api/billing/change-plan.ts +3 -3
- package/src/vercel/api/billing/create-checkout-session.ts +30 -7
- package/src/vercel/api/billing/customer-portal.ts +1 -1
- package/src/vercel/api/billing/index.ts +1 -1
- package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
- package/src/vercel/api/billing/webhook-handler.ts +11 -4
- package/src/vercel/api/config/constants.ts +9 -9
- package/src/vercel/api/config/firebase-admin.ts +3 -3
- package/src/vercel/api/config/index.ts +1 -1
- package/src/vercel/api/crud/create.ts +1 -1
- package/src/vercel/api/crud/delete.ts +1 -1
- package/src/vercel/api/crud/get.ts +1 -1
- package/src/vercel/api/crud/index.ts +1 -1
- package/src/vercel/api/crud/list.ts +1 -1
- package/src/vercel/api/crud/update.ts +1 -1
- package/src/vercel/api/index.d.ts +1 -1
- package/src/vercel/api/index.ts +2 -1
- package/src/vercel/api/oauth/check-github-access.ts +3 -3
- package/src/vercel/api/oauth/exchange-token.ts +25 -2
- package/src/vercel/api/oauth/grant-github-access.ts +3 -3
- package/src/vercel/api/oauth/index.ts +1 -1
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
- package/src/vercel/api/utils/cors.ts +10 -4
- package/src/vercel/baseFunction.ts +2 -2
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isFieldVisible,
|
|
5
|
+
getVisibleFields,
|
|
6
|
+
filterVisibleFields,
|
|
7
|
+
} from '../schema';
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// isFieldVisible
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
describe('isFieldVisible', () => {
|
|
14
|
+
describe('hidden visibility', () => {
|
|
15
|
+
it('never visible regardless of auth/admin', () => {
|
|
16
|
+
expect(isFieldVisible('secret', 'hidden', true, true)).toBe(false);
|
|
17
|
+
expect(isFieldVisible('secret', 'hidden', false, true)).toBe(false);
|
|
18
|
+
expect(isFieldVisible('secret', 'hidden', true, false)).toBe(false);
|
|
19
|
+
expect(isFieldVisible('secret', 'hidden', false, false)).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('guest visibility', () => {
|
|
24
|
+
it('always visible regardless of auth/admin', () => {
|
|
25
|
+
expect(isFieldVisible('title', 'guest', true, true)).toBe(true);
|
|
26
|
+
expect(isFieldVisible('title', 'guest', false, true)).toBe(true);
|
|
27
|
+
expect(isFieldVisible('title', 'guest', true, false)).toBe(true);
|
|
28
|
+
expect(isFieldVisible('title', 'guest', false, false)).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('admin visibility', () => {
|
|
33
|
+
it('visible only to admins', () => {
|
|
34
|
+
expect(isFieldVisible('config', 'admin', true, true)).toBe(true);
|
|
35
|
+
expect(isFieldVisible('config', 'admin', false, true)).toBe(false);
|
|
36
|
+
expect(isFieldVisible('config', 'admin', true, false)).toBe(true);
|
|
37
|
+
expect(isFieldVisible('config', 'admin', false, false)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('technical visibility', () => {
|
|
42
|
+
it('visible only to admins', () => {
|
|
43
|
+
expect(isFieldVisible('debug', 'technical', true)).toBe(true);
|
|
44
|
+
expect(isFieldVisible('debug', 'technical', false)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('owner visibility', () => {
|
|
49
|
+
it('returns false in aggregate context', () => {
|
|
50
|
+
expect(isFieldVisible('email', 'owner', true, true)).toBe(false);
|
|
51
|
+
expect(isFieldVisible('email', 'owner', false, true)).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('user visibility', () => {
|
|
56
|
+
it('visible to authenticated users', () => {
|
|
57
|
+
expect(isFieldVisible('profile', 'user', false, true)).toBe(true);
|
|
58
|
+
expect(isFieldVisible('profile', 'user', false, false)).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('visible to admins', () => {
|
|
62
|
+
expect(isFieldVisible('profile', 'user', true, true)).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('undefined visibility (defaults to user)', () => {
|
|
67
|
+
it('visible to authenticated users', () => {
|
|
68
|
+
expect(isFieldVisible('field', undefined, false, true)).toBe(true);
|
|
69
|
+
expect(isFieldVisible('field', undefined, false, false)).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('defaults isAuthenticated to true', () => {
|
|
73
|
+
expect(isFieldVisible('field', undefined, false)).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// getVisibleFields
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
describe('getVisibleFields', () => {
|
|
83
|
+
it('returns empty array for non-object schema', () => {
|
|
84
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
85
|
+
|
|
86
|
+
expect(getVisibleFields(null, false)).toEqual([]);
|
|
87
|
+
expect(getVisibleFields('string', false)).toEqual([]);
|
|
88
|
+
expect(getVisibleFields({}, false)).toEqual([]);
|
|
89
|
+
|
|
90
|
+
warnSpy.mockRestore();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('returns all fields without visibility for authenticated user', () => {
|
|
94
|
+
const schema = {
|
|
95
|
+
entries: {
|
|
96
|
+
name: {},
|
|
97
|
+
email: {},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const result = getVisibleFields(schema, false);
|
|
102
|
+
|
|
103
|
+
expect(result).toContain('name');
|
|
104
|
+
expect(result).toContain('email');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('excludes hidden fields', () => {
|
|
108
|
+
const schema = {
|
|
109
|
+
entries: {
|
|
110
|
+
name: {},
|
|
111
|
+
password: { visibility: 'hidden' },
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const result = getVisibleFields(schema, true);
|
|
116
|
+
|
|
117
|
+
expect(result).toContain('name');
|
|
118
|
+
expect(result).not.toContain('password');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('includes admin fields only for admins', () => {
|
|
122
|
+
const schema = {
|
|
123
|
+
entries: {
|
|
124
|
+
name: {},
|
|
125
|
+
config: { visibility: 'admin' },
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
expect(getVisibleFields(schema, true)).toContain('config');
|
|
130
|
+
expect(getVisibleFields(schema, false)).not.toContain('config');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('includes guest fields for everyone', () => {
|
|
134
|
+
const schema = {
|
|
135
|
+
entries: {
|
|
136
|
+
title: { visibility: 'guest' },
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
expect(getVisibleFields(schema, false)).toContain('title');
|
|
141
|
+
expect(getVisibleFields(schema, true)).toContain('title');
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
// filterVisibleFields
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
describe('filterVisibleFields', () => {
|
|
150
|
+
it('returns empty object for null data', () => {
|
|
151
|
+
const schema = { entries: { name: {} } };
|
|
152
|
+
|
|
153
|
+
expect(filterVisibleFields(null, schema, false)).toEqual({});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('returns empty object for undefined data', () => {
|
|
157
|
+
const schema = { entries: { name: {} } };
|
|
158
|
+
|
|
159
|
+
expect(filterVisibleFields(undefined, schema, false)).toEqual({});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('filters data to only visible fields', () => {
|
|
163
|
+
const schema = {
|
|
164
|
+
entries: {
|
|
165
|
+
name: { visibility: 'guest' },
|
|
166
|
+
secret: { visibility: 'hidden' },
|
|
167
|
+
config: { visibility: 'admin' },
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const data = { name: 'Alice', secret: 'pw123', config: 'x', extra: 'y' };
|
|
172
|
+
|
|
173
|
+
const result = filterVisibleFields(data, schema, false);
|
|
174
|
+
|
|
175
|
+
expect(result).toEqual({ name: 'Alice' });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('includes admin fields when isAdmin is true', () => {
|
|
179
|
+
const schema = {
|
|
180
|
+
entries: {
|
|
181
|
+
name: { visibility: 'guest' },
|
|
182
|
+
config: { visibility: 'admin' },
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const data = { name: 'Alice', config: 'x' };
|
|
187
|
+
const result = filterVisibleFields(data, schema, true);
|
|
188
|
+
|
|
189
|
+
expect(result).toEqual({ name: 'Alice', config: 'x' });
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('ignores data keys not in schema', () => {
|
|
193
|
+
const schema = {
|
|
194
|
+
entries: {
|
|
195
|
+
name: {},
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const data = { name: 'Alice', extraField: 'ignored' };
|
|
200
|
+
const result = filterVisibleFields(data, schema, false);
|
|
201
|
+
|
|
202
|
+
expect(result).toEqual({ name: 'Alice' });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('handles missing data keys gracefully', () => {
|
|
206
|
+
const schema = {
|
|
207
|
+
entries: {
|
|
208
|
+
name: {},
|
|
209
|
+
missing: {},
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const data = { name: 'Alice' };
|
|
214
|
+
const result = filterVisibleFields(data, schema, false);
|
|
215
|
+
|
|
216
|
+
expect(result).toEqual({ name: 'Alice' });
|
|
217
|
+
expect(result).not.toHaveProperty('missing');
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import * as v from 'valibot';
|
|
3
|
+
|
|
4
|
+
import { validateRequestData } from '../utils/schemaValidation';
|
|
5
|
+
|
|
6
|
+
const TestSchema = v.object({
|
|
7
|
+
name: v.pipe(v.string(), v.minLength(1)),
|
|
8
|
+
age: v.optional(v.pipe(v.number(), v.minValue(0))),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('validateRequestData', () => {
|
|
12
|
+
it('returns success with parsed data for valid input', () => {
|
|
13
|
+
const result = validateRequestData({ name: 'Alice', age: 30 }, TestSchema);
|
|
14
|
+
|
|
15
|
+
expect(result.success).toBe(true);
|
|
16
|
+
if (result.success) {
|
|
17
|
+
expect(result.data).toEqual({ name: 'Alice', age: 30 });
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('returns success when optional fields are missing', () => {
|
|
22
|
+
const result = validateRequestData({ name: 'Alice' }, TestSchema);
|
|
23
|
+
|
|
24
|
+
expect(result.success).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('returns error for invalid input', () => {
|
|
28
|
+
const result = validateRequestData({ name: '' }, TestSchema);
|
|
29
|
+
|
|
30
|
+
expect(result.success).toBe(false);
|
|
31
|
+
if (!result.success) {
|
|
32
|
+
expect(result.error).toContain('Validation failed');
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('returns error for completely wrong type', () => {
|
|
37
|
+
const result = validateRequestData('not an object', TestSchema);
|
|
38
|
+
|
|
39
|
+
expect(result.success).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns error for null input', () => {
|
|
43
|
+
const result = validateRequestData(null, TestSchema);
|
|
44
|
+
|
|
45
|
+
expect(result.success).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('uses custom schema when provided', () => {
|
|
49
|
+
const strictSchema = v.object({
|
|
50
|
+
name: v.pipe(v.string(), v.minLength(5)),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Passes default but fails custom
|
|
54
|
+
const result = validateRequestData(
|
|
55
|
+
{ name: 'Al' },
|
|
56
|
+
TestSchema,
|
|
57
|
+
strictSchema as any
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
expect(result.success).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('uses default schema when custom is undefined', () => {
|
|
64
|
+
const result = validateRequestData(
|
|
65
|
+
{ name: 'Alice' },
|
|
66
|
+
TestSchema,
|
|
67
|
+
undefined
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
expect(result.success).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
// Mock Firebase deps (only getTierFromPriceId is pure)
|
|
4
|
+
vi.mock('@donotdev/firebase/server', () => ({
|
|
5
|
+
getFirebaseAdminAuth: vi.fn(),
|
|
6
|
+
getFirebaseAdminFirestore: vi.fn(),
|
|
7
|
+
}));
|
|
8
|
+
vi.mock('@donotdev/core/server', () => ({
|
|
9
|
+
SUBSCRIPTION_STATUS: {
|
|
10
|
+
ACTIVE: 'active',
|
|
11
|
+
CANCELED: 'canceled',
|
|
12
|
+
INCOMPLETE: 'incomplete',
|
|
13
|
+
PAST_DUE: 'past_due',
|
|
14
|
+
TRIALING: 'trialing',
|
|
15
|
+
UNPAID: 'unpaid',
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
import { getTierFromPriceId } from '../utils/external/subscription';
|
|
20
|
+
|
|
21
|
+
describe('getTierFromPriceId', () => {
|
|
22
|
+
it('maps monthly pro price to "pro"', () => {
|
|
23
|
+
expect(getTierFromPriceId('price_pro_monthly')).toBe('pro');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('maps yearly pro price to "pro"', () => {
|
|
27
|
+
expect(getTierFromPriceId('price_pro_yearly')).toBe('pro');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('maps monthly ai price to "ai"', () => {
|
|
31
|
+
expect(getTierFromPriceId('price_ai_monthly')).toBe('ai');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('maps yearly ai price to "ai"', () => {
|
|
35
|
+
expect(getTierFromPriceId('price_ai_yearly')).toBe('ai');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns "free" for unknown price ID', () => {
|
|
39
|
+
expect(getTierFromPriceId('price_unknown_123')).toBe('free');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns "free" for empty string', () => {
|
|
43
|
+
expect(getTierFromPriceId('')).toBe('free');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach, afterAll } from 'vitest';
|
|
2
|
+
|
|
3
|
+
// Mock external dependencies that utils.ts imports at module level
|
|
4
|
+
vi.mock('stripe', () => ({
|
|
5
|
+
default: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
vi.mock('@donotdev/firebase/server', () => ({
|
|
8
|
+
getFirebaseAdminAuth: vi.fn(),
|
|
9
|
+
getFirebaseAdminFirestore: vi.fn(),
|
|
10
|
+
initFirebaseAdmin: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
vi.mock('@donotdev/core/server', () => {
|
|
13
|
+
class DoNotDevError extends Error {
|
|
14
|
+
code: string;
|
|
15
|
+
details: any;
|
|
16
|
+
constructor(message: string, code: string = 'internal', opts?: any) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'DoNotDevError';
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.details = opts?.details;
|
|
21
|
+
}
|
|
22
|
+
toString() {
|
|
23
|
+
return `DoNotDevError [${this.code}]: ${this.message}`;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return { DoNotDevError };
|
|
27
|
+
});
|
|
28
|
+
vi.mock('../utils/internal/auth.js', () => ({
|
|
29
|
+
assertAuthenticated: vi.fn((uid: string) => uid),
|
|
30
|
+
assertAdmin: vi.fn((uid: string) => Promise.resolve(uid)),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
import {
|
|
34
|
+
getUserRole,
|
|
35
|
+
createSuccessResponse,
|
|
36
|
+
createErrorResponse,
|
|
37
|
+
validateDocument,
|
|
38
|
+
validateCollectionName,
|
|
39
|
+
assertAuthenticated,
|
|
40
|
+
validateStripeEnvironment,
|
|
41
|
+
} from '../utils';
|
|
42
|
+
|
|
43
|
+
// Need to import DoNotDevError from the mocked module for instanceof checks
|
|
44
|
+
import { DoNotDevError } from '@donotdev/core/server';
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// getUserRole
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
describe('getUserRole', () => {
|
|
51
|
+
it('returns "guest" when auth is null', () => {
|
|
52
|
+
expect(getUserRole(null)).toBe('guest');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('returns "guest" when auth is undefined', () => {
|
|
56
|
+
expect(getUserRole(undefined)).toBe('guest');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('returns "guest" when auth.uid is missing', () => {
|
|
60
|
+
expect(getUserRole({})).toBe('guest');
|
|
61
|
+
expect(getUserRole({ uid: '' })).toBe('guest');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('returns "super" when token.role is "super"', () => {
|
|
65
|
+
expect(getUserRole({ uid: 'u1', token: { role: 'super' } })).toBe('super');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('returns "admin" when token.role is "admin"', () => {
|
|
69
|
+
expect(getUserRole({ uid: 'u1', token: { role: 'admin' } })).toBe('admin');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('returns "super" via legacy isSuper flag', () => {
|
|
73
|
+
expect(getUserRole({ uid: 'u1', token: { isSuper: true } })).toBe('super');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('returns "admin" via legacy isAdmin flag', () => {
|
|
77
|
+
expect(getUserRole({ uid: 'u1', token: { isAdmin: true } })).toBe('admin');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('returns "user" for authenticated user without special claims', () => {
|
|
81
|
+
expect(getUserRole({ uid: 'u1', token: {} })).toBe('user');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('returns "user" when token is missing', () => {
|
|
85
|
+
expect(getUserRole({ uid: 'u1' })).toBe('user');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('prefers role string over legacy flags', () => {
|
|
89
|
+
expect(
|
|
90
|
+
getUserRole({ uid: 'u1', token: { role: 'admin', isSuper: true } })
|
|
91
|
+
).toBe('admin');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// createSuccessResponse
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
describe('createSuccessResponse', () => {
|
|
100
|
+
it('wraps data in success envelope', () => {
|
|
101
|
+
const result = createSuccessResponse({ id: '123' });
|
|
102
|
+
|
|
103
|
+
expect(result).toEqual({ success: true, data: { id: '123' } });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('handles null data', () => {
|
|
107
|
+
const result = createSuccessResponse(null);
|
|
108
|
+
|
|
109
|
+
expect(result).toEqual({ success: true, data: null });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('handles array data', () => {
|
|
113
|
+
const result = createSuccessResponse([1, 2, 3]);
|
|
114
|
+
|
|
115
|
+
expect(result).toEqual({ success: true, data: [1, 2, 3] });
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// createErrorResponse
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
describe('createErrorResponse', () => {
|
|
124
|
+
it('formats DoNotDevError with code and details', () => {
|
|
125
|
+
const error = new DoNotDevError('Bad input', 'invalid-argument', {
|
|
126
|
+
details: { field: 'email' },
|
|
127
|
+
});
|
|
128
|
+
const result = createErrorResponse(error);
|
|
129
|
+
|
|
130
|
+
expect(result.success).toBe(false);
|
|
131
|
+
expect(result.error.code).toBe('invalid-argument');
|
|
132
|
+
expect(result.error.message).toBe('Bad input');
|
|
133
|
+
expect(result.error.details).toEqual({ field: 'email' });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('formats generic Error as internal', () => {
|
|
137
|
+
const result = createErrorResponse(new Error('oops'));
|
|
138
|
+
|
|
139
|
+
expect(result.success).toBe(false);
|
|
140
|
+
expect(result.error.code).toBe('internal');
|
|
141
|
+
expect(result.error.message).toBe('oops');
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('formats non-Error as internal with generic message', () => {
|
|
145
|
+
const result = createErrorResponse('some string');
|
|
146
|
+
|
|
147
|
+
expect(result.success).toBe(false);
|
|
148
|
+
expect(result.error.code).toBe('internal');
|
|
149
|
+
expect(result.error.message).toBe('An unexpected error occurred');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('formats null as internal with generic message', () => {
|
|
153
|
+
const result = createErrorResponse(null);
|
|
154
|
+
|
|
155
|
+
expect(result.success).toBe(false);
|
|
156
|
+
expect(result.error.code).toBe('internal');
|
|
157
|
+
expect(result.error.message).toBe('An unexpected error occurred');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
// validateDocument
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
describe('validateDocument', () => {
|
|
166
|
+
it('accepts valid object data', () => {
|
|
167
|
+
expect(() => validateDocument({ name: 'test' })).not.toThrow();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('rejects null', () => {
|
|
171
|
+
expect(() => validateDocument(null)).toThrow('Invalid document data');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('rejects undefined', () => {
|
|
175
|
+
expect(() => validateDocument(undefined)).toThrow('Invalid document data');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('rejects non-object', () => {
|
|
179
|
+
expect(() => validateDocument('string')).toThrow('Invalid document data');
|
|
180
|
+
expect(() => validateDocument(123)).toThrow('Invalid document data');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('rejects arrays', () => {
|
|
184
|
+
expect(() => validateDocument([1, 2])).toThrow(
|
|
185
|
+
'Document data cannot be an array'
|
|
186
|
+
);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
// validateCollectionName
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
|
|
194
|
+
describe('validateCollectionName', () => {
|
|
195
|
+
it('accepts valid collection names', () => {
|
|
196
|
+
expect(() => validateCollectionName('users')).not.toThrow();
|
|
197
|
+
expect(() => validateCollectionName('myCollection')).not.toThrow();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('rejects empty string', () => {
|
|
201
|
+
expect(() => validateCollectionName('')).toThrow(
|
|
202
|
+
'Collection name is required'
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('rejects names with slashes (path traversal)', () => {
|
|
207
|
+
expect(() => validateCollectionName('users/admin')).toThrow(
|
|
208
|
+
'Invalid collection name'
|
|
209
|
+
);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('rejects names with double dots (path traversal)', () => {
|
|
213
|
+
expect(() => validateCollectionName('..users')).toThrow(
|
|
214
|
+
'Invalid collection name'
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('rejects names starting with underscore (internal collections)', () => {
|
|
219
|
+
expect(() => validateCollectionName('_internal')).toThrow(
|
|
220
|
+
'Invalid collection name'
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
// assertAuthenticated
|
|
227
|
+
// ---------------------------------------------------------------------------
|
|
228
|
+
|
|
229
|
+
describe('assertAuthenticated', () => {
|
|
230
|
+
it('returns uid when auth has uid', () => {
|
|
231
|
+
const result = assertAuthenticated({ uid: 'user-123' });
|
|
232
|
+
|
|
233
|
+
expect(result).toBe('user-123');
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('throws when auth is null', () => {
|
|
237
|
+
expect(() => assertAuthenticated(null)).toThrow(
|
|
238
|
+
'User must be authenticated'
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('throws when auth has no uid', () => {
|
|
243
|
+
expect(() => assertAuthenticated({})).toThrow(
|
|
244
|
+
'User must be authenticated'
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('throws when auth.uid is empty string', () => {
|
|
249
|
+
expect(() => assertAuthenticated({ uid: '' })).toThrow(
|
|
250
|
+
'User must be authenticated'
|
|
251
|
+
);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// validateStripeEnvironment
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
describe('validateStripeEnvironment', () => {
|
|
260
|
+
const originalEnv = { ...process.env };
|
|
261
|
+
|
|
262
|
+
beforeEach(() => {
|
|
263
|
+
delete process.env.STRIPE_SECRET_KEY;
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
afterAll(() => {
|
|
267
|
+
process.env = { ...originalEnv };
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('throws when STRIPE_SECRET_KEY is missing', () => {
|
|
271
|
+
expect(() => validateStripeEnvironment()).toThrow(
|
|
272
|
+
'Missing STRIPE_SECRET_KEY'
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('does not throw when STRIPE_SECRET_KEY is set', () => {
|
|
277
|
+
process.env.STRIPE_SECRET_KEY = 'sk_test_123';
|
|
278
|
+
|
|
279
|
+
expect(() => validateStripeEnvironment()).not.toThrow();
|
|
280
|
+
});
|
|
281
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { validateEnvironment } from '../validation';
|
|
4
|
+
|
|
5
|
+
describe('validateEnvironment', () => {
|
|
6
|
+
const originalEnv = { ...process.env };
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
delete process.env.STRIPE_SECRET_KEY;
|
|
10
|
+
delete process.env.STRIPE_WEBHOOK_SECRET;
|
|
11
|
+
delete process.env.STRIPE_API_VERSION;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
process.env = { ...originalEnv };
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('throws listing all missing env vars', () => {
|
|
19
|
+
expect(() => validateEnvironment()).toThrow('Missing required environment variables');
|
|
20
|
+
expect(() => validateEnvironment()).toThrow('STRIPE_SECRET_KEY');
|
|
21
|
+
expect(() => validateEnvironment()).toThrow('STRIPE_WEBHOOK_SECRET');
|
|
22
|
+
expect(() => validateEnvironment()).toThrow('STRIPE_API_VERSION');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('throws for missing single env var', () => {
|
|
26
|
+
process.env.STRIPE_SECRET_KEY = 'sk_test_abc';
|
|
27
|
+
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_abc';
|
|
28
|
+
|
|
29
|
+
expect(() => validateEnvironment()).toThrow('STRIPE_API_VERSION');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('throws for invalid STRIPE_API_VERSION format', () => {
|
|
33
|
+
process.env.STRIPE_SECRET_KEY = 'sk_test_abc';
|
|
34
|
+
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_abc';
|
|
35
|
+
process.env.STRIPE_API_VERSION = 'bad-format';
|
|
36
|
+
|
|
37
|
+
expect(() => validateEnvironment()).toThrow('Invalid STRIPE_API_VERSION format');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('throws for invalid STRIPE_SECRET_KEY format', () => {
|
|
41
|
+
process.env.STRIPE_SECRET_KEY = 'pk_test_abc';
|
|
42
|
+
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_abc';
|
|
43
|
+
process.env.STRIPE_API_VERSION = '2025-09-30.clover';
|
|
44
|
+
|
|
45
|
+
expect(() => validateEnvironment()).toThrow('Invalid STRIPE_SECRET_KEY format');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('passes with all valid env vars', () => {
|
|
49
|
+
process.env.STRIPE_SECRET_KEY = 'sk_test_abc';
|
|
50
|
+
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_abc';
|
|
51
|
+
process.env.STRIPE_API_VERSION = '2025-09-30.clover';
|
|
52
|
+
|
|
53
|
+
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
54
|
+
|
|
55
|
+
expect(() => validateEnvironment()).not.toThrow();
|
|
56
|
+
|
|
57
|
+
consoleSpy.mockRestore();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('accepts various valid API version formats', () => {
|
|
61
|
+
process.env.STRIPE_SECRET_KEY = 'sk_live_abc';
|
|
62
|
+
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_abc';
|
|
63
|
+
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
64
|
+
|
|
65
|
+
process.env.STRIPE_API_VERSION = '2024-12-18.acacia';
|
|
66
|
+
expect(() => validateEnvironment()).not.toThrow();
|
|
67
|
+
|
|
68
|
+
process.env.STRIPE_API_VERSION = '2025-09-30.clover';
|
|
69
|
+
expect(() => validateEnvironment()).not.toThrow();
|
|
70
|
+
|
|
71
|
+
consoleSpy.mockRestore();
|
|
72
|
+
});
|
|
73
|
+
});
|