Skip to content

Commit

Permalink
Improve PausableToken tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Jan 28, 2018
1 parent 966ed26 commit 94fc8ed
Showing 1 changed file with 244 additions and 43 deletions.
287 changes: 244 additions & 43 deletions test/token/PausableToken.test.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,264 @@
'user strict';

import assertRevert from '../helpers/assertRevert';
var PausableTokenMock = artifacts.require('PausableTokenMock');

contract('PausableToken', function (accounts) {
let token;
const PausableToken = artifacts.require('PausableTokenMock');

contract('PausableToken', function ([_, owner, recipient, anotherAccount]) {
beforeEach(async function () {
token = await PausableTokenMock.new(accounts[0], 100);
this.token = await PausableToken.new(owner, 100, { from: owner });
});

it('should return paused false after construction', async function () {
let paused = await token.paused();
describe('pause', function () {
describe('when the sender is the token owner', function () {
const from = owner;

assert.equal(paused, false);
});
describe('when the token is unpaused', function () {
it('pauses the token', async function () {
await this.token.pause({ from });

it('should return paused true after pause', async function () {
await token.pause();
let paused = await token.paused();
const paused = await this.token.paused();
assert.equal(paused, true);
});

assert.equal(paused, true);
});
it('emits a paused event', async function () {
const { logs } = await this.token.pause({ from });

it('should return paused false after pause and unpause', async function () {
await token.pause();
await token.unpause();
let paused = await token.paused();
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Pause');
});
});

assert.equal(paused, false);
});
describe('when the token is paused', function () {
beforeEach(async function () {
await this.token.pause({ from });
});

it('reverts', async function () {
await assertRevert(this.token.pause({ from }));
});
});
});

it('should be able to transfer if transfers are unpaused', async function () {
await token.transfer(accounts[1], 100);
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
describe('when the sender is not the token owner', function () {
const from = anotherAccount;

let balance1 = await token.balanceOf(accounts[1]);
assert.equal(balance1, 100);
it('reverts', async function () {
await assertRevert(this.token.pause({ from }));
});
});
});

it('should be able to transfer after transfers are paused and unpaused', async function () {
await token.pause();
await token.unpause();
await token.transfer(accounts[1], 100);
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
describe('unpause', function () {
describe('when the sender is the token owner', function () {
const from = owner;

let balance1 = await token.balanceOf(accounts[1]);
assert.equal(balance1, 100);
});
describe('when the token is paused', function () {
beforeEach(async function () {
await this.token.pause({ from });
});

it('unpauses the token', async function () {
await this.token.unpause({ from });

it('should throw an error trying to transfer while transactions are paused', async function () {
await token.pause();
await assertRevert(token.transfer(accounts[1], 100));
const paused = await this.token.paused();
assert.equal(paused, false);
});

it('emits an unpaused event', async function () {
const { logs } = await this.token.unpause({ from });

assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Unpause');
});
});

describe('when the token is unpaused', function () {
it('reverts', async function () {
await assertRevert(this.token.unpause({ from }));
});
});
});

describe('when the sender is not the token owner', function () {
const from = anotherAccount;

it('reverts', async function () {
await assertRevert(this.token.unpause({ from }));
});
});
});

it('should throw an error trying to transfer from another account while transactions are paused', async function () {
await token.pause();
await assertRevert(token.transferFrom(accounts[0], accounts[1], 100));
describe('pausable token', function () {
const from = owner;

describe('paused', function () {
it('is not paused by default', async function () {
const paused = await this.token.paused({ from });

assert.equal(paused, false);
});

it('is paused after being paused', async function () {
await this.token.pause({ from });
const paused = await this.token.paused({ from });

assert.equal(paused, true);
});

it('is not paused after being paused and then unpaused', async function () {
await this.token.pause({ from });
await this.token.unpause({ from });
const paused = await this.token.paused();

assert.equal(paused, false);
});
});

describe('transfer', function () {
it('allows to transfer when unpaused', async function () {
await this.token.transfer(recipient, 100, { from: owner });

const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);

const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 100);
});

it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });

await this.token.transfer(recipient, 100, { from: owner });

const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);

const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 100);
});

it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });

await assertRevert(this.token.transfer(recipient, 100, { from: owner }));
});
});

describe('approve', function () {
it('allows to approve when unpaused', async function () {
await this.token.approve(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 40);
});

it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });

await this.token.approve(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 40);
});

it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });

await assertRevert(this.token.approve(anotherAccount, 40, { from: owner }));
});
});

describe('transfer from', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 50, { from: owner });
});

it('allows to transfer from when unpaused', async function () {
await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });

const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 60);

const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 40);
});

it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });

await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });

const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 60);

const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 40);
});

it('reverts when trying to transfer from when paused', async function () {
await this.token.pause({ from: owner });

await assertRevert(this.token.transferFrom(owner, recipient, 40, { from: anotherAccount }));
});
});

describe('decrease approval', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 100, { from: owner });
});

it('allows to decrease approval when unpaused', async function () {
await this.token.decreaseApproval(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 60);
});

it('allows to decrease approval when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });

await this.token.decreaseApproval(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 60);
});

it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });

await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: owner }));
});
});

describe('increase approval', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 100, { from: owner });
});

it('allows to increase approval when unpaused', async function () {
await this.token.increaseApproval(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 140);
});

it('allows to increase approval when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });

await this.token.increaseApproval(anotherAccount, 40, { from: owner });

const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 140);
});

it('reverts when trying to increase approval when paused', async function () {
await this.token.pause({ from: owner });

await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: owner }));
});
});
});
});

0 comments on commit 94fc8ed

Please sign in to comment.