Whitelist.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. (await this.mock.whitelist(whitelistedAddress1)).should.equal(true);
  20. });
  21. it('should add addresses to the whitelist', async function () {
  22. await expectEvent.inTransaction(
  23. this.mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
  24. 'RoleAdded',
  25. { role: this.role },
  26. );
  27. for (const addr of whitelistedAddresses) {
  28. (await this.mock.whitelist(addr)).should.equal(true);
  29. }
  30. });
  31. it('should remove address from the whitelist', async function () {
  32. await expectEvent.inTransaction(
  33. this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
  34. 'RoleRemoved',
  35. { role: this.role },
  36. );
  37. (await this.mock.whitelist(whitelistedAddress1)).should.equal(false);
  38. });
  39. it('should remove addresses from the the whitelist', async function () {
  40. await expectEvent.inTransaction(
  41. this.mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
  42. 'RoleRemoved',
  43. { role: this.role },
  44. );
  45. for (const addr of whitelistedAddresses) {
  46. (await this.mock.whitelist(addr)).should.equal(false);
  47. }
  48. });
  49. it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async function () {
  50. await this.mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  51. await this.mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 });
  52. });
  53. });
  54. context('in adversarial conditions', function () {
  55. it('should not allow "anyone" to add to the whitelist', async function () {
  56. await expectThrow(
  57. this.mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
  58. );
  59. });
  60. it('should not allow "anyone" to remove from the whitelist', async function () {
  61. await expectThrow(
  62. this.mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
  63. );
  64. });
  65. it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async function () {
  66. await expectThrow(
  67. this.mock.onlyWhitelistedCanDoThis({ from: anyone })
  68. );
  69. });
  70. });
  71. });