Commit f347ead8 authored by ubbcou's avatar ubbcou Committed by ι™ˆεΈ…

check it when authority is string and currentAuthority is array. And update CheckPermissions test

parent fdd6035f
...@@ -45,6 +45,14 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => { ...@@ -45,6 +45,14 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => {
if (authority === currentAuthority) { if (authority === currentAuthority) {
return target; return target;
} }
if (Array.isArray(currentAuthority)) {
for (let i = 0; i < currentAuthority.length; i += 1) {
const element = currentAuthority[i];
if (authority.indexOf(element) >= 0) {
return target;
}
}
}
return Exception; return Exception;
} }
......
...@@ -34,4 +34,22 @@ describe('test CheckPermissions', () => { ...@@ -34,4 +34,22 @@ describe('test CheckPermissions', () => {
it('Correct Function permission authentication', () => { it('Correct Function permission authentication', () => {
expect(checkPermissions(() => true, 'guest', target, error)).toEqual('ok'); expect(checkPermissions(() => true, 'guest', target, error)).toEqual('ok');
}); });
it('authority is string, currentAuthority is array, return ok', () => {
expect(checkPermissions('user', ['user'], target, error)).toEqual('ok');
});
it('authority is string, currentAuthority is array, return ok', () => {
expect(checkPermissions('user', ['user', 'admin'], target, error)).toEqual('ok');
});
it('authority is array, currentAuthority is array, return ok', () => {
expect(checkPermissions(['user', 'admin'], ['user', 'admin'], target, error)).toEqual('ok');
});
it('Wrong Function permission authentication', () => {
expect(checkPermissions(() => false, ['user'], target, error)).toEqual('error');
});
it('Correct Function permission authentication', () => {
expect(checkPermissions(() => true, ['user'], target, error)).toEqual('ok');
});
it('authority is undefined , return ok', () => {
expect(checkPermissions(null, ['user'], target, error)).toEqual('ok');
});
}); });
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment