|
| 1 | +/** |
| 2 | + * Unit tests for organizationSubjectRegistration policy guard. |
| 3 | + */ |
| 4 | +import { describe, test, expect, jest, beforeEach } from '@jest/globals'; |
| 5 | + |
| 6 | +import { organizationSubjectRegistration } from '../policies/organizations.policy.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Build a mock subject-registration registry that captures registered resolvers. |
| 10 | + * @returns {{ registerDocumentSubject: jest.Mock, registerPathSubject: jest.Mock, _documentResolvers: Map }} |
| 11 | + */ |
| 12 | +function mockRegistry() { |
| 13 | + const documentResolvers = new Map(); |
| 14 | + return { |
| 15 | + registerDocumentSubject: jest.fn((prop, type, guard) => { |
| 16 | + documentResolvers.set(prop, { type, guard }); |
| 17 | + }), |
| 18 | + registerPathSubject: jest.fn(), |
| 19 | + _documentResolvers: documentResolvers, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe('organizationSubjectRegistration policy unit tests:', () => { |
| 24 | + test('should register membershipDoc, organization, and path subjects', () => { |
| 25 | + const registry = mockRegistry(); |
| 26 | + organizationSubjectRegistration(registry); |
| 27 | + |
| 28 | + expect(registry.registerDocumentSubject).toHaveBeenCalledWith('membershipDoc', 'Membership'); |
| 29 | + expect(registry.registerDocumentSubject).toHaveBeenCalledWith('organization', 'Organization', expect.any(Function)); |
| 30 | + expect(registry.registerPathSubject).toHaveBeenCalledTimes(4); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('organization document subject guard:', () => { |
| 34 | + let guard; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + const registry = mockRegistry(); |
| 38 | + organizationSubjectRegistration(registry); |
| 39 | + guard = registry._documentResolvers.get('organization').guard; |
| 40 | + }); |
| 41 | + |
| 42 | + test('should return false when req.route is undefined', () => { |
| 43 | + expect(guard({ route: undefined })).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + test('should return false when req.route.path is undefined', () => { |
| 47 | + expect(guard({ route: {} })).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test('should return false when req.route.path is an empty string', () => { |
| 51 | + expect(guard({ route: { path: '' } })).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should return true for /api/organizations paths', () => { |
| 55 | + expect(guard({ route: { path: '/api/organizations' } })).toBe(true); |
| 56 | + expect(guard({ route: { path: '/api/organizations/:id' } })).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should return true for /api/admin/organizations paths', () => { |
| 60 | + expect(guard({ route: { path: '/api/admin/organizations' } })).toBe(true); |
| 61 | + expect(guard({ route: { path: '/api/admin/organizations/:id' } })).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should return false for unrelated paths (e.g. billing routes)', () => { |
| 65 | + expect(guard({ route: { path: '/api/billing/plans' } })).toBe(false); |
| 66 | + expect(guard({ route: { path: '/api/tasks' } })).toBe(false); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments