Whitelist.test.js 3.1 KB

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