Whitelist.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { expectThrow } = require('../helpers/expectThrow');
  2. const expectEvent = require('../helpers/expectEvent');
  3. const WhitelistMock = artifacts.require('WhitelistMock');
  4. require('chai')
  5. .should();
  6. contract('Whitelist', function ([_, owner, whitelistedAddress1, whitelistedAddress2, anyone]) {
  7. const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
  8. beforeEach(async function () {
  9. this.mock = await WhitelistMock.new({ from: owner });
  10. this.role = await this.mock.ROLE_WHITELISTED();
  11. });
  12. context('in normal conditions', function () {
  13. it('should add address to the whitelist', async function () {
  14. await expectEvent.inTransaction(
  15. this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }),
  16. 'RoleAdded',
  17. { role: this.role },
  18. );
  19. const isWhitelisted = await this.mock.whitelist(whitelistedAddress1);
  20. isWhitelisted.should.be.equal(true);
  21. });
  22. it('should add addresses to the whitelist', async function () {
  23. await expectEvent.inTransaction(
  24. this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
  25. 'RoleAdded',
  26. { role: this.role },
  27. );
  28. for (const addr of whitelistedAddresses) {
  29. const isWhitelisted = await this.mock.whitelist(addr);
  30. isWhitelisted.should.be.equal(true);
  31. }
  32. });
  33. it('should remove address from the whitelist', async function () {
  34. await expectEvent.inTransaction(
  35. this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
  36. 'RoleRemoved',
  37. { role: this.role },
  38. );
  39. const isWhitelisted = await this.mock.whitelist(whitelistedAddress1);
  40. isWhitelisted.should.be.equal(false);
  41. });
  42. it('should remove addresses from the the whitelist', async function () {
  43. await expectEvent.inTransaction(
  44. this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
  45. 'RoleRemoved',
  46. { role: this.role },
  47. );
  48. for (const addr of whitelistedAddresses) {
  49. const isWhitelisted = await this.mock.whitelist(addr);
  50. isWhitelisted.should.be.equal(false);
  51. }
  52. });
  53. it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async function () {
  54. await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  55. await this.mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 });
  56. });
  57. });
  58. context('in adversarial conditions', function () {
  59. it('should not allow "anyone" to add to the whitelist', async function () {
  60. await expectThrow(
  61. this.mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
  62. );
  63. });
  64. it('should not allow "anyone" to remove from the whitelist', async function () {
  65. await expectThrow(
  66. this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
  67. );
  68. });
  69. it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async function () {
  70. await expectThrow(
  71. this.mock.onlyWhitelistedCanDoThis({ from: anyone })
  72. );
  73. });
  74. });
  75. });