-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpermission.test.ts
More file actions
535 lines (480 loc) · 15.1 KB
/
Copy pathpermission.test.ts
File metadata and controls
535 lines (480 loc) · 15.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import { describe, it, expect } from 'vitest';
import {
PermissionSetSchema,
ObjectPermissionSchema,
EffectiveObjectPermissionSchema,
FieldPermissionSchema,
AdminScopeSchema,
type PermissionSet,
type ObjectPermission,
type FieldPermission,
} from './permission.zod';
describe('AdminScopeSchema (ADR-0090 D12)', () => {
it('parses a delegated-admin scope with defaults', () => {
const scope = AdminScopeSchema.parse({ businessUnit: 'east' });
expect(scope.businessUnit).toBe('east');
expect(scope.includeSubtree).toBe(true); // default: whole subtree
expect(scope.manageAssignments).toBe(false);
expect(scope.manageBindings).toBe(false);
expect(scope.authorEnvironmentSets).toBe(false);
expect(scope.assignablePermissionSets).toEqual([]);
});
it('rides on a permission set via adminScope', () => {
const ps = PermissionSetSchema.parse({
name: 'east_subsidiary_admin',
objects: {
sys_user_position: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true },
},
adminScope: {
businessUnit: 'east',
manageAssignments: true,
assignablePermissionSets: ['sales_user', 'support_user'],
},
});
expect(ps.adminScope?.businessUnit).toBe('east');
expect(ps.adminScope?.manageAssignments).toBe(true);
expect(ps.adminScope?.assignablePermissionSets).toEqual(['sales_user', 'support_user']);
});
it('requires the businessUnit boundary', () => {
expect(() => AdminScopeSchema.parse({ manageAssignments: true })).toThrow();
});
});
describe('ObjectPermissionSchema', () => {
it('should apply default values to false', () => {
const result = ObjectPermissionSchema.parse({});
expect(result.allowCreate).toBe(false);
expect(result.allowRead).toBe(false);
expect(result.allowEdit).toBe(false);
expect(result.allowDelete).toBe(false);
expect(result.viewAllRecords).toBe(false);
expect(result.modifyAllRecords).toBe(false);
});
it('allowExport is optional with no default — unset stays undefined (#3544)', () => {
// Deliberately NOT defaulted: unset = inherit read (backward-compatible
// opt-out), so adding the key changes nothing for existing permission sets.
const result = ObjectPermissionSchema.parse({});
expect(result.allowExport).toBeUndefined();
expect(ObjectPermissionSchema.parse({ allowExport: false }).allowExport).toBe(false);
expect(ObjectPermissionSchema.parse({ allowExport: true }).allowExport).toBe(true);
});
it('should accept CRUD permissions', () => {
const permission: ObjectPermission = {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: true,
};
expect(() => ObjectPermissionSchema.parse(permission)).not.toThrow();
});
it('should accept view all permissions', () => {
const permission: ObjectPermission = {
allowRead: true,
viewAllRecords: true,
};
expect(() => ObjectPermissionSchema.parse(permission)).not.toThrow();
});
it('should accept modify all permissions', () => {
const permission: ObjectPermission = {
allowEdit: true,
allowDelete: true,
modifyAllRecords: true,
};
expect(() => ObjectPermissionSchema.parse(permission)).not.toThrow();
});
it('should accept read-only permissions', () => {
const permission: ObjectPermission = {
allowRead: true,
allowCreate: false,
allowEdit: false,
allowDelete: false,
};
const result = ObjectPermissionSchema.parse(permission);
expect(result.allowRead).toBe(true);
expect(result.allowCreate).toBe(false);
});
});
describe('EffectiveObjectPermissionSchema (#3391 response-side)', () => {
it('carries every ObjectPermission field plus optional apiOperations', () => {
const parsed = EffectiveObjectPermissionSchema.parse({
allowRead: true,
allowCreate: true,
apiOperations: ['get', 'list', 'create', 'update', 'delete', 'bulk'],
});
expect(parsed.allowRead).toBe(true);
expect(parsed.apiOperations).toEqual(['get', 'list', 'create', 'update', 'delete', 'bulk']);
});
it('apiOperations is optional (absent = client default-allow)', () => {
const parsed = EffectiveObjectPermissionSchema.parse({ allowRead: true });
expect(parsed.apiOperations).toBeUndefined();
expect(parsed.allowRead).toBe(true);
});
it('rejects an apiOperations value outside the ApiMethod enum', () => {
expect(
EffectiveObjectPermissionSchema.safeParse({ allowRead: true, apiOperations: ['frobnicate'] }).success,
).toBe(false);
});
it('accepts the derived legacy operations (import/export/aggregate/…)', () => {
expect(
EffectiveObjectPermissionSchema.safeParse({
allowRead: true,
apiOperations: ['get', 'list', 'aggregate', 'search', 'export', 'import', 'upsert'],
}).success,
).toBe(true);
});
it('does not leak apiOperations onto the authoring ObjectPermissionSchema', () => {
// The authoring schema stays unextended — a stray apiOperations key is
// stripped (or rejected) there, never a valid authoring field.
const parsed = ObjectPermissionSchema.parse({ allowRead: true, apiOperations: ['get'] } as any);
expect((parsed as any).apiOperations).toBeUndefined();
});
});
describe('FieldPermissionSchema', () => {
it('should default readable to true', () => {
const result = FieldPermissionSchema.parse({});
expect(result.readable).toBe(true);
expect(result.editable).toBe(false);
});
it('should accept read-only field permission', () => {
const permission: FieldPermission = {
readable: true,
editable: false,
};
expect(() => FieldPermissionSchema.parse(permission)).not.toThrow();
});
it('should accept editable field permission', () => {
const permission: FieldPermission = {
readable: true,
editable: true,
};
expect(() => FieldPermissionSchema.parse(permission)).not.toThrow();
});
it('should accept hidden field', () => {
const permission: FieldPermission = {
readable: false,
editable: false,
};
expect(() => FieldPermissionSchema.parse(permission)).not.toThrow();
});
});
describe('PermissionSetSchema', () => {
it('should accept minimal permission set', () => {
const permSet: PermissionSet = {
name: 'standard_user',
objects: {},
};
expect(() => PermissionSetSchema.parse(permSet)).not.toThrow();
});
it('should default isDefault to false', () => {
const permSet = {
name: 'export_reports',
objects: {},
};
const result = PermissionSetSchema.parse(permSet);
expect(result.isDefault).toBe(false);
});
it('should accept permission set with label', () => {
const permSet: PermissionSet = {
name: 'sales_user',
label: 'Sales User',
objects: {},
};
expect(() => PermissionSetSchema.parse(permSet)).not.toThrow();
});
it('should accept profile permission set', () => {
const profile: PermissionSet = {
name: 'system_admin',
label: 'System Administrator',
objects: {},
};
expect(() => PermissionSetSchema.parse(profile)).not.toThrow();
});
it('should accept permission set with object permissions', () => {
const permSet: PermissionSet = {
name: 'sales_manager',
label: 'Sales Manager',
objects: {
account: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
viewAllRecords: true,
modifyAllRecords: false,
},
opportunity: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
contact: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
},
};
expect(() => PermissionSetSchema.parse(permSet)).not.toThrow();
});
it('should accept permission set with field permissions', () => {
const permSet: PermissionSet = {
name: 'restricted_user',
objects: {
account: {
allowRead: true,
},
},
fields: {
'account.annual_revenue': {
readable: false,
editable: false,
},
'account.account_number': {
readable: true,
editable: false,
},
},
};
expect(() => PermissionSetSchema.parse(permSet)).not.toThrow();
});
it('should accept permission set with system permissions', () => {
const permSet: PermissionSet = {
name: 'admin_tools',
label: 'Admin Tools',
objects: {},
systemPermissions: [
'manage_users',
'view_setup',
'customize_application',
'modify_all_data',
],
};
expect(() => PermissionSetSchema.parse(permSet)).not.toThrow();
});
describe('Real-World Permission Set Examples', () => {
it('should accept system administrator profile', () => {
const sysAdmin: PermissionSet = {
name: 'system_administrator',
label: 'System Administrator',
objects: {
user: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
account: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
opportunity: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: true,
viewAllRecords: true,
modifyAllRecords: true,
},
},
systemPermissions: [
'manage_users',
'view_all_data',
'modify_all_data',
'customize_application',
'view_setup',
'manage_roles',
'manage_profiles',
],
};
expect(() => PermissionSetSchema.parse(sysAdmin)).not.toThrow();
});
it('should accept standard sales user profile', () => {
const salesUser: PermissionSet = {
name: 'standard_sales_user',
label: 'Standard Sales User',
objects: {
account: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
contact: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
opportunity: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
lead: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
},
fields: {
'opportunity.amount': {
readable: true,
editable: true,
},
'account.annual_revenue': {
readable: true,
editable: false,
},
},
};
expect(() => PermissionSetSchema.parse(salesUser)).not.toThrow();
});
it('should accept marketing user permission set', () => {
const marketingPermSet: PermissionSet = {
name: 'marketing_user',
label: 'Marketing User',
objects: {
campaign: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
lead: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
},
systemPermissions: [
'run_reports',
'export_reports',
'manage_campaigns',
],
};
expect(() => PermissionSetSchema.parse(marketingPermSet)).not.toThrow();
});
it('should accept read-only analyst profile', () => {
const analyst: PermissionSet = {
name: 'read_only_analyst',
label: 'Read Only Analyst',
objects: {
account: {
allowRead: true,
viewAllRecords: true,
},
opportunity: {
allowRead: true,
viewAllRecords: true,
},
contact: {
allowRead: true,
viewAllRecords: true,
},
task: {
allowRead: true,
viewAllRecords: true,
},
},
systemPermissions: [
'run_reports',
'export_reports',
],
};
expect(() => PermissionSetSchema.parse(analyst)).not.toThrow();
});
it('should accept service agent profile with restricted fields', () => {
const serviceAgent: PermissionSet = {
name: 'service_agent',
label: 'Service Agent',
objects: {
case: {
allowCreate: true,
allowRead: true,
allowEdit: true,
allowDelete: false,
},
account: {
allowRead: true,
allowEdit: false,
},
contact: {
allowRead: true,
allowEdit: false,
},
},
fields: {
'account.annual_revenue': {
readable: false,
editable: false,
},
'account.account_owner': {
readable: true,
editable: false,
},
'case.priority': {
readable: true,
editable: true,
},
},
systemPermissions: [
'view_knowledge_base',
],
};
expect(() => PermissionSetSchema.parse(serviceAgent)).not.toThrow();
});
});
});
// ============================================================================
// Protocol Improvement Tests: Permission tabPermissions
// ============================================================================
describe('PermissionSetSchema - tabPermissions', () => {
it('should accept permission set with tabPermissions', () => {
const result = PermissionSetSchema.parse({
name: 'sales_user',
objects: {
account: { allowRead: true, allowCreate: true },
},
tabPermissions: {
'app_crm': 'visible',
'app_admin': 'hidden',
'app_marketing': 'default_on',
'app_support': 'default_off',
},
});
expect(result.tabPermissions?.['app_crm']).toBe('visible');
expect(result.tabPermissions?.['app_admin']).toBe('hidden');
expect(result.tabPermissions?.['app_marketing']).toBe('default_on');
expect(result.tabPermissions?.['app_support']).toBe('default_off');
});
it('should reject invalid tab permission values', () => {
expect(() => PermissionSetSchema.parse({
name: 'bad_perm',
objects: {},
tabPermissions: {
'app_test': 'invalid_value',
},
})).toThrow();
});
it('should accept permission set without tabPermissions (optional)', () => {
const result = PermissionSetSchema.parse({
name: 'basic_user',
objects: {
task: { allowRead: true },
},
});
expect(result.tabPermissions).toBeUndefined();
});
});