Skip to content

Commit

Permalink
All tests now use account names, and dont use accounts[0] (except ERC…
Browse files Browse the repository at this point in the history
…721)
  • Loading branch information
nventuro committed Aug 1, 2018
1 parent 567b773 commit f7b431b
Show file tree
Hide file tree
Showing 37 changed files with 190 additions and 265 deletions.
7 changes: 4 additions & 3 deletions test/examples/SampleCrowdsale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require('chai')
const SampleCrowdsale = artifacts.require('SampleCrowdsale');
const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');

contract('SampleCrowdsale', function ([owner, wallet, investor]) {
contract('SampleCrowdsale', function ([_, owner, wallet, investor]) {
const RATE = new BigNumber(10);
const GOAL = ether(10);
const CAP = ether(20);
Expand All @@ -33,9 +33,10 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {

this.token = await SampleCrowdsaleToken.new({ from: owner });
this.crowdsale = await SampleCrowdsale.new(
this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL
this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL,
{ from: owner }
);
await this.token.transferOwnership(this.crowdsale.address);
await this.token.transferOwnership(this.crowdsale.address, { from: owner });
});

it('should create crowdsale with correct parameters', async function () {
Expand Down
3 changes: 1 addition & 2 deletions test/examples/SimpleToken.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { decodeLogs } = require('../helpers/decodeLogs');
const SimpleToken = artifacts.require('SimpleToken');

contract('SimpleToken', accounts => {
contract('SimpleToken', function ([_, creator]) {
let token;
const creator = accounts[0];

beforeEach(async function () {
token = await SimpleToken.new({ from: creator });
Expand Down
2 changes: 1 addition & 1 deletion test/introspection/SupportsInterfaceWithLookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const SupportsInterfaceWithLookup = artifacts.require('SupportsInterfaceWithLook
require('chai')
.should();

contract('SupportsInterfaceWithLookup', function (accounts) {
contract('SupportsInterfaceWithLookup', function () {
beforeEach(async function () {
this.mock = await SupportsInterfaceWithLookup.new();
});
Expand Down
12 changes: 6 additions & 6 deletions test/library/ECRecovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ECRecoveryMock = artifacts.require('ECRecoveryMock');
require('chai')
.should();

contract('ECRecovery', function (accounts) {
contract('ECRecovery', function ([_, anyone]) {
let ecrecovery;
const TEST_MESSAGE = 'OpenZeppelin';

Expand Down Expand Up @@ -36,28 +36,28 @@ contract('ECRecovery', function (accounts) {

it('recover using web3.eth.sign()', async function () {
// Create the signature using account[0]
const signature = signMessage(accounts[0], web3.sha3(TEST_MESSAGE));
const signature = signMessage(anyone, web3.sha3(TEST_MESSAGE));

// Recover the signer address from the generated message and signature.
const addrRecovered = await ecrecovery.recover(
hashMessage(TEST_MESSAGE),
signature
);
addrRecovered.should.eq(accounts[0]);
addrRecovered.should.eq(anyone);
});

it('recover using web3.eth.sign() should return wrong signer', async function () {
// Create the signature using account[0]
const signature = signMessage(accounts[0], web3.sha3(TEST_MESSAGE));
const signature = signMessage(anyone, web3.sha3(TEST_MESSAGE));

// Recover the signer address from the generated message and wrong signature.
const addrRecovered = await ecrecovery.recover(hashMessage('Nope'), signature);
assert.notEqual(accounts[0], addrRecovered);
assert.notEqual(anyone, addrRecovered);
});

it('recover should revert when a small hash is sent', async function () {
// Create the signature using account[0]
const signature = signMessage(accounts[0], TEST_MESSAGE);
const signature = signMessage(anyone, TEST_MESSAGE);
try {
await expectThrow(
ecrecovery.recover(hashMessage(TEST_MESSAGE).substring(2), signature)
Expand Down
2 changes: 1 addition & 1 deletion test/library/Math.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const MathMock = artifacts.require('MathMock');

contract('Math', function (accounts) {
contract('Math', function () {
let math;

beforeEach(async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/library/MerkleProof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { sha3, bufferToHex } = require('ethereumjs-util');

const MerkleProofWrapper = artifacts.require('MerkleProofWrapper');

contract('MerkleProof', function (accounts) {
contract('MerkleProof', function () {
let merkleProof;

beforeEach(async function () {
Expand Down
20 changes: 9 additions & 11 deletions test/lifecycle/Destructible.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
const DestructibleMock = artifacts.require('DestructibleMock');
const { ethGetBalance } = require('../helpers/web3');

contract('Destructible', function (accounts) {
contract('Destructible', function ([_, owner, recipient]) {
beforeEach(async function () {
this.destructible = await DestructibleMock.new({ from: accounts[0] });
this.destructible = await DestructibleMock.new({ from: owner });
await web3.eth.sendTransaction({
from: accounts[0],
from: owner,
to: this.destructible.address,
value: web3.toWei('10', 'ether'),
});

this.owner = await this.destructible.owner();
});

it('should send balance to owner after destruction', async function () {
const initBalance = await ethGetBalance(this.owner);
await this.destructible.destroy({ from: this.owner });
const newBalance = await ethGetBalance(this.owner);
const initBalance = await ethGetBalance(owner);
await this.destructible.destroy({ from: owner });
const newBalance = await ethGetBalance(owner);
assert.isTrue(newBalance > initBalance);
});

it('should send balance to recepient after destruction', async function () {
const initBalance = await ethGetBalance(accounts[1]);
await this.destructible.destroyAndSend(accounts[1], { from: this.owner });
const newBalance = await ethGetBalance(accounts[1]);
const initBalance = await ethGetBalance(recipient);
await this.destructible.destroyAndSend(recipient, { from: owner });
const newBalance = await ethGetBalance(recipient);
assert.isTrue(newBalance.greaterThan(initBalance));
});
});
2 changes: 1 addition & 1 deletion test/lifecycle/Pausable.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assertRevert } = require('../helpers/assertRevert');
const PausableMock = artifacts.require('PausableMock');

contract('Pausable', function (accounts) {
contract('Pausable', function () {
beforeEach(async function () {
this.Pausable = await PausableMock.new();
});
Expand Down
7 changes: 2 additions & 5 deletions test/lifecycle/TokenDestructible.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ const { ethGetBalance } = require('../helpers/web3');
const TokenDestructible = artifacts.require('TokenDestructible');
const StandardTokenMock = artifacts.require('StandardTokenMock');

contract('TokenDestructible', function (accounts) {
contract('TokenDestructible', function ([_, owner]) {
let tokenDestructible;
let owner;

beforeEach(async function () {
tokenDestructible = await TokenDestructible.new({
from: accounts[0],
from: owner,
value: web3.toWei('10', 'ether'),
});

owner = await tokenDestructible.owner();
});

it('should send balance to owner after destruction', async function () {
Expand Down
16 changes: 8 additions & 8 deletions test/ownership/CanReclaimToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ const { expectThrow } = require('../helpers/expectThrow');
const CanReclaimToken = artifacts.require('CanReclaimToken');
const BasicTokenMock = artifacts.require('BasicTokenMock');

contract('CanReclaimToken', function (accounts) {
contract('CanReclaimToken', function ([_, owner, anyone]) {
let token = null;
let canReclaimToken = null;

beforeEach(async function () {
// Create contract and token
token = await BasicTokenMock.new(accounts[0], 100);
canReclaimToken = await CanReclaimToken.new();
token = await BasicTokenMock.new(owner, 100, { from: owner });
canReclaimToken = await CanReclaimToken.new({ from: owner });
// Force token into contract
await token.transfer(canReclaimToken.address, 10);
await token.transfer(canReclaimToken.address, 10, { from: owner });
const startBalance = await token.balanceOf(canReclaimToken.address);
assert.equal(startBalance, 10);
});

it('should allow owner to reclaim tokens', async function () {
const ownerStartBalance = await token.balanceOf(accounts[0]);
await canReclaimToken.reclaimToken(token.address);
const ownerFinalBalance = await token.balanceOf(accounts[0]);
const ownerStartBalance = await token.balanceOf(owner);
await canReclaimToken.reclaimToken(token.address, { from: owner });
const ownerFinalBalance = await token.balanceOf(owner);
const finalBalance = await token.balanceOf(canReclaimToken.address);
assert.equal(finalBalance, 0);
assert.equal(ownerFinalBalance - ownerStartBalance, 10);
});

it('should allow only owner to reclaim tokens', async function () {
await expectThrow(
canReclaimToken.reclaimToken(token.address, { from: accounts[1] }),
canReclaimToken.reclaimToken(token.address, { from: anyone })
);
});
});
25 changes: 5 additions & 20 deletions test/ownership/Claimable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,36 @@ const { assertRevert } = require('../helpers/assertRevert');

const Claimable = artifacts.require('Claimable');

contract('Claimable', function (accounts) {
contract('Claimable', function ([_, owner, newOwner, anyone]) {
let claimable;

beforeEach(async function () {
claimable = await Claimable.new();
});

it('should have an owner', async function () {
const owner = await claimable.owner();
assert.isTrue(owner !== 0);
});

it('changes pendingOwner after transfer', async function () {
const newOwner = accounts[1];
await claimable.transferOwnership(newOwner);
const pendingOwner = await claimable.pendingOwner();

assert.isTrue(pendingOwner === newOwner);
});

it('should prevent to claimOwnership from no pendingOwner', async function () {
await assertRevert(claimable.claimOwnership({ from: accounts[2] }));
it('should prevent to claimOwnership from anyone', async function () {
await assertRevert(claimable.claimOwnership({ from: anyone }));
});

it('should prevent non-owners from transfering', async function () {
const other = accounts[2];
const owner = await claimable.owner.call();

assert.isTrue(owner !== other);
await assertRevert(claimable.transferOwnership(other, { from: other }));
await assertRevert(claimable.transferOwnership(anyone, { from: anyone }));
});

describe('after initiating a transfer', function () {
let newOwner;

beforeEach(async function () {
newOwner = accounts[1];
await claimable.transferOwnership(newOwner);
});

it('changes allow pending owner to claim ownership', async function () {
await claimable.claimOwnership({ from: newOwner });
const owner = await claimable.owner();

assert.isTrue(owner === newOwner);
assert.isTrue((await claimable.owner()) === newOwner);
});
});
});
2 changes: 1 addition & 1 deletion test/ownership/Contactable.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Contactable = artifacts.require('Contactable');

contract('Contactable', function (accounts) {
contract('Contactable', function () {
let contactable;

beforeEach(async function () {
Expand Down
36 changes: 16 additions & 20 deletions test/ownership/DelayedClaimable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,46 @@ const { assertRevert } = require('../helpers/assertRevert');

const DelayedClaimable = artifacts.require('DelayedClaimable');

contract('DelayedClaimable', function (accounts) {
contract('DelayedClaimable', function ([_, owner, newOwner]) {
beforeEach(async function () {
this.delayedClaimable = await DelayedClaimable.new();
this.delayedClaimable = await DelayedClaimable.new({ from: owner });
});

it('can set claim blocks', async function () {
await this.delayedClaimable.transferOwnership(accounts[2]);
await this.delayedClaimable.setLimits(0, 1000);
await this.delayedClaimable.transferOwnership(newOwner, { from: owner });
await this.delayedClaimable.setLimits(0, 1000, { from: owner });
const end = await this.delayedClaimable.end();
assert.equal(end, 1000);
const start = await this.delayedClaimable.start();
assert.equal(start, 0);
});

it('changes pendingOwner after transfer successful', async function () {
await this.delayedClaimable.transferOwnership(accounts[2]);
await this.delayedClaimable.setLimits(0, 1000);
await this.delayedClaimable.transferOwnership(newOwner, { from: owner });
await this.delayedClaimable.setLimits(0, 1000, { from: owner });
const end = await this.delayedClaimable.end();
assert.equal(end, 1000);
const start = await this.delayedClaimable.start();
assert.equal(start, 0);
const pendingOwner = await this.delayedClaimable.pendingOwner();
assert.equal(pendingOwner, accounts[2]);
await this.delayedClaimable.claimOwnership({ from: accounts[2] });
const owner = await this.delayedClaimable.owner();
assert.equal(owner, accounts[2]);
assert.equal((await this.delayedClaimable.pendingOwner()), newOwner);
await this.delayedClaimable.claimOwnership({ from: newOwner });
assert.equal((await this.delayedClaimable.owner()), newOwner);
});

it('changes pendingOwner after transfer fails', async function () {
await this.delayedClaimable.transferOwnership(accounts[1]);
await this.delayedClaimable.setLimits(100, 110);
await this.delayedClaimable.transferOwnership(newOwner, { from: owner });
await this.delayedClaimable.setLimits(100, 110, { from: owner });
const end = await this.delayedClaimable.end();
assert.equal(end, 110);
const start = await this.delayedClaimable.start();
assert.equal(start, 100);
const pendingOwner = await this.delayedClaimable.pendingOwner();
assert.equal(pendingOwner, accounts[1]);
await assertRevert(this.delayedClaimable.claimOwnership({ from: accounts[1] }));
const owner = await this.delayedClaimable.owner();
assert.isTrue(owner !== accounts[1]);
assert.equal((await this.delayedClaimable.pendingOwner()), newOwner);
await assertRevert(this.delayedClaimable.claimOwnership({ from: newOwner }));
assert.isTrue((await this.delayedClaimable.owner()) !== newOwner);
});

it('set end and start invalid values fail', async function () {
await this.delayedClaimable.transferOwnership(accounts[1]);
await assertRevert(this.delayedClaimable.setLimits(1001, 1000));
await this.delayedClaimable.transferOwnership(newOwner, { from: owner });
await assertRevert(this.delayedClaimable.setLimits(1001, 1000, { from: owner }));
});
});
17 changes: 7 additions & 10 deletions test/ownership/HasNoContracts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ const { expectThrow } = require('../helpers/expectThrow');
const Ownable = artifacts.require('Ownable');
const HasNoContracts = artifacts.require('HasNoContracts');

contract('HasNoContracts', function (accounts) {
contract('HasNoContracts', function ([_, owner, anyone]) {
let hasNoContracts = null;
let ownable = null;

beforeEach(async () => {
// Create contract and token
hasNoContracts = await HasNoContracts.new();
ownable = await Ownable.new();
hasNoContracts = await HasNoContracts.new({ from: owner });
ownable = await Ownable.new({ from: owner });

// Force ownership into contract
await ownable.transferOwnership(hasNoContracts.address);
const owner = await ownable.owner();
assert.equal(owner, hasNoContracts.address);
await ownable.transferOwnership(hasNoContracts.address, { from: owner });
});

it('should allow owner to reclaim contracts', async function () {
await hasNoContracts.reclaimContract(ownable.address);
const owner = await ownable.owner();
assert.equal(owner, accounts[0]);
await hasNoContracts.reclaimContract(ownable.address, { from: owner });
assert.equal((await ownable.owner()), owner);
});

it('should allow only owner to reclaim contracts', async function () {
await expectThrow(
hasNoContracts.reclaimContract(ownable.address, { from: accounts[1] }),
hasNoContracts.reclaimContract(ownable.address, { from: anyone })
);
});
});
Loading

0 comments on commit f7b431b

Please sign in to comment.