From c7a5889b9ca5d11983c7326374eb9b3a9115755e Mon Sep 17 00:00:00 2001 From: Victor Wiebe Date: Thu, 21 Nov 2019 07:41:50 -0800 Subject: [PATCH 1/2] refactor: add prettier, rectify procedureTypes without hardcode string --- src/procedures/__tests__/AssignSecurityTokenRole.ts | 10 ++++++++-- src/procedures/__tests__/AssignStoRole.ts | 4 ++-- src/procedures/__tests__/EnableDividendManagers.ts | 4 ++-- .../__tests__/EnableGeneralPermissionManager.ts | 4 ++-- src/procedures/__tests__/PauseSto.ts | 1 + 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/procedures/__tests__/AssignSecurityTokenRole.ts b/src/procedures/__tests__/AssignSecurityTokenRole.ts index 56066e2..1e211ac 100644 --- a/src/procedures/__tests__/AssignSecurityTokenRole.ts +++ b/src/procedures/__tests__/AssignSecurityTokenRole.ts @@ -8,7 +8,13 @@ import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; import { AssignSecurityTokenRole } from '../../procedures/AssignSecurityTokenRole'; import { Procedure } from '../Procedure'; import { PolymathError } from '../../PolymathError'; -import { ErrorCode, Feature, PolyTransactionTag, SecurityTokenRole } from '../../types'; +import { + ErrorCode, + Feature, + PolyTransactionTag, + ProcedureType, + SecurityTokenRole, +} from '../../types'; import * as securityTokenFactoryModule from '../../entities/factories/SecurityTokenFactory'; import { mockFactories } from '../../testUtils/mockFactories'; @@ -76,7 +82,7 @@ describe('AssignSecurityTokenRole', () => { describe('Types', () => { test('should extend procedure and have AssignSecurityTokenRole type', async () => { expect(target instanceof Procedure).toBe(true); - expect(target.type).toBe('AssignSecurityTokenRole'); + expect(target.type).toBe(ProcedureType.AssignSecurityTokenRole); }); }); diff --git a/src/procedures/__tests__/AssignStoRole.ts b/src/procedures/__tests__/AssignStoRole.ts index dc773c1..b12aaa3 100644 --- a/src/procedures/__tests__/AssignStoRole.ts +++ b/src/procedures/__tests__/AssignStoRole.ts @@ -7,7 +7,7 @@ import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; import { AssignStoRole } from '../../procedures/AssignStoRole'; import { Procedure } from '../Procedure'; import { PolymathError } from '../../PolymathError'; -import { ErrorCode, PolyTransactionTag, StoRole } from '../../types'; +import { ErrorCode, PolyTransactionTag, ProcedureType, StoRole } from '../../types'; const params = { symbol: 'TEST1', @@ -46,7 +46,7 @@ describe('AssignStoRole', () => { describe('Types', () => { test('should extend procedure and have AssignStoRole type', async () => { expect(target instanceof Procedure).toBe(true); - expect(target.type).toBe('AssignStoRole'); + expect(target.type).toBe(ProcedureType.AssignStoRole); }); }); diff --git a/src/procedures/__tests__/EnableDividendManagers.ts b/src/procedures/__tests__/EnableDividendManagers.ts index 84c8d7d..344df0a 100644 --- a/src/procedures/__tests__/EnableDividendManagers.ts +++ b/src/procedures/__tests__/EnableDividendManagers.ts @@ -8,7 +8,7 @@ import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; import { EnableDividendManagers } from '../../procedures/EnableDividendManagers'; import { Procedure } from '../Procedure'; import { PolymathError } from '../../PolymathError'; -import { ErrorCode, PolyTransactionTag } from '../../types'; +import { ErrorCode, PolyTransactionTag, ProcedureType } from '../../types'; const params = { symbol: 'TEST1', @@ -57,7 +57,7 @@ describe('EnableDividendManagers', () => { describe('Types', () => { test('should extend procedure and have EnableDividendManagers type', async () => { expect(target instanceof Procedure).toBe(true); - expect(target.type).toBe('EnableDividendManagers'); + expect(target.type).toBe(ProcedureType.EnableDividendManagers); }); }); diff --git a/src/procedures/__tests__/EnableGeneralPermissionManager.ts b/src/procedures/__tests__/EnableGeneralPermissionManager.ts index 3dca716..f03f214 100644 --- a/src/procedures/__tests__/EnableGeneralPermissionManager.ts +++ b/src/procedures/__tests__/EnableGeneralPermissionManager.ts @@ -8,7 +8,7 @@ import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; import { EnableGeneralPermissionManager } from '../../procedures/EnableGeneralPermissionManager'; import { Procedure } from '../Procedure'; import { PolymathError } from '../../PolymathError'; -import { ErrorCode, PolyTransactionTag } from '../../types'; +import { ErrorCode, PolyTransactionTag, ProcedureType } from '../../types'; const params = { symbol: 'TEST1', @@ -56,7 +56,7 @@ describe('EnableGeneralPermissionManager', () => { describe('Types', () => { test('should extend procedure and have EnableGeneralPermissionManager type', async () => { expect(target instanceof Procedure).toBe(true); - expect(target.type).toBe('EnableGeneralPermissionManager'); + expect(target.type).toBe(ProcedureType.EnableGeneralPermissionManager); }); }); diff --git a/src/procedures/__tests__/PauseSto.ts b/src/procedures/__tests__/PauseSto.ts index d9adeca..b8b8172 100644 --- a/src/procedures/__tests__/PauseSto.ts +++ b/src/procedures/__tests__/PauseSto.ts @@ -100,6 +100,7 @@ describe('PauseSto', () => { // Instantiate PauseSto target = new PauseSto(cappedParams, contextMock.getMockInstance()); }); + afterEach(() => { restore(); }); From 44631b7ea739eb076090ba5344777ab0937bbe67 Mon Sep 17 00:00:00 2001 From: Victor Wiebe Date: Thu, 21 Nov 2019 08:03:26 -0800 Subject: [PATCH 2/2] test: enableGTM procedure --- .../__tests__/EnableGeneralTransferManager.ts | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/procedures/__tests__/EnableGeneralTransferManager.ts diff --git a/src/procedures/__tests__/EnableGeneralTransferManager.ts b/src/procedures/__tests__/EnableGeneralTransferManager.ts new file mode 100644 index 0000000..ce8e237 --- /dev/null +++ b/src/procedures/__tests__/EnableGeneralTransferManager.ts @@ -0,0 +1,96 @@ +import { ImportMock, MockManager } from 'ts-mock-imports'; +import { spy, restore } from 'sinon'; +import { BigNumber } from '@polymathnetwork/contract-wrappers'; +import * as contractWrappersModule from '@polymathnetwork/contract-wrappers'; +import * as contextModule from '../../Context'; +import * as wrappersModule from '../../PolymathBase'; +import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; +import { EnableGeneralTransferManager } from '../../procedures/EnableGeneralTransferManager'; +import { Procedure } from '../Procedure'; +import { PolymathError } from '../../PolymathError'; +import { ErrorCode, PolyTransactionTag, ProcedureType } from '../../types'; + +const params = { + symbol: 'TEST1', + address: '0x4444444444444444444444444444444444444444', +}; +const securityTokenAddress = '0x2222222222222222222222222222222222222222'; +const moduleFactoryAddress = '0x3333333333333333333333333333333333333333'; + +describe('EnableGeneralTransferManager', () => { + let target: EnableGeneralTransferManager; + let contextMock: MockManager; + let wrappersMock: MockManager; + let tokenFactoryMock: MockManager; + let etherDividendsMock: MockManager; + let securityTokenMock: MockManager; + + beforeAll(() => { + // Mock the context, wrappers, and tokenFactory to test EnableGeneralTransferManagers + contextMock = ImportMock.mockClass(contextModule, 'Context'); + wrappersMock = ImportMock.mockClass(wrappersModule, 'PolymathBase'); + tokenFactoryMock = ImportMock.mockClass(tokenFactoryModule, 'MockedTokenFactoryModule'); + contextMock.set('contractWrappers', wrappersMock.getMockInstance()); + wrappersMock.set('tokenFactory', tokenFactoryMock.getMockInstance()); + + securityTokenMock = ImportMock.mockClass(contractWrappersModule, 'SecurityToken_3_0_0'); + securityTokenMock.mock('address', Promise.resolve(securityTokenAddress)); + + etherDividendsMock = ImportMock.mockClass( + contractWrappersModule, + 'EtherDividendCheckpoint_3_0_0' + ); + wrappersMock.mock('getModuleFactoryAddress', Promise.resolve(moduleFactoryAddress)); + tokenFactoryMock.mock( + 'getSecurityTokenInstanceFromTicker', + securityTokenMock.getMockInstance() + ); + + // Instantiate EnableGeneralTransferManagers + target = new EnableGeneralTransferManager(params, contextMock.getMockInstance()); + }); + afterEach(() => { + restore(); + }); + + describe('Types', () => { + test('should extend procedure and have EnableGeneralTransferManager type', async () => { + expect(target instanceof Procedure).toBe(true); + expect(target.type).toBe(ProcedureType.EnableGeneralTransferManager); + }); + }); + + describe('EnableGeneralTransferManager', () => { + test('should add a transaction to the queue to enable general transfer manager', async () => { + const addTransactionSpy = spy(target, 'addTransaction'); + securityTokenMock.mock('addModuleWithLabel', Promise.resolve('AddModuleWithLabel')); + + // Real call + await target.prepareTransactions(); + + // Verifications + expect( + addTransactionSpy + .getCall(0) + .calledWithExactly(securityTokenMock.getMockInstance().addModuleWithLabel, { + tag: PolyTransactionTag.EnableGeneralTransferManager, + }) + ).toEqual(true); + expect(addTransactionSpy.callCount).toEqual(1); + }); + + test('should throw if there is no valid security token supplied', async () => { + tokenFactoryMock + .mock('getSecurityTokenInstanceFromTicker') + .withArgs(params.symbol) + .throws(); + + await expect(target.prepareTransactions()).rejects.toThrow( + new PolymathError({ + code: ErrorCode.ProcedureValidationError, + message: `There is no Security Token with symbol ${params.symbol}`, + }) + ); + }); + }); +});