WhitelistCrowdsale.test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(crowdsale.buyTokens(beneficiary, { from: beneficiary, value }));
  19. await shouldFail.reverting(crowdsale.sendTransaction({ from: beneficiary, value }));
  20. }
  21. context('with no whitelisted addresses', function () {
  22. it('rejects all purchases', async function () {
  23. await purchaseShouldFail(this.crowdsale, other, value);
  24. await purchaseShouldFail(this.crowdsale, whitelisted, value);
  25. });
  26. });
  27. context('with whitelisted addresses', function () {
  28. beforeEach(async function () {
  29. await this.crowdsale.addWhitelisted(whitelisted, { from: whitelister });
  30. await this.crowdsale.addWhitelisted(otherWhitelisted, { from: whitelister });
  31. });
  32. it('accepts purchases with whitelisted beneficiaries', async function () {
  33. await purchaseShouldSucceed(this.crowdsale, whitelisted, value);
  34. await purchaseShouldSucceed(this.crowdsale, otherWhitelisted, value);
  35. });
  36. it('rejects purchases from whitelisted addresses with non-whitelisted beneficiaries', async function () {
  37. await shouldFail(this.crowdsale.buyTokens(other, { from: whitelisted, value }));
  38. });
  39. it('rejects purchases with non-whitelisted beneficiaries', async function () {
  40. await purchaseShouldFail(this.crowdsale, other, value);
  41. });
  42. });
  43. });