Organize test files following contracts folders structure

This commit is contained in:
Facundo Spagnuolo
2018-01-09 12:34:29 -03:00
parent 7532dab17d
commit b925b2dae6
31 changed files with 55 additions and 56 deletions

View File

@ -0,0 +1,35 @@
import expectThrow from '../helpers/expectThrow';
const CanReclaimToken = artifacts.require('../contracts/ownership/CanReclaimToken.sol');
const BasicTokenMock = artifacts.require('mocks/BasicTokenMock.sol');
contract('CanReclaimToken', function (accounts) {
let token = null;
let canReclaimToken = null;
beforeEach(async function () {
// Create contract and token
token = await BasicTokenMock.new(accounts[0], 100);
canReclaimToken = await CanReclaimToken.new();
// Force token into contract
await token.transfer(canReclaimToken.address, 10);
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 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] }),
);
});
});

View File

@ -0,0 +1,53 @@
import assertRevert from '../helpers/assertRevert';
var Claimable = artifacts.require('../contracts/ownership/Claimable.sol');
contract('Claimable', function (accounts) {
let claimable;
beforeEach(async function () {
claimable = await Claimable.new();
});
it('should have an owner', async function () {
let owner = await claimable.owner();
assert.isTrue(owner !== 0);
});
it('changes pendingOwner after transfer', async function () {
let newOwner = accounts[1];
await claimable.transferOwnership(newOwner);
let 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 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 }));
});
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 });
let owner = await claimable.owner();
assert.isTrue(owner === newOwner);
});
});
});

View File

@ -0,0 +1,28 @@
var Contactable = artifacts.require('../contracts/ownership/Contactable.sol');
contract('Contactable', function (accounts) {
let contactable;
beforeEach(async function () {
contactable = await Contactable.new();
});
it('should have an empty contact info', async function () {
let info = await contactable.contactInformation();
assert.isTrue(info === '');
});
describe('after setting the contact information', function () {
let contactInfo = 'contact information';
beforeEach(async function () {
await contactable.setContactInformation(contactInfo);
});
it('should return the setted contact information', async function () {
let info = await contactable.contactInformation();
assert.isTrue(info === contactInfo);
});
});
});

View File

@ -0,0 +1,55 @@
import assertRevert from '../helpers/assertRevert';
var DelayedClaimable = artifacts.require('../contracts/ownership/DelayedClaimable.sol');
contract('DelayedClaimable', function (accounts) {
var delayedClaimable;
beforeEach(function () {
return DelayedClaimable.new().then(function (deployed) {
delayedClaimable = deployed;
});
});
it('can set claim blocks', async function () {
await delayedClaimable.transferOwnership(accounts[2]);
await delayedClaimable.setLimits(0, 1000);
let end = await delayedClaimable.end();
assert.equal(end, 1000);
let start = await delayedClaimable.start();
assert.equal(start, 0);
});
it('changes pendingOwner after transfer successful', async function () {
await delayedClaimable.transferOwnership(accounts[2]);
await delayedClaimable.setLimits(0, 1000);
let end = await delayedClaimable.end();
assert.equal(end, 1000);
let start = await delayedClaimable.start();
assert.equal(start, 0);
let pendingOwner = await delayedClaimable.pendingOwner();
assert.equal(pendingOwner, accounts[2]);
await delayedClaimable.claimOwnership({ from: accounts[2] });
let owner = await delayedClaimable.owner();
assert.equal(owner, accounts[2]);
});
it('changes pendingOwner after transfer fails', async function () {
await delayedClaimable.transferOwnership(accounts[1]);
await delayedClaimable.setLimits(100, 110);
let end = await delayedClaimable.end();
assert.equal(end, 110);
let start = await delayedClaimable.start();
assert.equal(start, 100);
let pendingOwner = await delayedClaimable.pendingOwner();
assert.equal(pendingOwner, accounts[1]);
await assertRevert(delayedClaimable.claimOwnership({ from: accounts[1] }));
let owner = await delayedClaimable.owner();
assert.isTrue(owner !== accounts[1]);
});
it('set end and start invalid values fail', async function () {
await delayedClaimable.transferOwnership(accounts[1]);
await assertRevert(delayedClaimable.setLimits(1001, 1000));
});
});

