|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Multi-tenancy boot guard (#6915, mirroring driver-mongodb's #3724 guard). |
| 5 | + * |
| 6 | + * The guard is pure (env posture + object metadata), and this driver holds its |
| 7 | + * store in a plain object, so nothing here needs a server. The driver-level |
| 8 | + * cases assert two things at once: that the refusal fires at construction and |
| 9 | + * at `connect()`, and — the risk this card carries — that the ordinary |
| 10 | + * single-tenant in-process path the dogfood suites, `@objectstack/verify` and |
| 11 | + * the example apps depend on still boots and serves clean. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 15 | +import { |
| 16 | + assertSingleTenantPosture, |
| 17 | + assertObjectsNotTenantScoped, |
| 18 | + declaresTenantScope, |
| 19 | + MemoryMultiTenantUnsupportedError, |
| 20 | + MULTI_TENANT_UNSUPPORTED_CODE, |
| 21 | +} from './memory-tenancy-guard.js'; |
| 22 | +import { InMemoryDriver } from './memory-driver.js'; |
| 23 | + |
| 24 | +const ORIGINAL_MULTI_ORG = process.env.OS_MULTI_ORG_ENABLED; |
| 25 | +const ORIGINAL_POSTURE = process.env.OS_TENANCY_POSTURE; |
| 26 | + |
| 27 | +function makeDriver() { |
| 28 | + return new InMemoryDriver({ persistence: false }); |
| 29 | +} |
| 30 | + |
| 31 | +describe('multi-tenancy boot guard (#6915)', () => { |
| 32 | + beforeEach(() => { |
| 33 | + delete process.env.OS_MULTI_ORG_ENABLED; |
| 34 | + delete process.env.OS_TENANCY_POSTURE; |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + if (ORIGINAL_MULTI_ORG === undefined) delete process.env.OS_MULTI_ORG_ENABLED; |
| 39 | + else process.env.OS_MULTI_ORG_ENABLED = ORIGINAL_MULTI_ORG; |
| 40 | + if (ORIGINAL_POSTURE === undefined) delete process.env.OS_TENANCY_POSTURE; |
| 41 | + else process.env.OS_TENANCY_POSTURE = ORIGINAL_POSTURE; |
| 42 | + }); |
| 43 | + |
| 44 | + describe('assertSingleTenantPosture', () => { |
| 45 | + it('passes when nothing is configured (posture derives to `single`)', () => { |
| 46 | + expect(() => assertSingleTenantPosture()).not.toThrow(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('passes when OS_MULTI_ORG_ENABLED is explicitly false', () => { |
| 50 | + process.env.OS_MULTI_ORG_ENABLED = 'false'; |
| 51 | + expect(() => assertSingleTenantPosture()).not.toThrow(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('passes for an explicit single posture', () => { |
| 55 | + process.env.OS_TENANCY_POSTURE = 'single'; |
| 56 | + expect(() => assertSingleTenantPosture()).not.toThrow(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('throws a coded error when multi-org mode is on', () => { |
| 60 | + process.env.OS_MULTI_ORG_ENABLED = 'true'; |
| 61 | + try { |
| 62 | + assertSingleTenantPosture(); |
| 63 | + expect.unreachable('expected the guard to throw'); |
| 64 | + } catch (err) { |
| 65 | + expect(err).toBeInstanceOf(MemoryMultiTenantUnsupportedError); |
| 66 | + expect((err as any).code).toBe(MULTI_TENANT_UNSUPPORTED_CODE); |
| 67 | + // The message must name the knobs and the escape route, not just fail. |
| 68 | + expect((err as Error).message).toContain('OS_MULTI_ORG_ENABLED'); |
| 69 | + expect((err as Error).message).toContain('OS_TENANCY_POSTURE'); |
| 70 | + expect((err as Error).message).toContain('@objectstack/driver-sql'); |
| 71 | + expect((err as Error).message).toContain('6915'); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it('treats any non-`false` value as enabled (matches resolveMultiOrgEnabled)', () => { |
| 76 | + process.env.OS_MULTI_ORG_ENABLED = '1'; |
| 77 | + expect(() => assertSingleTenantPosture()).toThrow(MemoryMultiTenantUnsupportedError); |
| 78 | + }); |
| 79 | + |
| 80 | + // OS_TENANCY_POSTURE (ADR-0105 D1) supersedes the boolean — BOTH walled |
| 81 | + // postures need an organization wall this driver cannot draw. |
| 82 | + it.each(['isolated', 'group', 'multi'])( |
| 83 | + 'throws for the `%s` posture even with OS_MULTI_ORG_ENABLED unset', |
| 84 | + (posture) => { |
| 85 | + process.env.OS_TENANCY_POSTURE = posture; |
| 86 | + const err = (() => { |
| 87 | + try { |
| 88 | + assertSingleTenantPosture(); |
| 89 | + return null; |
| 90 | + } catch (e) { |
| 91 | + return e; |
| 92 | + } |
| 93 | + })(); |
| 94 | + expect(err).toBeInstanceOf(MemoryMultiTenantUnsupportedError); |
| 95 | + // `multi` is the legacy alias, normalized to `isolated` by the resolver. |
| 96 | + expect((err as Error).message).toContain(posture === 'multi' ? 'isolated' : posture); |
| 97 | + }, |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('declaresTenantScope', () => { |
| 102 | + it('is true only for an explicit tenancy.enabled === true', () => { |
| 103 | + expect(declaresTenantScope({ name: 'task', tenancy: { enabled: true } })).toBe(true); |
| 104 | + expect(declaresTenantScope({ name: 'task', tenancy: { enabled: false } })).toBe(false); |
| 105 | + expect(declaresTenantScope({ name: 'task', tenancy: {} })).toBe(false); |
| 106 | + expect(declaresTenantScope({ name: 'task' })).toBe(false); |
| 107 | + expect(declaresTenantScope(null)).toBe(false); |
| 108 | + expect(declaresTenantScope(undefined)).toBe(false); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('assertObjectsNotTenantScoped', () => { |
| 113 | + it('passes for objects that do not declare tenancy', () => { |
| 114 | + expect(() => |
| 115 | + assertObjectsNotTenantScoped([ |
| 116 | + { object: 'task', schema: { name: 'task' } }, |
| 117 | + { object: 'sys_license', schema: { name: 'sys_license', tenancy: { enabled: false } } }, |
| 118 | + ]), |
| 119 | + ).not.toThrow(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('names every offending object in a single message', () => { |
| 123 | + try { |
| 124 | + assertObjectsNotTenantScoped([ |
| 125 | + { object: 'task', schema: { name: 'task' } }, |
| 126 | + { object: 'account', schema: { name: 'account', tenancy: { enabled: true } } }, |
| 127 | + { object: 'contact', schema: { name: 'contact', tenancy: { enabled: true } } }, |
| 128 | + ]); |
| 129 | + expect.unreachable('expected the guard to throw'); |
| 130 | + } catch (err) { |
| 131 | + expect((err as any).code).toBe(MULTI_TENANT_UNSUPPORTED_CODE); |
| 132 | + const message = (err as Error).message; |
| 133 | + expect(message).toContain('`account`'); |
| 134 | + expect(message).toContain('`contact`'); |
| 135 | + expect(message).not.toContain('`task`'); |
| 136 | + // Plural remedy when there is more than one offender. |
| 137 | + expect(message).toContain('these objects'); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('stays singular for a lone offender — the shape `syncSchema` actually calls', () => { |
| 142 | + try { |
| 143 | + assertObjectsNotTenantScoped([ |
| 144 | + { object: 'account', schema: { name: 'account', tenancy: { enabled: true } } }, |
| 145 | + ]); |
| 146 | + expect.unreachable('expected the guard to throw'); |
| 147 | + } catch (err) { |
| 148 | + const message = (err as Error).message; |
| 149 | + expect(message).toContain('object declaring'); |
| 150 | + expect(message).toContain('this object'); |
| 151 | + } |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('InMemoryDriver wiring', () => { |
| 156 | + it('the constructor refuses in multi-tenant mode', () => { |
| 157 | + process.env.OS_MULTI_ORG_ENABLED = 'true'; |
| 158 | + // Construction is the earliest seam, and the only one no escape hatch |
| 159 | + // reaches: `OS_ALLOW_DRIVER_CONNECT_FAILURE=1` downgrades a `connect()` |
| 160 | + // rejection to a warning, which would boot the deployment unisolated. |
| 161 | + expect(() => makeDriver()).toThrow(MemoryMultiTenantUnsupportedError); |
| 162 | + }); |
| 163 | + |
| 164 | + it.each(['isolated', 'group'])('the constructor refuses the `%s` posture', (posture) => { |
| 165 | + process.env.OS_TENANCY_POSTURE = posture; |
| 166 | + expect(() => makeDriver()).toThrow(MemoryMultiTenantUnsupportedError); |
| 167 | + }); |
| 168 | + |
| 169 | + it('connect() refuses when the posture flips after construction', async () => { |
| 170 | + const driver = makeDriver(); // built single-tenant |
| 171 | + process.env.OS_TENANCY_POSTURE = 'isolated'; |
| 172 | + await expect(driver.connect()).rejects.toThrow(MemoryMultiTenantUnsupportedError); |
| 173 | + }); |
| 174 | + |
| 175 | + it('syncSchema() refuses a tenant-scoped object, and allocates no table for it', async () => { |
| 176 | + const driver = makeDriver(); |
| 177 | + await driver.connect(); |
| 178 | + await expect( |
| 179 | + driver.syncSchema('account', { name: 'account', tenancy: { enabled: true } }), |
| 180 | + ).rejects.toThrow(MemoryMultiTenantUnsupportedError); |
| 181 | + // The refusal happens before the store is touched: reading the object back |
| 182 | + // finds nothing was created for it. |
| 183 | + const stats = driver.getSchemaSyncStats?.(); |
| 184 | + expect(stats?.created ?? []).not.toContain('account'); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + // The risk this card carries is a guard that is too EAGER: `driver-memory` is |
| 189 | + // the in-process store behind the dev stack, the example apps and every |
| 190 | + // single-tenant embedding. None of those set a posture, so none of them may |
| 191 | + // notice this guard exists. |
| 192 | + describe('the ordinary single-tenant path still boots clean', () => { |
| 193 | + it('constructs, connects, syncs and round-trips a record with no posture set', async () => { |
| 194 | + const driver = makeDriver(); |
| 195 | + await driver.connect(); |
| 196 | + await driver.syncSchema('task', { |
| 197 | + name: 'task', |
| 198 | + fields: { title: { type: 'text' } }, |
| 199 | + }); |
| 200 | + await driver.create('task', { title: 'hello' }); |
| 201 | + const rows = await driver.find('task', {}); |
| 202 | + expect(rows).toHaveLength(1); |
| 203 | + expect(rows[0].title).toBe('hello'); |
| 204 | + await driver.disconnect?.(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('is unaffected by an explicit `single` posture', async () => { |
| 208 | + process.env.OS_TENANCY_POSTURE = 'single'; |
| 209 | + const driver = makeDriver(); |
| 210 | + await expect(driver.connect()).resolves.not.toThrow(); |
| 211 | + await expect( |
| 212 | + driver.syncSchema('task', { name: 'task', fields: {} }), |
| 213 | + ).resolves.not.toThrow(); |
| 214 | + }); |
| 215 | + |
| 216 | + it('syncs an object that omits the tenancy block, and one that disables it', async () => { |
| 217 | + const driver = makeDriver(); |
| 218 | + await driver.connect(); |
| 219 | + await expect(driver.syncSchema('task', { name: 'task' })).resolves.not.toThrow(); |
| 220 | + await expect( |
| 221 | + driver.syncSchema('sys_license', { name: 'sys_license', tenancy: { enabled: false } }), |
| 222 | + ).resolves.not.toThrow(); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments