Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated tests to reflect the new behavior.
  • Loading branch information
nventuro committed Sep 7, 2018
commit 79fb00cb6deea7227b30d8e6920cd30afc82f91e
123 changes: 30 additions & 93 deletions test/token/ERC20/ERC20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,8 @@ contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
const amount = 100;
const spender = ZERO_ADDRESS;

it('approves the requested amount', async function () {
await this.token.approve(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
});

it('emits an approval event', async function () {
const { logs } = await this.token.approve(spender, amount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(amount);
it('reverts', async function () {
await assertRevert(this.token.approve(spender, amount, { from: owner }));
});
});
});
Expand Down Expand Up @@ -265,84 +253,57 @@ contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
describe('when the spender is not the zero address', function () {
const spender = recipient;

describe('when the sender has enough balance', function () {
const amount = 100;

it('emits an approval event', async function () {
const { logs } = await this.token.decreaseAllowance(spender, amount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(0);
});

function shouldDecreaseApproval(amount) {
describe('when there was no approved amount before', function () {
it('keeps the allowance to zero', async function () {
await this.token.decreaseAllowance(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
it('reverts', async function () {
await assertRevert(this.token.decreaseAllowance(spender, amount, { from: owner }));
});
});

describe('when the spender had an approved amount', function () {
const approvedAmount = amount;

beforeEach(async function () {
await this.token.approve(spender, approvedAmount, { from: owner });
({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: owner }));
});

it('emits an approval event', async function () {
const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(0);
});

it('decreases the spender allowance subtracting the requested amount', async function () {
await this.token.decreaseAllowance(spender, approvedAmount - 5, { from: owner });
await this.token.decreaseAllowance(spender, approvedAmount - 1, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(5);
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(1);
});

it('sets the allowance to zero when all allowance is removed', async function () {
await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
});

it('sets the allowance to zero when more than the full allowance is removed', async function () {
await this.token.decreaseAllowance(spender, approvedAmount + 5, { from: owner });
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
it('reverts when more than the full allowance is removed', async function () {
await assertRevert(this.token.decreaseAllowance(spender, approvedAmount + 1, { from: owner }));
});
});
}

describe('when the sender has enough balance', function () {
const amount = 100;

shouldDecreaseApproval(amount);
});

describe('when the sender does not have enough balance', function () {
const amount = 101;

it('emits an approval event', async function () {
const { logs } = await this.token.decreaseAllowance(spender, amount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(0);
});

describe('when there was no approved amount before', function () {
it('keeps the allowance to zero', async function () {
await this.token.decreaseAllowance(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
});
});

describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, amount + 1, { from: owner });
});

it('decreases the spender allowance subtracting the requested amount', async function () {
await this.token.decreaseAllowance(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(1);
});
});
shouldDecreaseApproval(amount);
});
});

Expand All @@ -351,19 +312,7 @@ contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
const spender = ZERO_ADDRESS;

it('decreases the requested amount', async function () {
await this.token.decreaseAllowance(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
});

it('emits an approval event', async function () {
const { logs } = await this.token.decreaseAllowance(spender, amount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(0);
await assertRevert(this.token.decreaseAllowance(spender, amount, { from: owner }));
});
});
});
Expand Down Expand Up @@ -444,20 +393,8 @@ contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
describe('when the spender is the zero address', function () {
const spender = ZERO_ADDRESS;

it('approves the requested amount', async function () {
await this.token.increaseAllowance(spender, amount, { from: owner });

(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
});

it('emits an approval event', async function () {
const { logs } = await this.token.increaseAllowance(spender, amount, { from: owner });

logs.length.should.equal(1);
logs[0].event.should.equal('Approval');
logs[0].args.owner.should.equal(owner);
logs[0].args.spender.should.equal(spender);
logs[0].args.value.should.be.bignumber.equal(amount);
it('reverts', async function () {
await assertRevert(this.token.increaseAllowance(spender, amount, { from: owner }));
});
});
});
Expand Down