WhitelistCrowdsale.test.js 2.5 KB

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