View File

@ -0,0 +1,35 @@
import expectThrow from '../helpers/expectThrow';
const Ownable = artifacts.require('../contracts/ownership/Ownable.sol');
const HasNoContracts = artifacts.require(
'../contracts/ownership/HasNoContracts.sol',
);
contract('HasNoContracts', function (accounts) {
let hasNoContracts = null;
let ownable = null;
beforeEach(async () => {
// Create contract and token
hasNoContracts = await HasNoContracts.new();
ownable = await Ownable.new();
// Force ownership into contract
await ownable.transferOwnership(hasNoContracts.address);
const owner = await ownable.owner();
assert.equal(owner, hasNoContracts.address);
});
it('should allow owner to reclaim contracts', async function () {
await hasNoContracts.reclaimContract(ownable.address);
const owner = await ownable.owner();
assert.equal(owner, accounts[0]);
});
it('should allow only owner to reclaim contracts', async function () {
await expectThrow(
hasNoContracts.reclaimContract(ownable.address, { from: accounts[1] }),
);
});
});

View File

@ -0,0 +1,64 @@
import expectThrow from '../helpers/expectThrow';
import toPromise from '../helpers/toPromise';
const HasNoEtherTest = artifacts.require('../mocks/HasNoEtherTest.sol');
const ForceEther = artifacts.require('../mocks/ForceEther.sol');
contract('HasNoEther', function (accounts) {
const amount = web3.toWei('1', 'ether');
it('should be constructorable', async function () {
await HasNoEtherTest.new();
});
it('should not accept ether in constructor', async function () {
await expectThrow(HasNoEtherTest.new({ value: amount }));
});
it('should not accept ether', async function () {
let hasNoEther = await HasNoEtherTest.new();
await expectThrow(
toPromise(web3.eth.sendTransaction)({
from: accounts[1],
to: hasNoEther.address,
value: amount,
}),
);
});
it('should allow owner to reclaim ether', async function () {
// Create contract
let hasNoEther = await HasNoEtherTest.new();
const startBalance = await web3.eth.getBalance(hasNoEther.address);
assert.equal(startBalance, 0);
// Force ether into it
let forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(hasNoEther.address);
const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
assert.equal(forcedBalance, amount);
// Reclaim
const ownerStartBalance = await web3.eth.getBalance(accounts[0]);
await hasNoEther.reclaimEther();
const ownerFinalBalance = await web3.eth.getBalance(accounts[0]);
const finalBalance = await web3.eth.getBalance(hasNoEther.address);
assert.equal(finalBalance, 0);
assert.isAbove(ownerFinalBalance, ownerStartBalance);
});
it('should allow only owner to reclaim ether', async function () {
// Create contract
let hasNoEther = await HasNoEtherTest.new({ from: accounts[0] });
// Force ether into it
let forceEther = await ForceEther.new({ value: amount });
await forceEther.destroyAndSend(hasNoEther.address);
const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
assert.equal(forcedBalance, amount);
// Reclaim
await expectThrow(hasNoEther.reclaimEther({ from: accounts[1] }));
});
});

View File

@ -0,0 +1,40 @@
import expectThrow from '../helpers/expectThrow';
const HasNoTokens = artifacts.require('../contracts/lifecycle/HasNoTokens.sol');
const ERC23TokenMock = artifacts.require('mocks/ERC23TokenMock.sol');
contract('HasNoTokens', function (accounts) {
let hasNoTokens = null;
let token = null;
beforeEach(async () => {
// Create contract and token
hasNoTokens = await HasNoTokens.new();
token = await ERC23TokenMock.new(accounts[0], 100);
// Force token into contract
await token.transfer(hasNoTokens.address, 10);
const startBalance = await token.balanceOf(hasNoTokens.address);
assert.equal(startBalance, 10);
});
it('should not accept ERC23 tokens', async function () {
await expectThrow(token.transferERC23(hasNoTokens.address, 10, ''));
});
it('should allow owner to reclaim tokens', async function () {
const ownerStartBalance = await token.balanceOf(accounts[0]);
await hasNoTokens.reclaimToken(token.address);
const ownerFinalBalance = await token.balanceOf(accounts[0]);
const finalBalance = await token.balanceOf(hasNoTokens.address);
assert.equal(finalBalance, 0);
assert.equal(ownerFinalBalance - ownerStartBalance, 10);
});
it('should allow only owner to reclaim tokens', async function () {
await expectThrow(
hasNoTokens.reclaimToken(token.address, { from: accounts[1] }),
);
});
});

