PostDeliveryCrowdsale.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const PostDeliveryCrowdsaleImpl = artifacts.require('PostDeliveryCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  5. const rate = new BN(1);
  6. const tokenSupply = new BN('10').pow(new BN('22'));
  7. before(async function () {
  8. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  9. await time.advanceBlock();
  10. });
  11. beforeEach(async function () {
  12. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  13. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  14. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  15. this.token = await SimpleToken.new();
  16. this.crowdsale = await PostDeliveryCrowdsaleImpl.new(
  17. this.openingTime, this.closingTime, rate, wallet, this.token.address
  18. );
  19. await this.token.transfer(this.crowdsale.address, tokenSupply);
  20. });
  21. context('after opening time', function () {
  22. beforeEach(async function () {
  23. await time.increaseTo(this.openingTime);
  24. });
  25. context('with bought tokens', function () {
  26. const value = ether('42');
  27. beforeEach(async function () {
  28. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  29. });
  30. it('does not immediately assign tokens to beneficiaries', async function () {
  31. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  32. (await this.token.balanceOf(investor)).should.be.bignumber.equal('0');
  33. });
  34. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  35. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  36. 'PostDeliveryCrowdsale: not closed'
  37. );
  38. });
  39. context('after closing time', function () {
  40. beforeEach(async function () {
  41. await time.increaseTo(this.afterClosingTime);
  42. });
  43. it('allows beneficiaries to withdraw tokens', async function () {
  44. await this.crowdsale.withdrawTokens(investor);
  45. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal('0');
  46. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  47. });
  48. it('rejects multiple withdrawals', async function () {
  49. await this.crowdsale.withdrawTokens(investor);
  50. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  51. 'PostDeliveryCrowdsale: beneficiary is not due any tokens'
  52. );
  53. });
  54. });
  55. });
  56. });
  57. });