Skip to content
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

Add loosePermissions method. #1061

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ List of available permissions methods:
* `isEntitled` - test if logged in user is entitled, entitlements to check for is passed as an argument
* `isProd` - test if current environment is production (prod-beta and prod-stable)
* `isBeta` - test if current environment is beta (ci-beta, qa-beta and prod-beta)
* `hasPermissions` - test if current user has rbac role permissions ['app:scope:permission']
* `hasPermissions` - test if current user has rbac role permissions ['app:scope:permission'], uses logical AND to evaluate the permissions
* `loosePermissions` - similar to `hasPermissions`, uses logical OR to evaluate the permissions
* `apiRequest` - call custom API endpoint to test if the item should be displayed.

* Expects `true`/`false` response.
Expand Down
17 changes: 13 additions & 4 deletions src/js/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ const matchValue = (value, matcher) => {
return typeof match === 'function' ? match(value) : value;
};

/**
* Check if is permitted to see navigation link
* @param {array} permissions array checked user permissions
* @param {every|some} require type of permissions requirement
* @returns {boolean}
*/
const checkPermissions = async (permissions = [], require = 'every') => {
const userPermissions = await insights.chrome.getUserPermissions();
return userPermissions && permissions[require]((item) => userPermissions.find(({ permission }) => permission === item));
};

export const visibilityFunctions = {
isOrgAdmin: async () => {
const { identity } = await insights.chrome.auth.getUser();
Expand Down Expand Up @@ -56,10 +67,8 @@ export const visibilityFunctions = {
},
isProd: () => insights.chrome.isProd,
isBeta: () => insights.chrome.isBeta(),
hasPermissions: async (permissions = []) => {
const userPermissions = await insights.chrome.getUserPermissions();
return userPermissions && permissions.every((item) => userPermissions.find(({ permission }) => permission === item));
},
loosePermissions: (permissions) => checkPermissions(permissions, 'some'),
hasPermissions: checkPermissions,
apiRequest: async ({ url, method, accessor, matcher, ...options }) => {
return instance({
url,
Expand Down
30 changes: 29 additions & 1 deletion src/js/consts.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */
const { visibilityFunctions, isVisible } = require('./consts');

describe.only('visibilityFunctions', () => {
describe('visibilityFunctions', () => {
const originalUser = insights.chrome.auth.getUser;
const originalProd = insights.chrome.isProd;
const originalBeta = insights.chrome.isBeta;
Expand Down Expand Up @@ -149,4 +149,32 @@ describe('isVisible', () => {
expect(isVisible(['something'], 'something', visibility)).toBe(visibility);
});
});

describe('loose permissions', () => {
const getUserSpy = jest.spyOn(global.window.insights.chrome.auth, 'getUser');
const getUserPermissions = jest.spyOn(window.insights.chrome, 'getUserPermissions');
getUserSpy.mockImplementation(() => Promise.resolve());

beforeEach(() => {
getUserSpy.mockClear();
getUserPermissions.mockReset();
});

afterAll(() => {
getUserSpy.mockRestore();
getUserPermissions.mockRestore();
});

test('should return false if user has no required permission', async () => {
getUserPermissions.mockImplementationOnce(() => Promise.resolve([{ permission: 'dogs:are:best' }]));
const result = await visibilityFunctions.loosePermissions(['foo:bar:baz', 'beep:boop:beep']);
expect(result).toEqual(false);
});

test('should return true if user has atleast one required permission', async () => {
getUserPermissions.mockImplementationOnce(() => Promise.resolve([{ permission: 'foo:bar:baz' }, { permission: 'dogs:are:best' }]));
const result = await visibilityFunctions.loosePermissions(['foo:bar:baz', 'beep:boop:beep']);
expect(result).toEqual(true);
});
});
});