-
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(policy): startup warn for missing SubjectRegistration + mailer test coverage #3397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6cb5fed
fix(policy): warn at startup if module has abilities without SubjectR…
PierreBrisorgueil c9ba2e9
test(mailer): add Buffer attachment and empty filename coverage (#3396)
PierreBrisorgueil 324a423
fix(jest): exclude tests/fixtures/ dirs from testPathIgnorePatterns
PierreBrisorgueil e9fe66e
fix(jest): exclude tests/fixtures from coverage collection
PierreBrisorgueil 3ac4b6d
fix(policy): improve warn message wording, add JSDoc to fixtures, pre…
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Fixture: policy file with abilities but no SubjectRegistration | ||
| /** | ||
| * Define task abilities for authenticated users (fixture without SubjectRegistration). | ||
| * @param {Object} _user - The authenticated user (unused in fixture) | ||
| * @param {Object} _membership - Optional organization membership (unused in fixture) | ||
| * @param {Object} context - CASL ability builder context | ||
| * @param {Function} context.can - Function to grant abilities | ||
| * @returns {void} | ||
| */ | ||
| export function taskAbilities(_user, _membership, { can }) { | ||
| can('read', 'Task'); | ||
| } |
10 changes: 10 additions & 0 deletions
10
lib/middlewares/tests/fixtures/policy-guest-abilities-only.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Fixture: policy file with guestAbilities but no SubjectRegistration | ||
| /** | ||
| * Define task abilities for guest users (fixture without SubjectRegistration). | ||
| * @param {Object} context - CASL ability builder context | ||
| * @param {Function} context.can - Function to grant abilities | ||
| * @returns {void} | ||
| */ | ||
| export function taskGuestAbilities({ can }) { | ||
| can('read', 'Task'); | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
lib/middlewares/tests/fixtures/policy-registration-only.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Fixture: policy file with SubjectRegistration only (no abilities) | ||
| /** | ||
| * Register task document subject for CASL resolution. | ||
| * @param {Object} context - Registration context | ||
| * @param {Function} context.registerDocumentSubject - Function to register document subjects | ||
| * @returns {void} | ||
| */ | ||
| export function taskSubjectRegistration({ registerDocumentSubject }) { | ||
| registerDocumentSubject('task', 'Task'); | ||
| } | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
22 changes: 22 additions & 0 deletions
22
lib/middlewares/tests/fixtures/policy-with-registration.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Fixture: policy file with abilities AND SubjectRegistration | ||
| /** | ||
| * Define task abilities for authenticated users. | ||
| * @param {Object} _user - The authenticated user (unused in fixture) | ||
| * @param {Object} _membership - Optional organization membership (unused in fixture) | ||
| * @param {Object} context - CASL ability builder context | ||
| * @param {Function} context.can - Function to grant abilities | ||
| * @returns {void} | ||
| */ | ||
| export function taskAbilities(_user, _membership, { can }) { | ||
| can('read', 'Task'); | ||
| } | ||
|
|
||
| /** | ||
| * Register task document subject for CASL resolution. | ||
| * @param {Object} context - Registration context | ||
| * @param {Function} context.registerDocumentSubject - Function to register document subjects | ||
| * @returns {void} | ||
| */ | ||
| export function taskSubjectRegistration({ registerDocumentSubject }) { | ||
| registerDocumentSubject('task', 'Task'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, describe, test, expect, beforeEach } from '@jest/globals'; | ||
| import { fileURLToPath } from 'url'; | ||
| import path from 'path'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| // Mock logger before importing policy | ||
| jest.unstable_mockModule('../../services/logger.js', () => ({ | ||
| default: { | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| error: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| // Mock @casl/ability | ||
| jest.unstable_mockModule('@casl/ability', () => ({ | ||
| AbilityBuilder: jest.fn().mockImplementation(() => ({ | ||
| can: jest.fn(), | ||
| cannot: jest.fn(), | ||
| build: jest.fn().mockReturnValue({ can: jest.fn().mockReturnValue(true) }), | ||
| })), | ||
| Ability: jest.fn(), | ||
| subject: jest.fn((type, doc) => doc), | ||
| })); | ||
|
|
||
| const { default: logger } = await import('../../services/logger.js'); | ||
| const { default: policy } = await import('../policy.js'); | ||
|
|
||
| /** | ||
| * Build absolute path to a test fixture file. | ||
| * @param {string} name - Fixture filename | ||
| * @returns {string} Absolute path to the fixture | ||
| */ | ||
| const fixture = (name) => path.join(__dirname, 'fixtures', name); | ||
PierreBrisorgueil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('policy discoverPolicies unit tests:', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should warn when a policy file exports abilities/guestAbilities but no SubjectRegistration', async () => { | ||
| await policy.discoverPolicies([fixture('policy-abilities-only.js')]); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('exports abilities/guestAbilities but no SubjectRegistration'), | ||
| ); | ||
| }); | ||
|
|
||
| test('should not warn when a policy file exports both abilities and SubjectRegistration', async () => { | ||
| await policy.discoverPolicies([fixture('policy-with-registration.js')]); | ||
|
|
||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should not warn when a policy file exports only SubjectRegistration without abilities', async () => { | ||
| await policy.discoverPolicies([fixture('policy-registration-only.js')]); | ||
|
|
||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should warn when a policy file exports guestAbilities but no SubjectRegistration', async () => { | ||
| await policy.discoverPolicies([fixture('policy-guest-abilities-only.js')]); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('exports abilities/guestAbilities but no SubjectRegistration'), | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.