Files
openzeppelin-contracts/test/crowdsale/Crowdsale.test.js
Nicolás Venturo 3e82db2f6f Migration to truffle 5 (and web3 1.0 (and BN)) (#1601)
* Now compiling using truffle 5.

* Migrated some test files, missing BN scientific notation usage.

* Now using BN time values.

* Migrate ERC20 tests.

* Migrate all ERC20 tests.

* Migrate utils, payment and ownership tests.

* All tests save ERC721 migrated.

* Migrated ERC721 tests.

* Fix lint errors.

* Delete old test helpers.

* Fix remaining crowdsale tests.

* Fix signature bouncer tests.

* Update how constants is used.

* Compile script pre-removes the build dir.

* Fix SafeMath tests.

* Revert "Compile script pre-removes the build dir."

This reverts commit 247e745113.

* Fix linter errors.

* Upgrade openzeppelin-test-helpers dependency.

* Update openzeppelin-test-helpers dependency.

* Define math constants globally.

* Remove unnecessary ether unit.

* Roll back reduced ether amounts in tests.

* Remove unnecessary toNumber conversions.

* Delete compile script.

* Fixed failing test.
2019-01-14 19:11:55 -03:00

122 lines
4.3 KiB
JavaScript

const { balance, BN, constants, ether, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
const { ZERO_ADDRESS } = constants;
const Crowdsale = artifacts.require('CrowdsaleMock');
const SimpleToken = artifacts.require('SimpleToken');
contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
const rate = new BN(1);
const value = ether('42');
const tokenSupply = new BN('10').pow(new BN('22'));
const expectedTokenAmount = rate.mul(value);
it('requires a non-null token', async function () {
await shouldFail.reverting(
Crowdsale.new(rate, wallet, ZERO_ADDRESS)
);
});
context('with token', async function () {
beforeEach(async function () {
this.token = await SimpleToken.new();
});
it('requires a non-zero rate', async function () {
await shouldFail.reverting(
Crowdsale.new(0, wallet, this.token.address)
);
});
it('requires a non-null wallet', async function () {
await shouldFail.reverting(
Crowdsale.new(rate, ZERO_ADDRESS, this.token.address)
);
});
context('once deployed', async function () {
beforeEach(async function () {
this.crowdsale = await Crowdsale.new(rate, wallet, this.token.address);
await this.token.transfer(this.crowdsale.address, tokenSupply);
});
describe('accepting payments', function () {
describe('bare payments', function () {
it('should accept payments', async function () {
await this.crowdsale.send(value, { from: purchaser });
});
it('reverts on zero-valued payments', async function () {
await shouldFail.reverting(
this.crowdsale.send(0, { from: purchaser })
);
});
});
describe('buyTokens', function () {
it('should accept payments', async function () {
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
});
it('reverts on zero-valued payments', async function () {
await shouldFail.reverting(
this.crowdsale.buyTokens(investor, { value: 0, from: purchaser })
);
});
it('requires a non-null beneficiary', async function () {
await shouldFail.reverting(
this.crowdsale.buyTokens(ZERO_ADDRESS, { value: value, from: purchaser })
);
});
});
});
describe('high-level purchase', function () {
it('should log purchase', async function () {
const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
expectEvent.inLogs(logs, 'TokensPurchased', {
purchaser: investor,
beneficiary: investor,
value: value,
amount: expectedTokenAmount,
});
});
it('should assign tokens to sender', async function () {
await this.crowdsale.sendTransaction({ value: value, from: investor });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
});
it('should forward funds to wallet', async function () {
(await balance.difference(wallet, () =>
this.crowdsale.sendTransaction({ value, from: investor }))
).should.be.bignumber.equal(value);
});
});
describe('low-level purchase', function () {
it('should log purchase', async function () {
const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
expectEvent.inLogs(logs, 'TokensPurchased', {
purchaser: purchaser,
beneficiary: investor,
value: value,
amount: expectedTokenAmount,
});
});
it('should assign tokens to beneficiary', async function () {
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
});
it('should forward funds to wallet', async function () {
(await balance.difference(wallet, () =>
this.crowdsale.buyTokens(investor, { value, from: purchaser }))
).should.be.bignumber.equal(value);
});
});
});
});
});