From fca2ea53af20f5d45975b238b161c1eec09fb0b9 Mon Sep 17 00:00:00 2001 From: CJ42 Date: Fri, 17 May 2024 09:31:50 +0200 Subject: [PATCH] test: move `permissions.test.ts` file --- src/lib/detector.test.ts | 164 +----------------------------------- src/lib/permissions.test.ts | 164 ++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 163 deletions(-) create mode 100644 src/lib/permissions.test.ts diff --git a/src/lib/detector.test.ts b/src/lib/detector.test.ts index d397feae..ac4afd50 100644 --- a/src/lib/detector.test.ts +++ b/src/lib/detector.test.ts @@ -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 () => { @@ -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; - }); - }); -}); diff --git a/src/lib/permissions.test.ts b/src/lib/permissions.test.ts new file mode 100644 index 00000000..e7ac432b --- /dev/null +++ b/src/lib/permissions.test.ts @@ -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; + }); + }); +});