WhitelistCrowdsale.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { BN, ether, shouldFail } = require('openzeppelin-test-helpers');
  2. const WhitelistCrowdsale = artifacts.require('WhitelistCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('WhitelistCrowdsale', function ([_, wallet, whitelister, whitelisted, otherWhitelisted, other]) {
  5. const rate = new BN(1);
  6. const value = ether('42');
  7. const tokenSupply = new BN('10').pow(new BN('22'));
  8. beforeEach(async function () {
  9. this.token = await SimpleToken.new({ from: whitelister });
  10. this.crowdsale = await WhitelistCrowdsale.new(rate, wallet, this.token.address, { from: whitelister });
  11. await this.token.transfer(this.crowdsale.address, tokenSupply, { from: whitelister });
  12. });
  13. async function purchaseShouldSucceed (crowdsale, beneficiary, value) {
  14. await crowdsale.buyTokens(beneficiary, { from: beneficiary, value });
  15. await crowdsale.sendTransaction({ from: beneficiary, value });
  16. }
  17. async function purchaseShouldFail (crowdsale, beneficiary, value) {
  18. await shouldFail.reverting.withMessage(crowdsale.buyTokens(beneficiary, { from: beneficiary, value }),
  19. 'WhitelistCrowdsale: beneficiary doesn\'t have the Whitelisted role'
  20. );
  21. await shouldFail.reverting.withMessage(crowdsale.sendTransaction({ from: beneficiary, value }),
  22. 'WhitelistCrowdsale: beneficiary doesn\'t have the Whitelisted role'
  23. );
  24. }
  25. context('with no whitelisted addresses', function () {
  26. it('rejects all purchases', async function () {
  27. await purchaseShouldFail(this.crowdsale, other, value);
  28. await purchaseShouldFail(this.crowdsale, whitelisted, value);
  29. });
  30. });
  31. context('with whitelisted addresses', function () {
  32. beforeEach(async function () {
  33. await this.crowdsale.addWhitelisted(whitelisted, { from: whitelister });
  34. await this.crowdsale.addWhitelisted(otherWhitelisted, { from: whitelister });
  35. });
  36. it('accepts purchases with whitelisted beneficiaries', async function () {
  37. await purchaseShouldSucceed(this.crowdsale, whitelisted, value);
  38. await purchaseShouldSucceed(this.crowdsale, otherWhitelisted, value);
  39. });
  40. it('rejects purchases from whitelisted addresses with non-whitelisted beneficiaries', async function () {
  41. await shouldFail(this.crowdsale.buyTokens(other, { from: whitelisted, value }));
  42. });
  43. it('rejects purchases with non-whitelisted beneficiaries', async function () {
  44. await purchaseShouldFail(this.crowdsale, other, value);
  45. });
  46. });
  47. });