|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 4 | +import { TenantContextService } from '../src/tenant-context'; |
| 5 | +import type { TenantRoutingConfig } from '@objectstack/spec/cloud'; |
| 6 | + |
| 7 | +describe('TenantContextService', () => { |
| 8 | + let service: TenantContextService; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + const config: TenantRoutingConfig = { |
| 12 | + enabled: true, |
| 13 | + identificationSources: ['header', 'custom_domain'], |
| 14 | + tenantHeaderName: 'X-Tenant-ID', |
| 15 | + customDomainMapping: { |
| 16 | + 'app.acme.com': '550e8400-e29b-41d4-a716-446655440000', |
| 17 | + }, |
| 18 | + }; |
| 19 | + service = new TenantContextService(config); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('resolveTenantContext', () => { |
| 23 | + it('should extract tenant from header', async () => { |
| 24 | + const context = await service.resolveTenantContext({ |
| 25 | + headers: { |
| 26 | + 'X-Tenant-ID': '550e8400-e29b-41d4-a716-446655440000', |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(context).toBeDefined(); |
| 31 | + expect(context?.tenantId).toBe('550e8400-e29b-41d4-a716-446655440000'); |
| 32 | + expect(context?.databaseUrl).toBe('libsql://550e8400-e29b-41d4-a716-446655440000.turso.io'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should extract tenant from custom domain', async () => { |
| 36 | + const context = await service.resolveTenantContext({ |
| 37 | + hostname: 'app.acme.com', |
| 38 | + }); |
| 39 | + |
| 40 | + expect(context).toBeDefined(); |
| 41 | + expect(context?.tenantId).toBe('550e8400-e29b-41d4-a716-446655440000'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return null when multi-tenant is disabled', async () => { |
| 45 | + const disabledConfig: TenantRoutingConfig = { |
| 46 | + enabled: false, |
| 47 | + identificationSources: [], |
| 48 | + }; |
| 49 | + const disabledService = new TenantContextService(disabledConfig); |
| 50 | + |
| 51 | + const context = await disabledService.resolveTenantContext({ |
| 52 | + headers: { 'X-Tenant-ID': '123' }, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(context).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should cache tenant contexts', async () => { |
| 59 | + const context1 = await service.resolveTenantContext({ |
| 60 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 61 | + }); |
| 62 | + |
| 63 | + const context2 = await service.resolveTenantContext({ |
| 64 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(context1).toBe(context2); // Same object reference from cache |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('cache management', () => { |
| 72 | + it('should clear cache', async () => { |
| 73 | + await service.resolveTenantContext({ |
| 74 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 75 | + }); |
| 76 | + |
| 77 | + service.clearCache(); |
| 78 | + |
| 79 | + // After clearing cache, should create new context |
| 80 | + const context = await service.resolveTenantContext({ |
| 81 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(context).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should invalidate specific tenant', async () => { |
| 88 | + await service.resolveTenantContext({ |
| 89 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 90 | + }); |
| 91 | + |
| 92 | + service.invalidateTenant('tenant-123'); |
| 93 | + |
| 94 | + // Should still work after invalidation |
| 95 | + const context = await service.resolveTenantContext({ |
| 96 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(context).toBeDefined(); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments