Whitelist.test.js 3.1 KB

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