Skip to content

Commit 3039d2c

Browse files
test(policy): guard createMongoAbility builder + clarify v7 migration note
Review (Copilot): the policy unit-test mock did not export createMongoAbility nor assert the builder is seeded with it, so the #3693 auth regression could silently return. Mock createMongoAbility + assert new AbilityBuilder(createMongoAbility). Also corrects the MIGRATIONS wording: v7 renames PureAbility→Ability (the class isn't removed); only its default conditions matcher is dropped.
1 parent ce245e6 commit 3039d2c

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

MIGRATIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Breaking changes and upgrade notes for downstream projects.
1010

1111
### What changed (this repo)
1212

13-
- **`lib/middlewares/policy.js`** — v7 renames `PureAbility` to `Ability` and **drops the default conditions matcher** from it; the historical MongoDB-matching `Ability` class no longer exists. `defineAbilityFor()` now builds via `createMongoAbility`:
13+
- **`lib/middlewares/policy.js`** — v7 renames `PureAbility` to `Ability` and **drops its default conditions matcher**, so the `Ability` export no longer does MongoDB-style condition matching out of the box (`createMongoAbility` is the replacement for the old behavior). `defineAbilityFor()` now builds via `createMongoAbility`:
1414
```js
1515
// before (v6)
1616
const { AbilityBuilder, Ability } = await import('@casl/ability');

lib/middlewares/tests/policy.unit.tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jest.unstable_mockModule('@casl/ability', () => ({
2424
build: jest.fn().mockReturnValue({ can: jest.fn().mockReturnValue(true) }),
2525
})),
2626
Ability: jest.fn(),
27+
createMongoAbility: jest.fn(),
2728
subject: jest.fn((type, doc) => doc),
2829
}));
2930

@@ -69,4 +70,12 @@ describe('policy discoverPolicies unit tests:', () => {
6970
expect.stringContaining('exports abilities/guestAbilities but no SubjectRegistration'),
7071
);
7172
});
73+
74+
test('defineAbilityFor builds via createMongoAbility (v7 — guards the #3693 auth regression)', async () => {
75+
// v7 drops the conditions matcher from the `Ability` export; the builder must be
76+
// seeded with createMongoAbility or Mongo-style conditions ({ _id }, ...) stop matching.
77+
const { AbilityBuilder, createMongoAbility } = await import('@casl/ability');
78+
await policy.defineAbilityFor({ _id: 'u1', roles: ['user'] }, null);
79+
expect(AbilityBuilder).toHaveBeenCalledWith(createMongoAbility);
80+
});
7281
});

0 commit comments

Comments
 (0)