Whitelist.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const { expectThrow } = require('../helpers/expectThrow');
  2. const WhitelistMock = artifacts.require('WhitelistMock');
  3. require('chai')
  4. .should();
  5. contract('Whitelist', function ([_, owner, whitelistedAddress1, whitelistedAddress2, anyone]) {
  6. const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
  7. beforeEach(async function () {
  8. this.mock = await WhitelistMock.new({ from: owner });
  9. });
  10. context('in normal conditions', function () {
  11. it('should add address to the whitelist', async function () {
  12. await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  13. (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(true);
  14. });
  15. it('should add addresses to the whitelist', async function () {
  16. await this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner });
  17. for (const addr of whitelistedAddresses) {
  18. (await this.mock.isWhitelisted(addr)).should.equal(true);
  19. }
  20. });
  21. it('should remove address from the whitelist', async function () {
  22. await this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner });
  23. (await this.mock.isWhitelisted(whitelistedAddress1)).should.equal(false);
  24. });
  25. it('should remove addresses from the the whitelist', async function () {
  26. await this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner });
  27. for (const addr of whitelistedAddresses) {
  28. (await this.mock.isWhitelisted(addr)).should.equal(false);
  29. }
  30. });
  31. it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async function () {
  32. await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  33. await this.mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 });
  34. });
  35. });
  36. context('in adversarial conditions', function () {
  37. it('should not allow "anyone" to add to the whitelist', async function () {
  38. await expectThrow(
  39. this.mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
  40. );
  41. });
  42. it('should not allow "anyone" to remove from the whitelist', async function () {
  43. await expectThrow(
  44. this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
  45. );
  46. });
  47. it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async function () {
  48. await expectThrow(
  49. this.mock.onlyWhitelistedCanDoThis({ from: anyone })
  50. );
  51. });
  52. });
  53. });