|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import type { ISecurityService } from './security-service'; |
| 5 | + |
| 6 | +/** |
| 7 | + * These tests pin the two things a consumer of the `security` service reasons |
| 8 | + * about and that a refactor could silently change: the SHAPE of the surface |
| 9 | + * (compile-time) and the MEANING of each "empty" answer (runtime). The second |
| 10 | + * matters more than it looks — `undefined` and `[]` from getReadableFields are |
| 11 | + * opposite instructions, and a consumer that conflates them either leaks a |
| 12 | + * column set or blanks one out. |
| 13 | + */ |
| 14 | + |
| 15 | +/** Minimal stub implementing the full surface. */ |
| 16 | +function makeService(overrides: Partial<ISecurityService> = {}): ISecurityService { |
| 17 | + return { |
| 18 | + getReadFilter: async () => undefined, |
| 19 | + getReadableFields: async () => [], |
| 20 | + resolvePermissionSetNames: async () => [], |
| 21 | + explain: async () => ({}) as any, |
| 22 | + listAudienceBindingSuggestions: async () => ({ |
| 23 | + suggestions: [], |
| 24 | + synced: { created: 0, confirmedObserved: 0, pruned: 0 }, |
| 25 | + }), |
| 26 | + confirmAudienceBindingSuggestion: async () => ({ suggestion: {}, bindingCreated: true }), |
| 27 | + dismissAudienceBindingSuggestion: async () => ({ suggestion: {} }), |
| 28 | + ...overrides, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('Security Service Contract', () => { |
| 33 | + it('a full implementation satisfies the surface', () => { |
| 34 | + const service = makeService(); |
| 35 | + for (const m of [ |
| 36 | + 'getReadFilter', |
| 37 | + 'getReadableFields', |
| 38 | + 'resolvePermissionSetNames', |
| 39 | + 'explain', |
| 40 | + 'listAudienceBindingSuggestions', |
| 41 | + 'confirmAudienceBindingSuggestion', |
| 42 | + 'dismissAudienceBindingSuggestion', |
| 43 | + ] as const) { |
| 44 | + expect(typeof service[m]).toBe('function'); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('getReadFilter: undefined means NO row restriction — the only thing it may mean', async () => { |
| 49 | + // A deny is expressed as a filter that matches nothing, never as `undefined`, |
| 50 | + // so a consumer can safely read `undefined` as "apply no filter". |
| 51 | + const open = makeService({ getReadFilter: async () => undefined }); |
| 52 | + await expect(open.getReadFilter('deal', { userId: 'u1' })).resolves.toBeUndefined(); |
| 53 | + |
| 54 | + const denied = makeService({ getReadFilter: async () => ({ id: { $eq: null } }) as any }); |
| 55 | + await expect(denied.getReadFilter('deal', { userId: 'u1' })).resolves.toBeDefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('getReadableFields: undefined (no answer) and [] (nothing readable) are opposite answers', async () => { |
| 59 | + const noAnswer = makeService({ getReadableFields: async () => undefined }); |
| 60 | + // `undefined` → the caller must fall back to its own projection… |
| 61 | + await expect(noAnswer.getReadableFields('deal', { userId: 'u1' })).resolves.toBeUndefined(); |
| 62 | + |
| 63 | + const nothingReadable = makeService({ getReadableFields: async () => [] }); |
| 64 | + // …whereas `[]` is authoritative: expose no columns at all. |
| 65 | + await expect(nothingReadable.getReadableFields('deal', { userId: 'u1' })).resolves.toEqual([]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('a system context is a full field-level bypass', async () => { |
| 69 | + const service = makeService({ |
| 70 | + getReadableFields: async (_object, context) => |
| 71 | + context?.isSystem ? ['id', 'name', 'secret'] : ['id', 'name'], |
| 72 | + }); |
| 73 | + await expect(service.getReadableFields('deal', { isSystem: true })) |
| 74 | + .resolves.toEqual(['id', 'name', 'secret']); |
| 75 | + await expect(service.getReadableFields('deal', { userId: 'u1' })) |
| 76 | + .resolves.toEqual(['id', 'name']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('a partial implementation is feature-detectable rather than wrong', () => { |
| 80 | + // Consumers probe (`typeof svc.getReadableFields === 'function'`) so an |
| 81 | + // implementation may omit a method it cannot honour and still be usable. |
| 82 | + const partial: Partial<ISecurityService> = { getReadFilter: async () => undefined }; |
| 83 | + expect(typeof partial.getReadFilter).toBe('function'); |
| 84 | + expect(typeof partial.getReadableFields).toBe('undefined'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('explain accepts a record-scoped request and an explicit target user', async () => { |
| 88 | + const seen: unknown[] = []; |
| 89 | + const service = makeService({ |
| 90 | + explain: async (request) => { seen.push(request); return {} as any; }, |
| 91 | + }); |
| 92 | + await service.explain( |
| 93 | + { object: 'deal', operation: 'update', userId: 'u2', recordId: 'r1' }, |
| 94 | + { userId: 'admin' }, |
| 95 | + ); |
| 96 | + expect(seen[0]).toEqual({ object: 'deal', operation: 'update', userId: 'u2', recordId: 'r1' }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments