PostDeliveryCrowdsale.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(this.crowdsale.withdrawTokens(investor));
  36. });
  37. context('after closing time', function () {
  38. beforeEach(async function () {
  39. await time.increaseTo(this.afterClosingTime);
  40. });
  41. it('allows beneficiaries to withdraw tokens', async function () {
  42. await this.crowdsale.withdrawTokens(investor);
  43. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal('0');
  44. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  45. });
  46. it('rejects multiple withdrawals', async function () {
  47. await this.crowdsale.withdrawTokens(investor);
  48. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  49. });
  50. });
  51. });
  52. });
  53. });