mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
openzeppelin-contracts/test/ownership/HasNoEther.test.js

64 lines
2.1 KiB

const { expectThrow } = require('../helpers/expectThrow');
const { ethSendTransaction, ethGetBalance } = require('../helpers/web3');
const HasNoEtherTest = artifacts.require('HasNoEtherTest');
const ForceEther = artifacts.require('ForceEther');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
contract('HasNoEther', function ([_, owner, anyone]) {
const amount = web3.toWei('1', 'ether');
beforeEach(async function () {
this.hasNoEther = await HasNoEtherTest.new({ from: owner });
});
it('should not accept ether in constructor', async function () {
await expectThrow(HasNoEtherTest.new({ value: amount }));
});
it('should not accept ether', async function () {
await expectThrow(
ethSendTransaction({
from: owner,
to: this.hasNoEther.address,
value: amount,
}),
);
});
it('should allow owner to reclaim ether', async function () {
const startBalance = await ethGetBalance(this.hasNoEther.address);
startBalance.should.be.bignumber.equal(0);
// Force ether into it
const forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(this.hasNoEther.address);
const forcedBalance = await ethGetBalance(this.hasNoEther.address);
forcedBalance.should.be.bignumber.equal(amount);
// Reclaim
const ownerStartBalance = await ethGetBalance(owner);
await this.hasNoEther.reclaimEther({ from: owner });
const ownerFinalBalance = await ethGetBalance(owner);
const finalBalance = await ethGetBalance(this.hasNoEther.address);
finalBalance.should.be.bignumber.equal(0);
ownerFinalBalance.should.be.bignumber.gt(ownerStartBalance);
});
it('should allow only owner to reclaim ether', async function () {
// Force ether into it
const forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(this.hasNoEther.address);
const forcedBalance = await ethGetBalance(this.hasNoEther.address);
forcedBalance.should.be.bignumber.equal(amount);
// Reclaim
await expectThrow(this.hasNoEther.reclaimEther({ from: anyone }));
});
});