|
| 1 | +import { describe, test, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type { License } from '@sourcebot/db'; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + env: { |
| 6 | + SOURCEBOT_PUBLIC_KEY_PATH: '/tmp/test-key', |
| 7 | + SOURCEBOT_EE_LICENSE_KEY: undefined as string | undefined, |
| 8 | + } as Record<string, string | undefined>, |
| 9 | + verifySignature: vi.fn(() => true), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('./env.server.js', () => ({ |
| 13 | + env: mocks.env, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('./crypto.js', () => ({ |
| 17 | + verifySignature: mocks.verifySignature, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('./logger.js', () => ({ |
| 21 | + createLogger: () => ({ |
| 22 | + error: vi.fn(), |
| 23 | + info: vi.fn(), |
| 24 | + warn: vi.fn(), |
| 25 | + debug: vi.fn(), |
| 26 | + }), |
| 27 | +})); |
| 28 | + |
| 29 | +import { |
| 30 | + isAnonymousAccessAvailable, |
| 31 | + getEntitlements, |
| 32 | + hasEntitlement, |
| 33 | +} from './entitlements.js'; |
| 34 | + |
| 35 | +const encodeOfflineKey = (payload: object): string => { |
| 36 | + const encoded = Buffer.from(JSON.stringify(payload)).toString('base64url'); |
| 37 | + return `sourcebot_ee_${encoded}`; |
| 38 | +}; |
| 39 | + |
| 40 | +const futureDate = new Date(Date.now() + 1000 * 60 * 60 * 24 * 365).toISOString(); |
| 41 | +const pastDate = new Date(Date.now() - 1000 * 60 * 60 * 24).toISOString(); |
| 42 | + |
| 43 | +const validOfflineKey = (overrides: { seats?: number; expiryDate?: string } = {}) => |
| 44 | + encodeOfflineKey({ |
| 45 | + id: 'test-customer', |
| 46 | + expiryDate: overrides.expiryDate ?? futureDate, |
| 47 | + ...(overrides.seats !== undefined ? { seats: overrides.seats } : {}), |
| 48 | + sig: 'fake-sig', |
| 49 | + }); |
| 50 | + |
| 51 | +const makeLicense = (overrides: Partial<License> = {}): License => ({ |
| 52 | + id: 'lic_1', |
| 53 | + orgId: 1, |
| 54 | + activationCode: 'code', |
| 55 | + entitlements: [], |
| 56 | + seats: null, |
| 57 | + status: null, |
| 58 | + lastSyncAt: null, |
| 59 | + createdAt: new Date(), |
| 60 | + updatedAt: new Date(), |
| 61 | + ...overrides, |
| 62 | +}); |
| 63 | + |
| 64 | +beforeEach(() => { |
| 65 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = undefined; |
| 66 | + mocks.verifySignature.mockReturnValue(true); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('isAnonymousAccessAvailable', () => { |
| 70 | + describe('without any license', () => { |
| 71 | + test('returns true when license is null', () => { |
| 72 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + test('returns true when license has no status', () => { |
| 76 | + expect(isAnonymousAccessAvailable(makeLicense())).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + test('returns true when license status is canceled', () => { |
| 80 | + expect(isAnonymousAccessAvailable(makeLicense({ status: 'canceled' }))).toBe(true); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('with an active online license', () => { |
| 85 | + test.each(['active', 'trialing', 'past_due'] as const)( |
| 86 | + 'returns false when status is %s', |
| 87 | + (status) => { |
| 88 | + expect(isAnonymousAccessAvailable(makeLicense({ status }))).toBe(false); |
| 89 | + } |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('with an offline license key', () => { |
| 94 | + test('returns false when offline key has a seat count', () => { |
| 95 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 100 }); |
| 96 | + expect(isAnonymousAccessAvailable(null)).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + test('returns true when offline key has no seat count (unlimited)', () => { |
| 100 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey(); |
| 101 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + test('unlimited offline key beats an active online license', () => { |
| 105 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey(); |
| 106 | + expect(isAnonymousAccessAvailable(makeLicense({ status: 'active' }))).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + test('falls through to online license check when offline key is expired', () => { |
| 110 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 100, expiryDate: pastDate }); |
| 111 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 112 | + expect(isAnonymousAccessAvailable(makeLicense({ status: 'active' }))).toBe(false); |
| 113 | + }); |
| 114 | + |
| 115 | + test('falls through when offline key is malformed', () => { |
| 116 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = 'sourcebot_ee_not-valid-base64-or-json'; |
| 117 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + test('falls through when offline key has wrong prefix', () => { |
| 121 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = 'bogus_prefix_xyz'; |
| 122 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + test('falls through when offline key signature is invalid', () => { |
| 126 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 100 }); |
| 127 | + mocks.verifySignature.mockReturnValue(false); |
| 128 | + expect(isAnonymousAccessAvailable(null)).toBe(true); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('getEntitlements', () => { |
| 134 | + test('returns empty array when no license and no offline key', () => { |
| 135 | + expect(getEntitlements(null)).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + test('returns license.entitlements when license is active', () => { |
| 139 | + const license = makeLicense({ status: 'active', entitlements: ['sso', 'audit'] }); |
| 140 | + expect(getEntitlements(license)).toEqual(['sso', 'audit']); |
| 141 | + }); |
| 142 | + |
| 143 | + test('returns empty when license has no status', () => { |
| 144 | + expect(getEntitlements(makeLicense({ entitlements: ['sso'] }))).toEqual([]); |
| 145 | + }); |
| 146 | + |
| 147 | + test('returns all entitlements when offline key is valid', () => { |
| 148 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 50 }); |
| 149 | + const result = getEntitlements(null); |
| 150 | + expect(result).toContain('sso'); |
| 151 | + expect(result).toContain('audit'); |
| 152 | + expect(result).toContain('search-contexts'); |
| 153 | + }); |
| 154 | + |
| 155 | + test('falls through when offline key is expired', () => { |
| 156 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 50, expiryDate: pastDate }); |
| 157 | + expect(getEntitlements(null)).toEqual([]); |
| 158 | + expect( |
| 159 | + getEntitlements(makeLicense({ status: 'active', entitlements: ['sso'] })) |
| 160 | + ).toEqual(['sso']); |
| 161 | + }); |
| 162 | + |
| 163 | + test('falls through when offline key is malformed', () => { |
| 164 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = 'sourcebot_ee_not-a-valid-payload'; |
| 165 | + expect(getEntitlements(null)).toEqual([]); |
| 166 | + }); |
| 167 | +}); |
| 168 | + |
| 169 | +describe('hasEntitlement', () => { |
| 170 | + test('returns true when entitlement is present in license', () => { |
| 171 | + expect( |
| 172 | + hasEntitlement('sso', makeLicense({ status: 'active', entitlements: ['sso'] })) |
| 173 | + ).toBe(true); |
| 174 | + }); |
| 175 | + |
| 176 | + test('returns false when entitlement is absent from license', () => { |
| 177 | + expect( |
| 178 | + hasEntitlement('audit', makeLicense({ status: 'active', entitlements: ['sso'] })) |
| 179 | + ).toBe(false); |
| 180 | + }); |
| 181 | + |
| 182 | + test('returns true for any entitlement when offline key is valid', () => { |
| 183 | + mocks.env.SOURCEBOT_EE_LICENSE_KEY = validOfflineKey({ seats: 50 }); |
| 184 | + expect(hasEntitlement('sso', null)).toBe(true); |
| 185 | + expect(hasEntitlement('audit', null)).toBe(true); |
| 186 | + }); |
| 187 | +}); |
0 commit comments