|
| 1 | +import { ImportMock, MockManager } from 'ts-mock-imports'; |
| 2 | +import { spy, restore } from 'sinon'; |
| 3 | +import * as contractWrappersModule from '@polymathnetwork/contract-wrappers'; |
| 4 | +import * as contextModule from '../../Context'; |
| 5 | +import { Factories } from '../../Context'; |
| 6 | +import * as wrappersModule from '../../PolymathBase'; |
| 7 | +import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; |
| 8 | +import { SignTransferData } from '../../procedures/SignTransferData'; |
| 9 | +import { Procedure } from '../../procedures/Procedure'; |
| 10 | +import { ProcedureType, ErrorCode, PolyTransactionTag } from '../../types'; |
| 11 | +import { PolymathError } from '../../PolymathError'; |
| 12 | +import { mockFactories } from '../../testUtils/mockFactories'; |
| 13 | + |
| 14 | +const params = { |
| 15 | + symbol: 'TEST1', |
| 16 | + whitelistData: [ |
| 17 | + { |
| 18 | + address: '0x01', |
| 19 | + canSendAfter: new Date(), |
| 20 | + canReceiveAfter: new Date(), |
| 21 | + kycExpiry: new Date(), |
| 22 | + }, |
| 23 | + { |
| 24 | + address: '0x02', |
| 25 | + canSendAfter: new Date(), |
| 26 | + canReceiveAfter: new Date(), |
| 27 | + kycExpiry: new Date(), |
| 28 | + }, |
| 29 | + ], |
| 30 | + validFrom: new Date(0), |
| 31 | + validTo: new Date(new Date().getTime() + 10000), |
| 32 | +}; |
| 33 | + |
| 34 | +describe('SignTransferData', () => { |
| 35 | + let target: SignTransferData; |
| 36 | + let contextMock: MockManager<contextModule.Context>; |
| 37 | + let wrappersMock: MockManager<wrappersModule.PolymathBase>; |
| 38 | + let tokenFactoryMock: MockManager<tokenFactoryModule.MockedTokenFactoryModule>; |
| 39 | + let securityTokenMock: MockManager<contractWrappersModule.SecurityToken_3_0_0>; |
| 40 | + let factoriesMockedSetup: Factories; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + // Mock the context, wrappers, tokenFactory and securityToken to test SignTransferData |
| 44 | + contextMock = ImportMock.mockClass(contextModule, 'Context'); |
| 45 | + wrappersMock = ImportMock.mockClass(wrappersModule, 'PolymathBase'); |
| 46 | + |
| 47 | + tokenFactoryMock = ImportMock.mockClass(tokenFactoryModule, 'MockedTokenFactoryModule'); |
| 48 | + securityTokenMock = ImportMock.mockClass(contractWrappersModule, 'SecurityToken_3_0_0'); |
| 49 | + |
| 50 | + tokenFactoryMock.mock( |
| 51 | + 'getSecurityTokenInstanceFromTicker', |
| 52 | + securityTokenMock.getMockInstance() |
| 53 | + ); |
| 54 | + |
| 55 | + contextMock.set('contractWrappers', wrappersMock.getMockInstance()); |
| 56 | + wrappersMock.set('tokenFactory', tokenFactoryMock.getMockInstance()); |
| 57 | + |
| 58 | + factoriesMockedSetup = mockFactories(); |
| 59 | + contextMock.set('factories', factoriesMockedSetup); |
| 60 | + |
| 61 | + target = new SignTransferData(params, contextMock.getMockInstance()); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + restore(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Types', () => { |
| 69 | + test('should extend procedure and have SignTransferData type', async () => { |
| 70 | + expect(target instanceof Procedure).toBe(true); |
| 71 | + expect(target.type).toBe(ProcedureType.SignTransferData); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('SignTransferData', () => { |
| 76 | + test('should throw if there is no valid security token being provided', async () => { |
| 77 | + tokenFactoryMock |
| 78 | + .mock('getSecurityTokenInstanceFromTicker') |
| 79 | + .withArgs(params.symbol) |
| 80 | + .throws(); |
| 81 | + |
| 82 | + await expect(target.prepareTransactions()).rejects.toThrow( |
| 83 | + new PolymathError({ |
| 84 | + code: ErrorCode.ProcedureValidationError, |
| 85 | + message: `There is no Security Token with symbol ${params.symbol}`, |
| 86 | + }) |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + test('should throw if the signature validity lower bound is not an earlier date than the upper bound', async () => { |
| 91 | + const now = new Date(); |
| 92 | + target = new SignTransferData( |
| 93 | + { |
| 94 | + ...params, |
| 95 | + validTo: now, |
| 96 | + validFrom: now, |
| 97 | + }, |
| 98 | + contextMock.getMockInstance() |
| 99 | + ); |
| 100 | + |
| 101 | + // Real call |
| 102 | + await expect(target.prepareTransactions()).rejects.toThrowError( |
| 103 | + new PolymathError({ |
| 104 | + code: ErrorCode.ProcedureValidationError, |
| 105 | + message: 'Signature validity lower bound must be at an earlier date than the upper bound', |
| 106 | + }) |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should throw if the signature validity upper bound is in the past', async () => { |
| 111 | + const now = new Date(); |
| 112 | + target = new SignTransferData( |
| 113 | + { |
| 114 | + ...params, |
| 115 | + validTo: new Date(now.getTime() - 10000), |
| 116 | + }, |
| 117 | + contextMock.getMockInstance() |
| 118 | + ); |
| 119 | + |
| 120 | + // Real call |
| 121 | + await expect(target.prepareTransactions()).rejects.toThrowError( |
| 122 | + new PolymathError({ |
| 123 | + code: ErrorCode.ProcedureValidationError, |
| 124 | + message: "Signature validity upper bound can't be in the past", |
| 125 | + }) |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test('should add a signature request to the queue to sign whitelist data', async () => { |
| 130 | + const addSignatureRequestSpy = spy(target, 'addSignatureRequest'); |
| 131 | + securityTokenMock.mock('signTransferData', Promise.resolve('SignTransferData')); |
| 132 | + |
| 133 | + // Real call |
| 134 | + await target.prepareTransactions(); |
| 135 | + |
| 136 | + // Verifications |
| 137 | + expect( |
| 138 | + addSignatureRequestSpy |
| 139 | + .getCall(0) |
| 140 | + .calledWith(securityTokenMock.getMockInstance().signTransferData) |
| 141 | + ).toEqual(true); |
| 142 | + expect(addSignatureRequestSpy.callCount).toEqual(1); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments