Migrate utils, payment and ownership tests.

This commit is contained in:
Nicolás Venturo
2019-01-09 17:43:37 -03:00
parent d32d4c3773
commit b55f557c90
12 changed files with 78 additions and 109 deletions

View File

@ -1,7 +1,4 @@
const shouldFail = require('../helpers/shouldFail');
const expectEvent = require('../helpers/expectEvent');
const { ZERO_ADDRESS } = require('../helpers/constants');
require('./../helpers/setup');
const { constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
function shouldBehaveLikeOwnable (owner, [anyone]) {
describe('as an ownable', function () {
@ -23,14 +20,14 @@ function shouldBehaveLikeOwnable (owner, [anyone]) {
});
it('should guard ownership against stuck state', async function () {
await shouldFail.reverting(this.ownable.transferOwnership(null, { from: owner }));
await shouldFail.reverting(this.ownable.transferOwnership(constants.ZERO_ADDRESS, { from: owner }));
});
it('loses owner after renouncement', async function () {
const { logs } = await this.ownable.renounceOwnership({ from: owner });
expectEvent.inLogs(logs, 'OwnershipTransferred');
(await this.ownable.owner()).should.equal(ZERO_ADDRESS);
(await this.ownable.owner()).should.equal(constants.ZERO_ADDRESS);
});
it('should prevent non-owners from renouncement', async function () {

View File

@ -1,3 +1,4 @@
require('openzeppelin-test-helpers');
const { shouldBehaveLikeOwnable } = require('./Ownable.behavior');
const Ownable = artifacts.require('OwnableMock');

View File

@ -1,11 +1,7 @@
const shouldFail = require('../helpers/shouldFail');
const expectEvent = require('../helpers/expectEvent');
const { ZERO_ADDRESS } = require('../helpers/constants');
const { constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
const SecondaryMock = artifacts.require('SecondaryMock');
require('../helpers/setup');
contract('Secondary', function ([_, primary, newPrimary, anyone]) {
beforeEach(async function () {
this.secondary = await SecondaryMock.new({ from: primary });
@ -33,7 +29,7 @@ contract('Secondary', function ([_, primary, newPrimary, anyone]) {
});
it('reverts when transfering to the null address', async function () {
await shouldFail.reverting(this.secondary.transferPrimary(ZERO_ADDRESS, { from: primary }));
await shouldFail.reverting(this.secondary.transferPrimary(constants.ZERO_ADDRESS, { from: primary }));
});
it('reverts when called by anyone', async function () {

View File

@ -1,16 +1,9 @@
const { ethGetBalance } = require('../helpers/web3');
const expectEvent = require('../helpers/expectEvent');
const send = require('./../helpers/send');
const { ether } = require('../helpers/ether');
const { ZERO_ADDRESS } = require('./../helpers/constants');
const { balance, constants, ether, expectEvent, send, shouldFail } = require('openzeppelin-test-helpers');
require('../helpers/setup');
const shouldFail = require('../helpers/shouldFail');
const PaymentSplitter = artifacts.require('PaymentSplitter');
contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpayee1, payer1]) {
const amount = ether(1.0);
const amount = ether('1', 'ether');
it('rejects an empty set of payees', async function () {
await shouldFail.reverting(PaymentSplitter.new([], []));
@ -25,7 +18,7 @@ contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpaye
});
it('rejects null payees', async function () {
await shouldFail.reverting(PaymentSplitter.new([payee1, ZERO_ADDRESS], [20, 30]));
await shouldFail.reverting(PaymentSplitter.new([payee1, constants.ZERO_ADDRESS], [20, 30]));
});
it('rejects zero-valued shares', async function () {
@ -45,28 +38,28 @@ contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpaye
});
it('should have total shares', async function () {
(await this.contract.totalShares()).should.be.bignumber.equal(20 + 10 + 70);
(await this.contract.totalShares()).should.be.bignumber.equal('100');
});
it('should have payees', async function () {
await Promise.all(this.payees.map(async (payee, index) => {
(await this.contract.payee(index)).should.be.equal(payee);
(await this.contract.released(payee)).should.be.bignumber.equal(0);
(await this.contract.released(payee)).should.be.bignumber.equal('0');
}));
});
it('should accept payments', async function () {
await send.ether(owner, this.contract.address, amount);
(await ethGetBalance(this.contract.address)).should.be.bignumber.equal(amount);
(await balance.current(this.contract.address)).should.be.bignumber.equal(amount);
});
it('should store shares if address is payee', async function () {
(await this.contract.shares(payee1)).should.be.bignumber.not.equal(0);
(await this.contract.shares(payee1)).should.be.bignumber.not.equal('0');
});
it('should not store shares if address is not payee', async function () {
(await this.contract.shares(nonpayee1)).should.be.bignumber.equal(0);
(await this.contract.shares(nonpayee1)).should.be.bignumber.equal('0');
});
it('should throw if no funds to claim', async function () {
@ -82,30 +75,31 @@ contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpaye
await send.ether(payer1, this.contract.address, amount);
// receive funds
const initBalance = await ethGetBalance(this.contract.address);
const initBalance = await balance.current(this.contract.address);
initBalance.should.be.bignumber.equal(amount);
// distribute to payees
const initAmount1 = await ethGetBalance(payee1);
const { logs: logs1 } = await this.contract.release(payee1);
const profit1 = (await ethGetBalance(payee1)).sub(initAmount1);
profit1.sub(web3.toWei(0.20, 'ether')).abs().should.be.bignumber.lt(1e16);
const initAmount1 = await balance.current(payee1);
const { logs: logs1 } = await this.contract.release(payee1, { gasPrice: 0 });
const profit1 = (await balance.current(payee1)).sub(initAmount1);
profit1.should.be.bignumber.equal(ether('0.20', 'ether'));
expectEvent.inLogs(logs1, 'PaymentReleased', { to: payee1, amount: profit1 });
const initAmount2 = await ethGetBalance(payee2);
const { logs: logs2 } = await this.contract.release(payee2);
const profit2 = (await ethGetBalance(payee2)).sub(initAmount2);
profit2.sub(web3.toWei(0.10, 'ether')).abs().should.be.bignumber.lt(1e16);
const initAmount2 = await balance.current(payee2);
const { logs: logs2 } = await this.contract.release(payee2, { gasPrice: 0 });
const profit2 = (await balance.current(payee2)).sub(initAmount2);
profit2.should.be.bignumber.equal(ether('0.10', 'ether'));
expectEvent.inLogs(logs2, 'PaymentReleased', { to: payee2, amount: profit2 });
const initAmount3 = await ethGetBalance(payee3);
const { logs: logs3 } = await this.contract.release(payee3);
const profit3 = (await ethGetBalance(payee3)).sub(initAmount3);
profit3.sub(web3.toWei(0.70, 'ether')).abs().should.be.bignumber.lt(1e16);
const initAmount3 = await balance.current(payee3);
const { logs: logs3 } = await this.contract.release(payee3, { gasPrice: 0 });
const profit3 = (await balance.current(payee3)).sub(initAmount3);
profit3.should.be.bignumber.equal(ether('0.70', 'ether'));
expectEvent.inLogs(logs3, 'PaymentReleased', { to: payee3, amount: profit3 });
// end balance should be zero
(await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0);
(await balance.current(this.contract.address)).should.be.bignumber.equal('0');
// check correct funds released accounting
(await this.contract.totalReleased()).should.be.bignumber.equal(initBalance);

View File

@ -1,12 +1,9 @@
const { balanceDifference } = require('../helpers/balanceDifference');
const { ether } = require('../helpers/ether');
require('../helpers/setup');
const { balance, ether } = require('openzeppelin-test-helpers');
const PullPaymentMock = artifacts.require('PullPaymentMock');
contract('PullPayment', function ([_, payer, payee1, payee2]) {
const amount = ether(17.0);
const amount = ether('17', 'ether');
beforeEach(async function () {
this.contract = await PullPaymentMock.new({ value: amount });
@ -14,32 +11,32 @@ contract('PullPayment', function ([_, payer, payee1, payee2]) {
it('can record an async payment correctly', async function () {
await this.contract.callTransfer(payee1, 100, { from: payer });
(await this.contract.payments(payee1)).should.be.bignumber.equal(100);
(await this.contract.payments(payee1)).should.be.bignumber.equal('100');
});
it('can add multiple balances on one account', async function () {
await this.contract.callTransfer(payee1, 200, { from: payer });
await this.contract.callTransfer(payee1, 300, { from: payer });
(await this.contract.payments(payee1)).should.be.bignumber.equal(500);
(await this.contract.payments(payee1)).should.be.bignumber.equal('500');
});
it('can add balances on multiple accounts', async function () {
await this.contract.callTransfer(payee1, 200, { from: payer });
await this.contract.callTransfer(payee2, 300, { from: payer });
(await this.contract.payments(payee1)).should.be.bignumber.equal(200);
(await this.contract.payments(payee1)).should.be.bignumber.equal('200');
(await this.contract.payments(payee2)).should.be.bignumber.equal(300);
(await this.contract.payments(payee2)).should.be.bignumber.equal('300');
});
it('can withdraw payment', async function () {
(await balanceDifference(payee1, async () => {
(await balance.difference(payee1, async () => {
await this.contract.callTransfer(payee1, amount, { from: payer });
(await this.contract.payments(payee1)).should.be.bignumber.equal(amount);
await this.contract.withdrawPayments(payee1);
})).should.be.bignumber.equal(amount);
(await this.contract.payments(payee1)).should.be.bignumber.equal(0);
(await this.contract.payments(payee1)).should.be.bignumber.equal('0');
});
});

View File

@ -1,10 +1,6 @@
const { ether, shouldFail } = require('openzeppelin-test-helpers');
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
const shouldFail = require('../../helpers/shouldFail');
const { ether } = require('../../helpers/ether');
require('../../helpers/setup');
const ConditionalEscrowMock = artifacts.require('ConditionalEscrowMock');
contract('ConditionalEscrow', function ([_, owner, payee, ...otherAccounts]) {
@ -21,7 +17,7 @@ contract('ConditionalEscrow', function ([_, owner, payee, ...otherAccounts]) {
});
context('when withdrawal is disallowed', function () {
const amount = ether(23.0);
const amount = ether('2.3', 'ether');
beforeEach(async function () {
await this.escrow.setAllowed(payee, false);

View File

@ -1,20 +1,14 @@
const expectEvent = require('../../helpers/expectEvent');
const shouldFail = require('../../helpers/shouldFail');
const { ethGetBalance } = require('../../helpers/web3');
const { balanceDifference } = require('../../helpers/balanceDifference');
const { ether } = require('../../helpers/ether');
require('../../helpers/setup');
const { balance, ether, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
const amount = ether(42.0);
const amount = ether('4.2', 'ether');
describe('as an escrow', function () {
describe('deposits', function () {
it('can accept a single deposit', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount);
(await balance.current(this.escrow.address)).should.be.bignumber.equal(amount);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
});
@ -37,34 +31,34 @@ function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
it('can add multiple deposits on a single account', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.deposit(payee1, { from: primary, value: amount * 2 });
await this.escrow.deposit(payee1, { from: primary, value: amount.muln(2) });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
(await balance.current(this.escrow.address)).should.be.bignumber.equal(amount.muln(3));
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount * 3);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount.muln(3));
});
it('can track deposits to multiple accounts', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.deposit(payee2, { from: primary, value: amount * 2 });
await this.escrow.deposit(payee2, { from: primary, value: amount.muln(2) });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
(await balance.current(this.escrow.address)).should.be.bignumber.equal(amount.muln(3));
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
(await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount * 2);
(await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount.muln(2));
});
});
describe('withdrawals', async function () {
it('can withdraw payments', async function () {
(await balanceDifference(payee1, async () => {
(await balance.difference(payee1, async () => {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.withdraw(payee1, { from: primary });
})).should.be.bignumber.equal(amount);
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
(await balance.current(this.escrow.address)).should.be.bignumber.equal('0');
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal('0');
});
it('can do an empty withdrawal', async function () {

View File

@ -1,3 +1,4 @@
require('openzeppelin-test-helpers');
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
const Escrow = artifacts.require('Escrow');

View File

@ -1,20 +1,14 @@
const shouldFail = require('../../helpers/shouldFail');
const expectEvent = require('../../helpers/expectEvent');
const { balanceDifference } = require('../../helpers/balanceDifference');
const { ether } = require('../../helpers/ether');
const { ZERO_ADDRESS } = require('../../helpers/constants');
require('../../helpers/setup');
const { balance, constants, ether, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
const RefundEscrow = artifacts.require('RefundEscrow');
contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) {
const amount = ether(54.0);
const amount = ether('54', 'ether');
const refundees = [refundee1, refundee2];
it('requires a non-null beneficiary', async function () {
await shouldFail.reverting(
RefundEscrow.new(ZERO_ADDRESS, { from: primary })
RefundEscrow.new(constants.ZERO_ADDRESS, { from: primary })
);
});
@ -26,7 +20,7 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee
context('active state', function () {
it('has beneficiary and state', async function () {
(await this.escrow.beneficiary()).should.be.equal(beneficiary);
(await this.escrow.state()).should.be.bignumber.equal(0);
(await this.escrow.state()).should.be.bignumber.equal('0');
});
it('accepts deposits', async function () {
@ -69,9 +63,9 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee
});
it('allows beneficiary withdrawal', async function () {
(await balanceDifference(beneficiary, () =>
(await balance.difference(beneficiary, () =>
this.escrow.beneficiaryWithdraw()
)).should.be.bignumber.equal(amount * refundees.length);
)).should.be.bignumber.equal(amount.muln(refundees.length));
});
it('prevents entering the refund state', async function () {
@ -103,7 +97,7 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee
it('refunds refundees', async function () {
for (const refundee of [refundee1, refundee2]) {
(await balanceDifference(refundee, () =>
(await balance.difference(refundee, () =>
this.escrow.withdraw(refundee, { from: primary }))
).should.be.bignumber.equal(amount);
}

View File

@ -1,8 +1,8 @@
require('openzeppelin-test-helpers');
const AddressImpl = artifacts.require('AddressImpl');
const SimpleToken = artifacts.require('SimpleToken');
require('../helpers/setup');
contract('Address', function ([_, anyone]) {
beforeEach(async function () {
this.mock = await AddressImpl.new();

View File

@ -1,6 +1,6 @@
const ArraysImpl = artifacts.require('ArraysImpl');
require('openzeppelin-test-helpers');
require('../helpers/setup');
const ArraysImpl = artifacts.require('ArraysImpl');
contract('Arrays', function () {
context('Even number of elements', function () {
@ -11,23 +11,23 @@ contract('Arrays', function () {
});
it('should return correct index for the basic case', async function () {
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal('5');
});
it('should return 0 for the first element', async function () {
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal('0');
});
it('should return index of the last element', async function () {
(await this.arrays.findUpperBound(20)).should.be.bignumber.equal(9);
(await this.arrays.findUpperBound(20)).should.be.bignumber.equal('9');
});
it('should return first index after last element if searched value is over the upper boundary', async function () {
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal(10);
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal('10');
});
it('should return 0 for the element under the lower boundary', async function () {
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal(0);
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal('0');
});
});
@ -39,23 +39,23 @@ contract('Arrays', function () {
});
it('should return correct index for the basic case', async function () {
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
(await this.arrays.findUpperBound(16)).should.be.bignumber.equal('5');
});
it('should return 0 for the first element', async function () {
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
(await this.arrays.findUpperBound(11)).should.be.bignumber.equal('0');
});
it('should return index of the last element', async function () {
(await this.arrays.findUpperBound(21)).should.be.bignumber.equal(10);
(await this.arrays.findUpperBound(21)).should.be.bignumber.equal('10');
});
it('should return first index after last element if searched value is over the upper boundary', async function () {
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal(11);
(await this.arrays.findUpperBound(32)).should.be.bignumber.equal('11');
});
it('should return 0 for the element under the lower boundary', async function () {
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal(0);
(await this.arrays.findUpperBound(2)).should.be.bignumber.equal('0');
});
});
@ -67,7 +67,7 @@ contract('Arrays', function () {
});
it('should return index of first element in next filled range', async function () {
(await this.arrays.findUpperBound(17)).should.be.bignumber.equal(5);
(await this.arrays.findUpperBound(17)).should.be.bignumber.equal('5');
});
});
@ -77,7 +77,7 @@ contract('Arrays', function () {
});
it('should always return 0 for empty array', async function () {
(await this.arrays.findUpperBound(10)).should.be.bignumber.equal(0);
(await this.arrays.findUpperBound(10)).should.be.bignumber.equal('0');
});
});
});

View File

@ -1,13 +1,12 @@
const shouldFail = require('../helpers/shouldFail');
const { shouldFail } = require('openzeppelin-test-helpers');
const ReentrancyMock = artifacts.require('ReentrancyMock');
const ReentrancyAttack = artifacts.require('ReentrancyAttack');
require('../helpers/setup');
contract('ReentrancyGuard', function () {
beforeEach(async function () {
this.reentrancyMock = await ReentrancyMock.new();
(await this.reentrancyMock.counter()).should.be.bignumber.equal(0);
(await this.reentrancyMock.counter()).should.be.bignumber.equal('0');
});
it('should not allow remote callback', async function () {