WhitelistCrowdsale.test.js 2.3 KB

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