-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsharing-service.test.ts
More file actions
415 lines (367 loc) · 15.3 KB
/
Copy pathsharing-service.test.ts
File metadata and controls
415 lines (367 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach } from 'vitest';
import { SharingService } from './sharing-service.js';
import { buildSharingMiddleware } from './sharing-plugin.js';
// ─────────────────────────────────────────────────────────────────────
// In-memory fake engine
// ─────────────────────────────────────────────────────────────────────
interface FakeRow {
[k: string]: any;
}
function makeFakeEngine(schemas: Record<string, any>) {
const tables: Record<string, FakeRow[]> = {};
const ensure = (name: string) => (tables[name] ??= []);
function matches(row: FakeRow, filter: any): boolean {
if (!filter || typeof filter !== 'object') return true;
if (filter.$or && Array.isArray(filter.$or)) {
return filter.$or.some((f: any) => matches(row, f));
}
if (filter.$and && Array.isArray(filter.$and)) {
return filter.$and.every((f: any) => matches(row, f));
}
for (const [k, v] of Object.entries(filter)) {
if (k === '$or' || k === '$and') continue;
const rv = row[k];
if (v != null && typeof v === 'object' && '$in' in (v as any)) {
if (!(v as any).$in.includes(rv)) return false;
continue;
}
if (rv !== v) return false;
}
return true;
}
return {
_tables: tables,
getSchema(name: string) { return schemas[name]; },
async find(object: string, options?: any) {
const table = ensure(object);
const filter = options?.filter ?? options?.where;
let out = table.filter(r => matches(r, filter));
if (options?.orderBy?.[0]) {
// Canonical SortNode key only (spec/data/query.zod.ts): the real
// engine strips an unknown `direction:` key and defaults to asc, so
// the mock must too — honoring both keys masks wrong-key sorts.
const { field, order } = options.orderBy[0];
out = [...out].sort((a, b) => {
const av = a[field]; const bv = b[field];
if (av === bv) return 0;
const cmp = av > bv ? 1 : -1;
return order === 'desc' ? -cmp : cmp;
});
}
return out.slice(0, options?.limit ?? 1000);
},
async insert(object: string, data: any) {
const row = { ...data };
ensure(object).push(row);
return row;
},
async update(object: string, idOrData: any, dataOrOptions?: any) {
// Engine signature is overloaded — handle the (data, options)
// shape used by SharingService.grant() where id lives on data.
const data = typeof idOrData === 'object' ? idOrData : dataOrOptions;
const id = typeof idOrData === 'object' ? idOrData.id : idOrData;
const table = ensure(object);
const i = table.findIndex(r => r.id === id);
if (i >= 0) table[i] = { ...table[i], ...data };
return table[i];
},
async delete(object: string, options?: any) {
const table = ensure(object);
const id = options?.where?.id ?? options?.id;
const i = table.findIndex(r => r.id === id);
if (i >= 0) table.splice(i, 1);
return { id };
},
};
}
const ACCOUNT_SCHEMA = {
name: 'account',
sharingModel: 'private',
fields: { id: {}, name: {}, owner_id: {} },
};
const LEAD_SCHEMA = {
name: 'lead',
sharingModel: 'read',
fields: { id: {}, name: {}, owner_id: {} },
};
const PUBLIC_SCHEMA = {
name: 'task',
// no sharingModel — treated as public
fields: { id: {}, name: {}, owner_id: {} },
};
// ADR-0056 D1 — canonical OWD vocabulary maps onto the same enforced behaviours.
const CANON_PUBLIC_READ_SCHEMA = {
name: 'kbarticle',
sharingModel: 'public_read', // canonical alias of legacy `read`
fields: { id: {}, name: {}, owner_id: {} },
};
const CANON_PUBLIC_RW_SCHEMA = {
name: 'whiteboard',
sharingModel: 'public_read_write', // canonical alias of "public" (no record filter)
fields: { id: {}, name: {}, owner_id: {} },
};
const ORPHAN_SCHEMA = {
name: 'note',
sharingModel: 'private',
// no owner_id — sharing skipped
fields: { id: {}, body: {} },
};
// ─────────────────────────────────────────────────────────────────────
describe('SharingService.buildReadFilter', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: SharingService;
beforeEach(() => {
engine = makeFakeEngine({
account: ACCOUNT_SCHEMA,
lead: LEAD_SCHEMA,
task: PUBLIC_SCHEMA,
note: ORPHAN_SCHEMA,
kbarticle: CANON_PUBLIC_READ_SCHEMA,
whiteboard: CANON_PUBLIC_RW_SCHEMA,
sys_record_share: { name: 'sys_record_share' },
});
svc = new SharingService({ engine });
});
it('returns null for system context', async () => {
expect(await svc.buildReadFilter('account', { isSystem: true })).toBeNull();
});
it('returns null for objects in the bypass list', async () => {
expect(await svc.buildReadFilter('sys_user', { userId: 'u1' })).toBeNull();
});
it('returns null for public objects', async () => {
expect(await svc.buildReadFilter('task', { userId: 'u1' })).toBeNull();
});
it('returns null for read-only-sharing objects (writes are gated, reads are not)', async () => {
expect(await svc.buildReadFilter('lead', { userId: 'u1' })).toBeNull();
});
it('canonical public_read reads like `read` (everyone reads → no filter) [ADR-0056 D1]', async () => {
expect(await svc.buildReadFilter('kbarticle', { userId: 'u1' })).toBeNull();
});
it('canonical public_read_write is unscoped on read [ADR-0056 D1]', async () => {
expect(await svc.buildReadFilter('whiteboard', { userId: 'u1' })).toBeNull();
});
it('returns null for objects without owner_id even when private', async () => {
expect(await svc.buildReadFilter('note', { userId: 'u1' })).toBeNull();
});
it('returns deny-all for private object with no userId', async () => {
const f = await svc.buildReadFilter('account', {});
expect(f).toEqual({ id: '__deny_all__' });
});
it('returns owner-only filter when user has no explicit shares', async () => {
const f = await svc.buildReadFilter('account', { userId: 'alice' });
expect(f).toEqual({ owner_id: 'alice' });
});
it('returns owner OR shared-record filter when grants exist', async () => {
await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'alice' }, { userId: 'admin' });
await svc.grant({ object: 'account', recordId: 'a2', recipientId: 'alice', accessLevel: 'edit' }, { userId: 'admin' });
const f: any = await svc.buildReadFilter('account', { userId: 'alice' });
expect(f.$or).toBeDefined();
expect(f.$or[0]).toEqual({ owner_id: 'alice' });
expect(f.$or[1].id.$in.sort()).toEqual(['a1', 'a2']);
});
});
describe('SharingService.canEdit', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: SharingService;
beforeEach(() => {
engine = makeFakeEngine({
account: ACCOUNT_SCHEMA,
lead: LEAD_SCHEMA,
task: PUBLIC_SCHEMA,
kbarticle: CANON_PUBLIC_READ_SCHEMA,
whiteboard: CANON_PUBLIC_RW_SCHEMA,
sys_record_share: { name: 'sys_record_share' },
});
svc = new SharingService({ engine });
engine._tables.account = [
{ id: 'a1', name: 'Acme', owner_id: 'alice' },
{ id: 'a2', name: 'Beta', owner_id: 'bob' },
];
engine._tables.lead = [
{ id: 'l1', name: 'Lead1', owner_id: 'alice' },
];
engine._tables.kbarticle = [
{ id: 'k1', name: 'KB1', owner_id: 'alice' },
];
});
it('returns true for system context', async () => {
expect(await svc.canEdit('account', 'a1', { isSystem: true })).toBe(true);
});
it('returns true for public objects', async () => {
expect(await svc.canEdit('task', 'anything', { userId: 'bob' })).toBe(true);
});
it('returns true for record owner', async () => {
expect(await svc.canEdit('account', 'a1', { userId: 'alice' })).toBe(true);
});
it('returns false for non-owner without share', async () => {
expect(await svc.canEdit('account', 'a1', { userId: 'bob' })).toBe(false);
});
it('returns false for read-only share', async () => {
await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob', accessLevel: 'read' }, { userId: 'admin' });
expect(await svc.canEdit('account', 'a1', { userId: 'bob' })).toBe(false);
});
it('returns true for edit share', async () => {
await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob', accessLevel: 'edit' }, { userId: 'admin' });
expect(await svc.canEdit('account', 'a1', { userId: 'bob' })).toBe(true);
});
it('enforces canEdit for sharingModel=read', async () => {
expect(await svc.canEdit('lead', 'l1', { userId: 'alice' })).toBe(true);
expect(await svc.canEdit('lead', 'l1', { userId: 'bob' })).toBe(false);
});
it('canonical public_read gates writes to the owner [ADR-0056 D1]', async () => {
expect(await svc.canEdit('kbarticle', 'k1', { userId: 'alice' })).toBe(true);
expect(await svc.canEdit('kbarticle', 'k1', { userId: 'bob' })).toBe(false);
});
it('canonical public_read_write lets anyone write [ADR-0056 D1]', async () => {
expect(await svc.canEdit('whiteboard', 'anything', { userId: 'bob' })).toBe(true);
});
});
describe('SharingService.grant / listShares / revoke', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: SharingService;
beforeEach(() => {
engine = makeFakeEngine({ account: ACCOUNT_SCHEMA, sys_record_share: {} });
svc = new SharingService({ engine });
});
it('creates a new grant on first call', async () => {
const r = await svc.grant(
{ object: 'account', recordId: 'a1', recipientId: 'bob', accessLevel: 'edit' },
{ userId: 'admin' },
);
expect(r.id).toMatch(/^shr_/);
expect(r.access_level).toBe('edit');
expect(r.granted_by).toBe('admin');
expect(engine._tables.sys_record_share.length).toBe(1);
});
it('upserts on second call with same (object, record, recipient)', async () => {
const a = await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob' }, { userId: 'admin' });
const b = await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob', accessLevel: 'full' }, { userId: 'admin' });
expect(engine._tables.sys_record_share.length).toBe(1);
expect(b.id).toBe(a.id);
expect(b.access_level).toBe('full');
});
it('listShares returns all grants on a record', async () => {
await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob' }, { userId: 'admin' });
await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'carol', accessLevel: 'edit' }, { userId: 'admin' });
const rows = await svc.listShares('account', 'a1', { userId: 'admin' });
expect(rows.length).toBe(2);
});
it('listShares returns the newest grant first', async () => {
// Regression: the query sorted with the non-canonical `direction: 'desc'`
// key, which SortNode strips — so it sorted ascending (oldest first).
engine._tables.sys_record_share = [
{ id: 'shr_old', object_name: 'account', record_id: 'a1', recipient_id: 'bob', created_at: '2026-01-01T00:00:00Z' },
{ id: 'shr_new', object_name: 'account', record_id: 'a1', recipient_id: 'carol', created_at: '2026-02-01T00:00:00Z' },
];
const rows = await svc.listShares('account', 'a1', { userId: 'admin' });
expect(rows.map(r => r.id)).toEqual(['shr_new', 'shr_old']);
});
it('revoke removes the row', async () => {
const r = await svc.grant({ object: 'account', recordId: 'a1', recipientId: 'bob' }, { userId: 'admin' });
await svc.revoke(r.id, { userId: 'admin' });
expect(engine._tables.sys_record_share.length).toBe(0);
});
it('rejects grant input missing required fields', async () => {
await expect(svc.grant({} as any, {})).rejects.toThrow(/VALIDATION_FAILED/);
await expect(svc.grant({ object: 'account' } as any, {})).rejects.toThrow(/VALIDATION_FAILED/);
await expect(svc.grant({ object: 'account', recordId: 'a1' } as any, {})).rejects.toThrow(/VALIDATION_FAILED/);
});
});
describe('buildSharingMiddleware (engine integration)', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: SharingService;
beforeEach(() => {
engine = makeFakeEngine({
account: ACCOUNT_SCHEMA,
lead: LEAD_SCHEMA,
task: PUBLIC_SCHEMA,
sys_record_share: {},
});
svc = new SharingService({ engine });
engine._tables.account = [
{ id: 'a1', name: 'Acme', owner_id: 'alice' },
{ id: 'a2', name: 'Beta', owner_id: 'bob' },
];
});
it('adds visibility filter on find', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'find',
ast: {},
context: { userId: 'alice' },
};
await mw(ctx, async () => {});
expect(ctx.ast.where).toEqual({ owner_id: 'alice' });
});
it('skips read filter for system context', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'find',
ast: {},
context: { isSystem: true },
};
await mw(ctx, async () => {});
expect(ctx.ast.where).toBeUndefined();
});
it('throws FORBIDDEN on update by non-owner', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'update',
data: { id: 'a1', name: 'X' },
context: { userId: 'bob' },
};
await expect(mw(ctx, async () => {})).rejects.toMatchObject({ code: 'FORBIDDEN', status: 403 });
});
it('allows update by owner', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'update',
data: { id: 'a1', name: 'X' },
context: { userId: 'alice' },
};
let nextCalled = false;
await mw(ctx, async () => { nextCalled = true; });
expect(nextCalled).toBe(true);
});
it('allows delete after explicit edit grant', async () => {
await svc.grant({ object: 'account', recordId: 'a2', recipientId: 'alice', accessLevel: 'edit' }, { userId: 'admin' });
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'delete',
options: { where: { id: 'a2' } },
context: { userId: 'alice' },
};
let nextCalled = false;
await mw(ctx, async () => { nextCalled = true; });
expect(nextCalled).toBe(true);
});
it('does not block insert', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'insert',
data: { id: 'a3', owner_id: 'eve' },
context: { userId: 'eve' },
};
let nextCalled = false;
await mw(ctx, async () => { nextCalled = true; });
expect(nextCalled).toBe(true);
});
it('preserves caller-provided filter via $and', async () => {
const mw = buildSharingMiddleware(svc);
const ctx: any = {
object: 'account',
operation: 'find',
ast: { where: { name: 'Acme' } },
context: { userId: 'alice' },
};
await mw(ctx, async () => {});
expect(ctx.ast.where).toEqual({ $and: [{ name: 'Acme' }, { owner_id: 'alice' }] });
});
});