Skip to content

Commit

Permalink
Merge pull request #203 from PolymathNetwork/feat/improve-procedure-t…
Browse files Browse the repository at this point in the history
…ests

feat/improve procedure tests
  • Loading branch information
VictorVicente authored Feb 20, 2020
2 parents 454d285 + 33480b2 commit a8a5e5f
Show file tree
Hide file tree
Showing 50 changed files with 2,154 additions and 696 deletions.
2 changes: 1 addition & 1 deletion src/procedures/AssignStoRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class AssignStoRole extends Procedure<AssignStoRoleProcedureArgs> {
checkStringLength(description, 'description');
// Delegate not found. Add them here
await this.addTransaction(permissionModule.addDelegate, {
tag: PolyTransactionTag.ChangePermission,
tag: PolyTransactionTag.AddDelegate,
})({ delegate, details: description });
}

Expand Down
68 changes: 53 additions & 15 deletions src/procedures/__tests__/ApproveErc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { ImportMock, MockManager } from 'ts-mock-imports';
import * as contractWrappersModule from '@polymathnetwork/contract-wrappers';
import { BigNumber } from '@polymathnetwork/contract-wrappers';
import { spy } from 'sinon';
import { spy, stub } from 'sinon';
import sinon from 'sinon';
import * as contextModule from '../../Context';
import * as polymathBaseModule from '../../PolymathBase';
import { ApproveErc20 } from '../../procedures/ApproveErc20';
Expand Down Expand Up @@ -79,19 +80,28 @@ describe('ApproveErc20', () => {
// Instantiate ApproveErc20
target = new ApproveErc20(params, contextMock.getMockInstance());

const addTransactionSpy = spy(target, 'addTransaction');
const approveArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
polyTokenMock.mock('approve', Promise.resolve('Approve'));
const { approve } = polyTokenMock.getMockInstance();
addTransactionStub.withArgs(approve).returns(approveArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(approveArgsSpy.getCall(0).args[0]).toEqual({
value: params.amount,
spender: params.spender,
});
expect(approveArgsSpy.callCount).toEqual(1);

expect(
addTransactionSpy.getCall(0).calledWithExactly(polyTokenMock.getMockInstance().approve, {
addTransactionStub.getCall(0).calledWithExactly(polyTokenMock.getMockInstance().approve, {
tag: PolyTransactionTag.ApproveErc20,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(1);
expect(addTransactionStub.callCount).toEqual(1);
});

test('should add an approve transaction to the queue using a custom ERC20 contract as a token address is supplied', async () => {
Expand All @@ -104,19 +114,28 @@ describe('ApproveErc20', () => {
// Instantiate ApproveErc20
target = new ApproveErc20({ ...params, tokenAddress }, contextMock.getMockInstance());

const addTransactionSpy = spy(target, 'addTransaction');
const approveArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
erc20Mock.mock('approve', Promise.resolve('Approve'));

const { approve } = erc20Mock.getMockInstance();
addTransactionStub.withArgs(approve).returns(approveArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(approveArgsSpy.getCall(0).args[0]).toEqual({
value: params.amount,
spender: params.spender,
});
expect(approveArgsSpy.callCount).toEqual(1);

expect(
addTransactionSpy.getCall(0).calledWithExactly(erc20Mock.getMockInstance().approve, {
addTransactionStub.getCall(0).calledWithExactly(erc20Mock.getMockInstance().approve, {
tag: PolyTransactionTag.ApproveErc20,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(1);
expect(addTransactionStub.callCount).toEqual(1);
});

test("should throw an error if the wallet doesn't have enough funds to approve the required amount", async () => {
Expand Down Expand Up @@ -145,25 +164,44 @@ describe('ApproveErc20', () => {
// Instantiate ApproveErc20
target = new ApproveErc20(params, contextMock.getMockInstance());

const addTransactionSpy = spy(target, 'addTransaction');
const approveArgsSpy = sinon.spy();
const getPolyTokensArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
polyTokenMock.mock('approve', Promise.resolve('Approve'));
wrappersMock.mock('getPolyTokens', Promise.resolve('GetPolyTokens'));
const { approve } = polyTokenMock.getMockInstance();
const { getPolyTokens } = wrappersMock.getMockInstance();
addTransactionStub.withArgs(approve).returns(approveArgsSpy);
addTransactionStub.withArgs(getPolyTokens).returns(getPolyTokensArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(getPolyTokensArgsSpy.getCall(0).args[0]).toEqual({
address: params.owner,
amount: params.amount,
});
expect(getPolyTokensArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(0).calledWithExactly(wrappersMock.getMockInstance().getPolyTokens, {
tag: PolyTransactionTag.GetTokens,
})
addTransactionStub
.getCall(0)
.calledWithExactly(wrappersMock.getMockInstance().getPolyTokens, {
tag: PolyTransactionTag.GetTokens,
})
).toEqual(true);

expect(approveArgsSpy.getCall(0).args[0]).toEqual({
value: params.amount,
spender: params.spender,
});
expect(approveArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(1).calledWithExactly(polyTokenMock.getMockInstance().approve, {
addTransactionStub.getCall(1).calledWithExactly(polyTokenMock.getMockInstance().approve, {
tag: PolyTransactionTag.ApproveErc20,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(2);

expect(addTransactionStub.callCount).toEqual(2);
});

test('should return if it has sufficient allowance, will never add the transaction', async () => {
Expand Down
41 changes: 33 additions & 8 deletions src/procedures/__tests__/AssignSecurityTokenRole.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable import/no-duplicates */
import { ImportMock, MockManager } from 'ts-mock-imports';
import { spy, restore } from 'sinon';
import { stub, spy, restore } from 'sinon';
import * as contractWrappersModule from '@polymathnetwork/contract-wrappers';
import { ModuleName, Perm } from '@polymathnetwork/contract-wrappers';
import sinon from 'sinon';
import * as contextModule from '../../Context';
import * as wrappersModule from '../../PolymathBase';
import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule';
Expand All @@ -28,6 +29,7 @@ const params = {
};

const moduleAddress = '0x9999999999999999999999999999999999999999';
const modulePermission = Perm.Operator;

describe('AssignSecurityTokenRole', () => {
let target: AssignSecurityTokenRole;
Expand Down Expand Up @@ -73,7 +75,7 @@ describe('AssignSecurityTokenRole', () => {

wrappersMock.mock('roleToPermission', {
moduleName: ModuleName.PercentageTransferManager,
permission: Perm.Operator,
permission: modulePermission,
});
wrappersMock.mock('getModuleAddressesByName', [moduleAddress]);

Expand Down Expand Up @@ -109,25 +111,48 @@ describe('AssignSecurityTokenRole', () => {

test('should add transactions to the queue for add delegate and change permissions with a new delegate address', async () => {
gpmMock.mock('getAllDelegates', Promise.resolve([]));
const addTransactionSpy = spy(target, 'addTransaction');
const addDelegateArgsSpy = sinon.spy();
const changePermissionArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');

gpmMock.mock('addDelegate', Promise.resolve('AddDelegate'));
gpmMock.mock('changePermission', Promise.resolve('ChangePermission'));
const { addDelegate } = gpmMock.getMockInstance();
const { changePermission } = gpmMock.getMockInstance();
addTransactionStub.withArgs(addDelegate).returns(addDelegateArgsSpy);
addTransactionStub.withArgs(changePermission).returns(changePermissionArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(addDelegateArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
details: params.description,
});
expect(addDelegateArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(0).calledWithExactly(gpmMock.getMockInstance().addDelegate, {
addTransactionStub.getCall(0).calledWithExactly(gpmMock.getMockInstance().addDelegate, {
tag: PolyTransactionTag.AddDelegate,
})
).toEqual(true);

expect(changePermissionArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
module: moduleAddress,
perm: modulePermission,
valid: params.assign,
});
expect(changePermissionArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(1).calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
addTransactionStub
.getCall(1)
.calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(2);

expect(addTransactionStub.callCount).toEqual(2);
});

test('should throw if there is no valid security token being provided', async () => {
Expand Down
83 changes: 65 additions & 18 deletions src/procedures/__tests__/AssignStoRole.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ImportMock, MockManager } from 'ts-mock-imports';
import { spy } from 'sinon';
import sinon, { spy, stub } from 'sinon';
import * as contractWrappersModule from '@polymathnetwork/contract-wrappers';
import { Perm } from '@polymathnetwork/contract-wrappers';
import * as contextModule from '../../Context';
import * as wrappersModule from '../../PolymathBase';
import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule';
Expand Down Expand Up @@ -52,42 +53,76 @@ describe('AssignStoRole', () => {

describe('AssignStoRole', () => {
test('should add transaction to the queue to change permission for an sto', async () => {
const addTransactionSpy = spy(target, 'addTransaction');
const changePermissionArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
gpmMock.mock('changePermission', Promise.resolve('ChangePermission'));
const { changePermission } = gpmMock.getMockInstance();
addTransactionStub.withArgs(changePermission).returns(changePermissionArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(changePermissionArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
module: params.stoAddress,
perm: Perm.Admin,
valid: params.assign,
});
expect(changePermissionArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(0).calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
addTransactionStub
.getCall(0)
.calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(1);
expect(addTransactionStub.callCount).toEqual(1);
});

test('should add transaction to the queue to add delegate and change permission for a new delegate in an sto', async () => {
gpmMock.mock('getAllDelegates', Promise.resolve([]));
const addTransactionSpy = spy(target, 'addTransaction');
const addDelegateArgsSpy = sinon.spy();
const changePermissionArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
gpmMock.mock('addDelegate', Promise.resolve('AddDelegate'));
gpmMock.mock('changePermission', Promise.resolve('ChangePermission'));
const { addDelegate } = gpmMock.getMockInstance();
const { changePermission } = gpmMock.getMockInstance();
addTransactionStub.withArgs(addDelegate).returns(addDelegateArgsSpy);
addTransactionStub.withArgs(changePermission).returns(changePermissionArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(addDelegateArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
details: '',
});
expect(addDelegateArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(0).calledWithExactly(gpmMock.getMockInstance().addDelegate, {
tag: PolyTransactionTag.ChangePermission,
addTransactionStub.getCall(0).calledWithExactly(gpmMock.getMockInstance().addDelegate, {
tag: PolyTransactionTag.AddDelegate,
})
).toEqual(true);

expect(changePermissionArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
module: params.stoAddress,
perm: Perm.Admin,
valid: params.assign,
});
expect(changePermissionArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(1).calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
addTransactionStub
.getCall(1)
.calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(2);

expect(addTransactionStub.callCount).toEqual(2);
});

test('should throw error if the description is over 32 characters long', async () => {
Expand All @@ -111,19 +146,31 @@ describe('AssignStoRole', () => {
{ ...params, role: StoRole.StoOperator },
contextMock.getMockInstance()
);
const addTransactionSpy = spy(target, 'addTransaction');
const changePermissionArgsSpy = sinon.spy();
const addTransactionStub = stub(target, 'addTransaction');
gpmMock.mock('changePermission', Promise.resolve('ChangePermission'));
const { changePermission } = gpmMock.getMockInstance();
addTransactionStub.withArgs(changePermission).returns(changePermissionArgsSpy);

// Real call
await target.prepareTransactions();

// Verifications
expect(changePermissionArgsSpy.getCall(0).args[0]).toEqual({
delegate: params.delegateAddress,
module: params.stoAddress,
perm: Perm.Operator,
valid: params.assign,
});
expect(changePermissionArgsSpy.callCount).toEqual(1);
expect(
addTransactionSpy.getCall(0).calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
addTransactionStub
.getCall(0)
.calledWithExactly(gpmMock.getMockInstance().changePermission, {
tag: PolyTransactionTag.ChangePermission,
})
).toEqual(true);
expect(addTransactionSpy.callCount).toEqual(1);
expect(addTransactionStub.callCount).toEqual(1);
});

test('should throw if there is no valid security token supplied', async () => {
Expand Down
Loading

0 comments on commit a8a5e5f

Please sign in to comment.