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
#1137)

* All tests now use account names, and dont use accounts[0] (except ERC721)

* Added account names to some missing contracts.
  • Loading branch information
nventuro authored Aug 2, 2018
1 parent f497215 commit 4544df4
Show file tree
Hide file tree
Showing 41 changed files with 211 additions and 301 deletions.
35 changes: 10 additions & 25 deletions test/Heritable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';

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

contract('Heritable', function (accounts) {
contract('Heritable', function ([_, owner, heir, anyone]) {
let heritable;
let owner;

beforeEach(async function () {
heritable = await Heritable.new(4141);
owner = await heritable.owner();
heritable = await Heritable.new(4141, { from: owner });
});

it('should start off with an owner, but without heir', async function () {
Expand All @@ -31,31 +29,23 @@ contract('Heritable', function (accounts) {
});

it('only owner should set heir', async function () {
const newHeir = accounts[1];
const someRandomAddress = accounts[2];
assert.isTrue(owner !== someRandomAddress);

await heritable.setHeir(newHeir, { from: owner });
await expectThrow(heritable.setHeir(newHeir, { from: someRandomAddress }));
await heritable.setHeir(heir, { from: owner });
await expectThrow(heritable.setHeir(heir, { from: anyone }));
});

it('owner can\'t be heir', async function () {
await assertRevert(heritable.setHeir(owner, { from: owner }));
});

it('owner can remove heir', async function () {
const newHeir = accounts[1];
await heritable.setHeir(newHeir, { from: owner });
let heir = await heritable.heir();

assert.notStrictEqual(heir, NULL_ADDRESS);
await heritable.removeHeir();
heir = await heritable.heir();
assert.isTrue(heir === NULL_ADDRESS);
await heritable.setHeir(heir, { from: owner });
assert.equal(await heritable.heir(), heir);

await heritable.removeHeir({ from: owner });
assert.equal(await heritable.heir(), NULL_ADDRESS);
});

it('heir can claim ownership only if owner is dead and timeout was reached', async function () {
const heir = accounts[1];
await heritable.setHeir(heir, { from: owner });
await expectThrow(heritable.claimHeirOwnership({ from: heir }));

Expand All @@ -69,20 +59,17 @@ contract('Heritable', function (accounts) {
});

it('only heir can proclaim death', async function () {
const someRandomAddress = accounts[2];
await assertRevert(heritable.proclaimDeath({ from: owner }));
await assertRevert(heritable.proclaimDeath({ from: someRandomAddress }));
await assertRevert(heritable.proclaimDeath({ from: anyone }));
});

it('heir can\'t proclaim death if owner is death', async function () {
const heir = accounts[1];
await heritable.setHeir(heir, { from: owner });
await heritable.proclaimDeath({ from: heir });
await assertRevert(heritable.proclaimDeath({ from: heir }));
});

it('heir can\'t claim ownership if owner heartbeats', async function () {
const heir = accounts[1];
await heritable.setHeir(heir, { from: owner });

await heritable.proclaimDeath({ from: heir });
Expand All @@ -96,8 +83,6 @@ contract('Heritable', function (accounts) {
});

it('should log events appropriately', async function () {
const heir = accounts[1];

const setHeirLogs = (await heritable.setHeir(heir, { from: owner })).logs;
const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged');

Expand Down
2 changes: 1 addition & 1 deletion test/LimitBalance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { ethGetBalance } = require('./helpers/web3');

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

contract('LimitBalance', function (accounts) {
contract('LimitBalance', function () {
let limitBalance;

beforeEach(async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/ReentrancyGuard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { expectThrow } = require('./helpers/expectThrow');
const ReentrancyMock = artifacts.require('ReentrancyMock');
const ReentrancyAttack = artifacts.require('ReentrancyAttack');

contract('ReentrancyGuard', function (accounts) {
contract('ReentrancyGuard', function () {
let reentrancyMock;

beforeEach(async function () {
Expand Down
17 changes: 8 additions & 9 deletions test/SimpleSavingsWallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');

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

contract('SimpleSavingsWallet', function (accounts) {
contract('SimpleSavingsWallet', function ([_, owner, anyone]) {
let savingsWallet;
let owner;

const paymentAmount = 4242;

beforeEach(async function () {
savingsWallet = await SimpleSavingsWallet.new(4141);
owner = await savingsWallet.owner();
savingsWallet = await SimpleSavingsWallet.new(4141, { from: owner });
});

it('should receive funds', async function () {
Expand All @@ -22,14 +20,15 @@ contract('SimpleSavingsWallet', function (accounts) {

it('owner can send funds', async function () {
// Receive payment so we have some money to spend.
await ethSendTransaction({ from: accounts[9], to: savingsWallet.address, value: 1000000 });
await ethSendTransaction({ from: anyone, to: savingsWallet.address, value: 1000000 });

await expectThrow(savingsWallet.sendTo(0, paymentAmount, { from: owner }));
await expectThrow(savingsWallet.sendTo(savingsWallet.address, paymentAmount, { from: owner }));
await expectThrow(savingsWallet.sendTo(accounts[1], 0, { from: owner }));
await expectThrow(savingsWallet.sendTo(anyone, 0, { from: owner }));

const balance = await ethGetBalance(accounts[1]);
await savingsWallet.sendTo(accounts[1], paymentAmount, { from: owner });
const updatedBalance = await ethGetBalance(accounts[1]);
const balance = await ethGetBalance(anyone);
await savingsWallet.sendTo(anyone, paymentAmount, { from: owner });
const updatedBalance = await ethGetBalance(anyone);
assert.isTrue(balance.plus(paymentAmount).equals(updatedBalance));
});
});
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 })
);
});
});
Loading

0 comments on commit 4544df4

Please sign in to comment.