PostDeliveryCrowdsale.test.js 2.8 KB

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