Skip to content

Commit

Permalink
fix(smart-actions): skip scope validation for smart action on smart c…
Browse files Browse the repository at this point in the history
…ollection (#819)
  • Loading branch information
jeffladiray authored Oct 25, 2021
1 parent b6d7319 commit 6b97e19
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/middlewares/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ class PermissionMiddlewareCreator {
_ensureRecordIdsInScope() {
return async (request, response, next) => {
try {
const { primaryKeys, isVirtual } = Schemas.schemas[this.collectionName];

// Smart collections does not have the scope feature
if (isVirtual) {
return next();
}

// if performing a `selectAll` let the `getIdsFromRequest` handle the scopes
const attributes = PermissionMiddlewareCreator._getRequestAttributes(request);
if (attributes.allRecords) {
return next();
}

// Otherwise, check that all records are within scope.
const { primaryKeys } = Schemas.schemas[this.collectionName];
const filters = JSON.stringify(primaryKeys.length === 1
? { field: primaryKeys[0], operator: 'in', value: attributes.ids }
: {
Expand Down
26 changes: 26 additions & 0 deletions test/middlewares/permissions.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,32 @@ describe('middlewares > permissions', () => {
expect(next).not.toHaveBeenCalled();
});
});

describe('with a smart collection', () => {
it('should call next() since no scope can be configured', async () => {
expect.assertions(2);

Schemas.schemas = {
users: {
name: 'users',
idField: 'id',
primaryKeys: ['id'],
isVirtual: true,
},
};

const request = buildRequest({ ...defaultAttributes, ids: ['1'] });
const next = jest.fn();

const dependencies = getDependencies();
const permissionMiddlewareCreator = createPermissionMiddlewareCreator('users', dependencies);
const middleware = permissionMiddlewareCreator._ensureRecordIdsInScope();
await middleware(request, defaultResponse, next);

expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith();
});
});
});

describe('smartAction', () => {
Expand Down

0 comments on commit 6b97e19

Please sign in to comment.