View File

@ -0,0 +1,37 @@
import assertRevert from '../helpers/assertRevert';
var Ownable = artifacts.require('../contracts/ownership/Ownable.sol');
contract('Ownable', function (accounts) {
let ownable;
beforeEach(async function () {
ownable = await Ownable.new();
});
it('should have an owner', async function () {
let owner = await ownable.owner();
assert.isTrue(owner !== 0);
});
it('changes owner after transfer', async function () {
let other = accounts[1];
await ownable.transferOwnership(other);
let owner = await ownable.owner();
assert.isTrue(owner === other);
});
it('should prevent non-owners from transfering', async function () {
const other = accounts[2];
const owner = await ownable.owner.call();
assert.isTrue(owner !== other);
await assertRevert(ownable.transferOwnership(other, { from: other }));
});
it('should guard ownership against stuck state', async function () {
let originalOwner = await ownable.owner();
await assertRevert(ownable.transferOwnership(null, { from: originalOwner }));
});
});

View File

@ -0,0 +1,98 @@
import expectThrow from '../../helpers/expectThrow';
import expectEvent from '../../helpers/expectEvent';
const RBACMock = artifacts.require('mocks/RBACMock.sol');
require('chai')
.use(require('chai-as-promised'))
.should();
const ROLE_ADVISOR = 'advisor';
contract('RBAC', function (accounts) {
let mock;
const [
admin,
anyone,
futureAdvisor,
...advisors
] = accounts;
before(async () => {
mock = await RBACMock.new(advisors, { from: admin });
});
context('in normal conditions', () => {
it('allows admin to call #onlyAdminsCanDoThis', async () => {
await mock.onlyAdminsCanDoThis({ from: admin })
.should.be.fulfilled;
});
it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
await mock.onlyAdvisorsCanDoThis({ from: admin })
.should.be.fulfilled;
});
it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
await mock.onlyAdvisorsCanDoThis({ from: advisors[0] })
.should.be.fulfilled;
});
it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
await mock.eitherAdminOrAdvisorCanDoThis({ from: admin })
.should.be.fulfilled;
});
it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
await mock.eitherAdminOrAdvisorCanDoThis({ from: advisors[0] })
.should.be.fulfilled;
});
it('does not allow admins to call #nobodyCanDoThis', async () => {
await expectThrow(
mock.nobodyCanDoThis({ from: admin })
);
});
it('does not allow advisors to call #nobodyCanDoThis', async () => {
await expectThrow(
mock.nobodyCanDoThis({ from: advisors[0] })
);
});
it('does not allow anyone to call #nobodyCanDoThis', async () => {
await expectThrow(
mock.nobodyCanDoThis({ from: anyone })
);
});
it('allows an admin to remove an advisor\'s role', async () => {
await mock.removeAdvisor(advisors[0], { from: admin })
.should.be.fulfilled;
});
it('allows admins to #adminRemoveRole', async () => {
await mock.adminRemoveRole(advisors[3], ROLE_ADVISOR, { from: admin })
.should.be.fulfilled;
});
it('announces a RoleAdded event on addRole', async () => {
await expectEvent.inTransaction(
mock.adminAddRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
'RoleAdded'
);
});
it('announces a RoleRemoved event on removeRole', async () => {
await expectEvent.inTransaction(
mock.adminRemoveRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
'RoleRemoved'
);
});
});
context('in adversarial conditions', () => {
it('does not allow an advisor to remove another advisor', async () => {
await expectThrow(
mock.removeAdvisor(advisors[1], { from: advisors[0] })
);
});
it('does not allow "anyone" to remove an advisor', async () => {
await expectThrow(
mock.removeAdvisor(advisors[0], { from: anyone })
);
});
});
});