|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { jest, describe, test, expect, beforeEach } from '@jest/globals'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import path from 'path'; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +// Mock logger before importing policy |
| 11 | +jest.unstable_mockModule('../../services/logger.js', () => ({ |
| 12 | + default: { |
| 13 | + warn: jest.fn(), |
| 14 | + info: jest.fn(), |
| 15 | + error: jest.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock @casl/ability |
| 20 | +jest.unstable_mockModule('@casl/ability', () => ({ |
| 21 | + AbilityBuilder: jest.fn().mockImplementation(() => ({ |
| 22 | + can: jest.fn(), |
| 23 | + cannot: jest.fn(), |
| 24 | + build: jest.fn().mockReturnValue({ can: jest.fn().mockReturnValue(true) }), |
| 25 | + })), |
| 26 | + Ability: jest.fn(), |
| 27 | + subject: jest.fn((type, doc) => doc), |
| 28 | +})); |
| 29 | + |
| 30 | +const { default: logger } = await import('../../services/logger.js'); |
| 31 | +const { default: policy } = await import('../policy.js'); |
| 32 | + |
| 33 | +/** |
| 34 | + * Build absolute path to a test fixture file. |
| 35 | + * @param {string} name - Fixture filename |
| 36 | + * @returns {string} Absolute path to the fixture |
| 37 | + */ |
| 38 | +const fixture = (name) => path.join(__dirname, 'fixtures', name); |
| 39 | + |
| 40 | +describe('policy discoverPolicies unit tests:', () => { |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + test('should warn when a policy file exports abilities/guestAbilities but no SubjectRegistration', async () => { |
| 46 | + await policy.discoverPolicies([fixture('policy-abilities-only.js')]); |
| 47 | + |
| 48 | + expect(logger.warn).toHaveBeenCalledWith( |
| 49 | + expect.stringContaining('exports abilities/guestAbilities but no SubjectRegistration'), |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should not warn when a policy file exports both abilities and SubjectRegistration', async () => { |
| 54 | + await policy.discoverPolicies([fixture('policy-with-registration.js')]); |
| 55 | + |
| 56 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should not warn when a policy file exports only SubjectRegistration without abilities', async () => { |
| 60 | + await policy.discoverPolicies([fixture('policy-registration-only.js')]); |
| 61 | + |
| 62 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should warn when a policy file exports guestAbilities but no SubjectRegistration', async () => { |
| 66 | + await policy.discoverPolicies([fixture('policy-guest-abilities-only.js')]); |
| 67 | + |
| 68 | + expect(logger.warn).toHaveBeenCalledWith( |
| 69 | + expect.stringContaining('exports abilities/guestAbilities but no SubjectRegistration'), |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments