Skip to content

Commit

Permalink
test: move permissions.test.ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 17, 2024
1 parent 8b2bb98 commit fca2ea5
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 163 deletions.
164 changes: 1 addition & 163 deletions src/lib/detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { INTERFACE_IDS_0_12_0 } from '../constants/interfaces';

import { internalSupportsInterface, checkPermissions } from './detector';
import { internalSupportsInterface } from './detector';

describe('supportsInterface', () => {
it('it should return true if the contract supports the interface with name', async () => {
Expand Down Expand Up @@ -66,165 +66,3 @@ describe('supportsInterface', () => {
expect(doesSupportInterface).to.be.true;
});
});

describe('checkPermissions', () => {
describe('test with single permission', () => {
it('should throw an error when given an invalid permission string', async () => {
const requiredPermissions = 'INVALIDPERMISSION';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an invalid 32-byte hex string', async () => {
const requiredPermissions = '0xinvalidhexstring';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an invalid grantedPermission 32-byte hex string', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions = '0xinvalidgrantedpermissionhexstring';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid grantedPermissions string. It must be a valid 32-byte hex string.',
);
});

it('should return true when single literal permission matches granted permissions', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return true when single bytes32 permission matches granted permissions', async () => {
const requiredPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when single bytes32 permission does not match granted permissions', async () => {
const requiredPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000fff2';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when single literal permission does not match granted permissions', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000fff2';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});
});

describe('test with multiple permissions', () => {
it('should throw an error when given an array containing an invalid permission string', async () => {
const requiredPermissions = ['CHANGEOWNER', 'INVALIDPERMISSION'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an array containing an invalid 32-byte hex string', async () => {
const requiredPermissions = ['CHANGEOWNER', '0xinvalidhexstring'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should return false when one of the literal permissions does not match granted permissions', async () => {
const requiredPermissions = ['EDITPERMISSIONS', 'CALL'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when one of the bytes32 permissions does not match granted permissions', async () => {
const requiredPermissions = [
'0x0000000000000000000000000000000000000000000000000000000000000004',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return true when all the mixed literal and bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'EDITPERMISSIONS',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff54';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when not all multiple literal permissions match granted permissions', async () => {
const requiredPermissions = ['CHANGEOWNER', 'CALL'];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return true when all multiple literal permissions match granted permissions', async () => {
const requiredPermissions = ['CHANGEOWNER', 'CALL'];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000801';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when not all multiple bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when not all mixed literal and bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'CHANGEOWNER',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});
});
});
164 changes: 164 additions & 0 deletions src/lib/permissions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { expect } from 'chai';
import { checkPermissions } from './permissions';

describe('checkPermissions', () => {
describe('test with single permission', () => {
it('should throw an error when given an invalid permission string', async () => {
const requiredPermissions = 'INVALIDPERMISSION';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an invalid 32-byte hex string', async () => {
const requiredPermissions = '0xinvalidhexstring';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an invalid grantedPermission 32-byte hex string', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions = '0xinvalidgrantedpermissionhexstring';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid grantedPermissions string. It must be a valid 32-byte hex string.',
);
});

it('should return true when single literal permission matches granted permissions', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return true when single bytes32 permission matches granted permissions', async () => {
const requiredPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when single bytes32 permission does not match granted permissions', async () => {
const requiredPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000fff2';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when single literal permission does not match granted permissions', async () => {
const requiredPermissions = 'CHANGEOWNER';
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000fff2';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});
});

describe('test with multiple permissions', () => {
it('should throw an error when given an array containing an invalid permission string', async () => {
const requiredPermissions = ['CHANGEOWNER', 'INVALIDPERMISSION'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should throw an error when given an array containing an invalid 32-byte hex string', async () => {
const requiredPermissions = ['CHANGEOWNER', '0xinvalidhexstring'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
expect(() =>
checkPermissions(requiredPermissions, grantedPermissions),
).to.throw(
'Invalid permission string. It must be a valid 32-byte hex string or a known permission name.',
);
});

it('should return false when one of the literal permissions does not match granted permissions', async () => {
const requiredPermissions = ['EDITPERMISSIONS', 'CALL'];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when one of the bytes32 permissions does not match granted permissions', async () => {
const requiredPermissions = [
'0x0000000000000000000000000000000000000000000000000000000000000004',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff51';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return true when all the mixed literal and bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'EDITPERMISSIONS',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x000000000000000000000000000000000000000000000000000000000000ff54';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when not all multiple literal permissions match granted permissions', async () => {
const requiredPermissions = ['CHANGEOWNER', 'CALL'];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return true when all multiple literal permissions match granted permissions', async () => {
const requiredPermissions = ['CHANGEOWNER', 'CALL'];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000801';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.true;
});

it('should return false when not all multiple bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});

it('should return false when not all mixed literal and bytes32 permissions match granted permissions', async () => {
const requiredPermissions = [
'CHANGEOWNER',
'0x0000000000000000000000000000000000000000000000000000000000000800',
];
const grantedPermissions =
'0x0000000000000000000000000000000000000000000000000000000000000051';
const result = checkPermissions(requiredPermissions, grantedPermissions);
expect(result).to.be.false;
});
});
});

0 comments on commit fca2ea5

Please sign in to comment.