|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [ADR-0131 8c-1] The `[tenant-audit]` line skips an INSERT whose row carries |
| 4 | +// its tenant column, and only an insert. |
| 5 | +// |
| 6 | +// An insert names its organization in two places: `options.tenantId`, which |
| 7 | +// `injectTenantOnInsert` stamps onto the row, and the row's own tenant column. |
| 8 | +// A `create` / `bulkCreate` whose every row already carries that column is |
| 9 | +// confined to the organizations it names, so the line had nothing to report — |
| 10 | +// yet a walled boot printed one for every elevated seed insert (they reach the |
| 11 | +// driver with no `tenantId` and an `organization_id` on the row; a system |
| 12 | +// insert with none is refused above the driver, ADR-0131 D9). |
| 13 | +// |
| 14 | +// The other half is what must NOT move: an update or a delete with no |
| 15 | +// `tenantId` still warns whatever its payload says (the row's column is a value |
| 16 | +// being written there, not the statement's scope), and so does an upsert, whose |
| 17 | +// merge leg can reach another organization's row. The prefix is byte-identical, |
| 18 | +// because `delivery-claim-tenant-audit.integration.test.ts` matches it. |
| 19 | +// |
| 20 | +// Real `SqlDriver` against a real in-memory SQLite; the logger is the |
| 21 | +// assertion surface. |
| 22 | + |
| 23 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 24 | +import { SqlDriver } from '../src/index.js'; |
| 25 | + |
| 26 | +const OLD_POSTURE = process.env.OS_TENANCY_POSTURE; |
| 27 | + |
| 28 | +let driver: SqlDriver; |
| 29 | +let warns: Array<{ msg: string; meta: any }>; |
| 30 | + |
| 31 | +/** |
| 32 | + * An organization-less row is refused by the NOT NULL organization column |
| 33 | + * (ADR-0131 D1) — AFTER the audit has run, which is the order these controls |
| 34 | + * read: the line is printed, then the statement fails. |
| 35 | + */ |
| 36 | +const refused = (write: Promise<unknown>) => |
| 37 | + write.then( |
| 38 | + () => { throw new Error('expected the organization-less insert to be refused'); }, |
| 39 | + () => undefined, |
| 40 | + ); |
| 41 | + |
| 42 | +const lines = (op?: string) => |
| 43 | + warns.filter((w) => w.msg.startsWith(op === undefined ? '[tenant-audit] ' : `[tenant-audit] ${op} on tenant-scoped object "account"`)); |
| 44 | + |
| 45 | +beforeEach(async () => { |
| 46 | + process.env.OS_TENANCY_POSTURE = 'isolated'; |
| 47 | + driver = new SqlDriver({ |
| 48 | + client: 'better-sqlite3', |
| 49 | + connection: { filename: ':memory:' }, |
| 50 | + useNullAsDefault: true, |
| 51 | + }); |
| 52 | + warns = []; |
| 53 | + (driver as any).logger = { warn: (msg: string, meta: any) => warns.push({ msg, meta }) }; |
| 54 | + await driver.initObjects([ |
| 55 | + { name: 'account', fields: { organization_id: { type: 'string' }, name: { type: 'string' } } }, |
| 56 | + ]); |
| 57 | +}); |
| 58 | +afterEach(async () => { |
| 59 | + await driver.disconnect(); |
| 60 | + if (OLD_POSTURE === undefined) delete process.env.OS_TENANCY_POSTURE; |
| 61 | + else process.env.OS_TENANCY_POSTURE = OLD_POSTURE; |
| 62 | +}); |
| 63 | + |
| 64 | +describe('[ADR-0131 8c-1] an insert whose row carries its organization is not audited', () => { |
| 65 | + it('create: the row names its organization, no tenantId — no line', async () => { |
| 66 | + await driver.create('account', { id: 'a1', organization_id: 'org_a', name: 'A1' }); |
| 67 | + expect(lines()).toEqual([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('bulkCreate: every row names its organization — no line, across organizations too', async () => { |
| 71 | + await driver.bulkCreate('account', [ |
| 72 | + { id: 'a1', organization_id: 'org_a', name: 'A1' }, |
| 73 | + { id: 'b1', organization_id: 'org_b', name: 'B1' }, |
| 74 | + ]); |
| 75 | + expect(lines()).toEqual([]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('create: a row WITHOUT its organization still warns (control)', async () => { |
| 79 | + await refused(driver.create('account', { id: 'a1', name: 'A1' })); |
| 80 | + expect(lines('create')).toHaveLength(1); |
| 81 | + expect(lines('create')[0].meta).toMatchObject({ object: 'account', op: 'create', tenantField: 'organization_id' }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('create: an empty organization is no organization — it still warns', async () => { |
| 85 | + await driver.create('account', { id: 'a1', organization_id: '', name: 'A1' }); |
| 86 | + expect(lines('create')).toHaveLength(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('create: a null organization is no organization — it still warns', async () => { |
| 90 | + await refused(driver.create('account', { id: 'a1', organization_id: null, name: 'A1' })); |
| 91 | + expect(lines('create')).toHaveLength(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('bulkCreate: ONE row without its organization keeps the line', async () => { |
| 95 | + await refused(driver.bulkCreate('account', [ |
| 96 | + { id: 'a1', organization_id: 'org_a', name: 'A1' }, |
| 97 | + { id: 'a2', name: 'A2' }, |
| 98 | + ])); |
| 99 | + expect(lines('bulkCreate')).toHaveLength(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it('the prefix is byte-identical', async () => { |
| 103 | + await refused(driver.create('account', { id: 'a1', name: 'A1' })); |
| 104 | + expect(lines()[0].msg.startsWith( |
| 105 | + '[tenant-audit] create on tenant-scoped object "account" without options.tenantId — ', |
| 106 | + )).toBe(true); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('[ADR-0131 8c-1] what must not move: non-insert writes with no tenantId still warn', () => { |
| 111 | + beforeEach(async () => { |
| 112 | + await driver.create('account', { id: 'a1', organization_id: 'org_a', name: 'A1' }, { tenantId: 'org_a' }); |
| 113 | + warns = []; |
| 114 | + }); |
| 115 | + |
| 116 | + it('update, even when its payload names an organization', async () => { |
| 117 | + await driver.update('account', 'a1', { organization_id: 'org_a', name: 'renamed' }); |
| 118 | + expect(lines('update')).toHaveLength(1); |
| 119 | + }); |
| 120 | + |
| 121 | + it('delete', async () => { |
| 122 | + await driver.delete('account', 'a1'); |
| 123 | + expect(lines('delete')).toHaveLength(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('updateMany', async () => { |
| 127 | + await driver.updateMany('account', { where: { name: 'A1' } } as any, { organization_id: 'org_a', name: 'x' }); |
| 128 | + expect(lines('updateMany')).toHaveLength(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it('deleteMany', async () => { |
| 132 | + await driver.deleteMany('account', { where: { name: 'A1' } } as any); |
| 133 | + expect(lines('deleteMany')).toHaveLength(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('upsert, even when its row names an organization — the merge leg is an update', async () => { |
| 137 | + await driver.upsert('account', { id: 'a1', organization_id: 'org_a', name: 'merged' }); |
| 138 | + expect(lines('upsert')).toHaveLength(1); |
| 139 | + }); |
| 140 | +}); |
0 commit comments