Files
upstream-rocketchat/apps/meteor/tests/unit/server/api/checkPermissionsForInvocation.spec.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

127 lines
4.0 KiB
TypeScript

import { expect } from 'chai';
import { describe, it } from 'mocha';
import mock from 'proxyquire';
import Sinon from 'sinon';
import type { PermissionsPayload } from '../../../../server/api/api.helpers';
const userPermissions: { [k: string]: string[] } = {
'4r3fsadfasf': ['view-all', 'view-none'],
'4r3fsadfasf2': ['view-all', 'view-0'],
'4r3fsadfasf3': ['view-all', 'view-1'],
'4r3fsadfasf4': [],
};
const mocks = {
'../lib/authorization/hasPermission': {
hasAllPermissionAsync: (userId: string, permissions: string[]): boolean => {
return permissions.every((permission) => userPermissions[userId].includes(permission));
},
hasAtLeastOnePermissionAsync: (userId: string, permissions: string[]): boolean => {
return permissions.some((permission) => userPermissions[userId].includes(permission));
},
},
'../lib/deprecationWarningLogger': {
apiDeprecationLogger: {
endpoint: Sinon.stub(),
},
},
};
const { checkPermissionsForInvocation } = mock.noCallThru().load('../../../../server/api/api.helpers', mocks);
describe('checkPermissionsForInvocation', () => {
it('should return false when no permissions are provided', async () => {
const options = {
permissionsRequired: {},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'GET')).to.be.false;
});
it('should return false when no config is provided for that specific method', async () => {
const options = {
permissionsRequired: {
GET: {
operation: 'hasAll',
permissions: ['view-all', 'view-none'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'POST')).to.be.false;
});
it('should return true path is configured with empty permissions array', async () => {
const options = {
permissionsRequired: {
GET: { permissions: [], operation: 'hasAll' },
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'GET')).to.be.true;
});
it('should return true when user has all permissions', async () => {
const options: { permissionsRequired: PermissionsPayload } = {
permissionsRequired: {
GET: {
operation: 'hasAll',
permissions: ['view-all', 'view-none'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'GET')).to.be.true;
});
it('should read permissions config from * when request method provided doesnt have config', async () => {
const options: { permissionsRequired: PermissionsPayload } = {
permissionsRequired: {
'GET': {
operation: 'hasAll',
permissions: ['view-all', 'view-none'],
},
'*': {
operation: 'hasAll',
permissions: ['view-all', 'view-none'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'PUT')).to.be.true;
});
it('should return false when user has no permissions', async () => {
const options: { permissionsRequired: PermissionsPayload } = {
permissionsRequired: {
GET: {
operation: 'hasAll',
permissions: ['view-all', 'view-none'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf4', options.permissionsRequired, 'GET')).to.be.false;
});
it('should return false when operation is invalid', async () => {
const options: { permissionsRequired: PermissionsPayload } = {
permissionsRequired: {
GET: {
// @ts-expect-error - for testing purposes
operation: 'invalid',
permissions: ['view-all', 'view-none'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'GET')).to.be.false;
});
it('should return true when operation is hasAny and user has at least one listed permission', async () => {
const options: { permissionsRequired: PermissionsPayload } = {
permissionsRequired: {
GET: {
operation: 'hasAny',
permissions: ['view-all', 'admin'],
},
},
};
expect(await checkPermissionsForInvocation('4r3fsadfasf', options.permissionsRequired, 'GET')).to.be.true;
});
});