CheckPermissions.test.js 2.47 KB
Newer Older
1
import { checkPermissions } from './CheckPermissions';
ddcat1115's avatar
ddcat1115 committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15

const target = 'ok';
const error = 'error';

describe('test CheckPermissions', () => {
  it('Correct string permission authentication', () => {
    expect(checkPermissions('user', 'user', target, error)).toEqual('ok');
  });
  it('Correct string permission authentication', () => {
    expect(checkPermissions('user', 'NULL', target, error)).toEqual('error');
  });
  it('authority is undefined , return ok', () => {
    expect(checkPermissions(null, 'NULL', target, error)).toEqual('ok');
  });
陈帅's avatar
陈帅 committed
16 17 18
  it('currentAuthority is undefined , return error', () => {
    expect(checkPermissions('admin', null, target, error)).toEqual('error');
  });
ddcat1115's avatar
ddcat1115 committed
19 20 21 22
  it('Wrong string permission authentication', () => {
    expect(checkPermissions('admin', 'user', target, error)).toEqual('error');
  });
  it('Correct Array permission authentication', () => {
jim's avatar
jim committed
23
    expect(checkPermissions(['user', 'admin'], 'user', target, error)).toEqual('ok');
ddcat1115's avatar
ddcat1115 committed
24 25
  });
  it('Wrong Array permission authentication,currentAuthority error', () => {
jim's avatar
jim committed
26
    expect(checkPermissions(['user', 'admin'], 'user,admin', target, error)).toEqual('error');
ddcat1115's avatar
ddcat1115 committed
27 28
  });
  it('Wrong Array permission authentication', () => {
jim's avatar
jim committed
29
    expect(checkPermissions(['user', 'admin'], 'guest', target, error)).toEqual('error');
ddcat1115's avatar
ddcat1115 committed
30 31
  });
  it('Wrong Function permission authentication', () => {
jim's avatar
jim committed
32
    expect(checkPermissions(() => false, 'guest', target, error)).toEqual('error');
ddcat1115's avatar
ddcat1115 committed
33 34 35 36
  });
  it('Correct Function permission authentication', () => {
    expect(checkPermissions(() => true, 'guest', target, error)).toEqual('ok');
  });
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  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');
  });
ddcat1115's avatar
ddcat1115 committed
55
});