🧪 test: rename misleading public-dedup test and add real dedup coverage

The test previously named "returns deduplicated IDs even if the public
principal has multiple entries" only set up a single ACL entry, so it
did not actually exercise deduplication. Split into two tests: one for
the happy path (single entry with required bits), and one that bypasses
`grantPermission`'s upsert via `AclEntry.create` to confirm the
application-layer dedup Map handles genuine duplicates.

Refs #12729.
This commit is contained in:
Danny Avila 2026-04-18 10:51:35 -04:00
parent 858f0381e5
commit a1d4dc7226

View file

@ -1378,7 +1378,7 @@ describe('AclEntry Model Tests', () => {
});
describe('findPublicResourceIds', () => {
test('returns deduplicated IDs even if the public principal has multiple entries', async () => {
test('returns the public resource ID when required bits are present', async () => {
const shared = new mongoose.Types.ObjectId();
await methods.grantPermission(
PrincipalType.PUBLIC,
@ -1396,6 +1396,35 @@ describe('AclEntry Model Tests', () => {
expect(result).toHaveLength(1);
expect(result[0].toString()).toBe(shared.toString());
});
test('deduplicates when duplicate public entries exist for the same resource', async () => {
/**
* `grantPermission` upserts, so duplicates are not reachable through the
* public API. Bypass it with `AclEntry.create` to confirm the
* application-layer dedup logic handles the defensive case.
*/
const shared = new mongoose.Types.ObjectId();
await AclEntry.create([
{
principalType: PrincipalType.PUBLIC,
resourceType: ResourceType.AGENT,
resourceId: shared,
permBits: PermissionBits.VIEW,
grantedBy: grantedById,
},
{
principalType: PrincipalType.PUBLIC,
resourceType: ResourceType.AGENT,
resourceId: shared,
permBits: PermissionBits.VIEW | PermissionBits.EDIT,
grantedBy: grantedById,
},
]);
const result = await methods.findPublicResourceIds(ResourceType.AGENT, PermissionBits.VIEW);
expect(result).toHaveLength(1);
expect(result[0].toString()).toBe(shared.toString());
});
});
describe('getSoleOwnedResourceIds', () => {