Whitelist.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import expectThrow from '../helpers/expectThrow';
  2. import expectEvent from '../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. let mock;
  9. const [
  10. owner,
  11. whitelistedAddress1,
  12. whitelistedAddress2,
  13. anyone,
  14. ] = accounts;
  15. const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
  16. before(async function () {
  17. mock = await WhitelistMock.new();
  18. });
  19. context('in normal conditions', () => {
  20. it('should add address to the whitelist', async function () {
  21. await expectEvent.inTransaction(
  22. mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }),
  23. 'WhitelistedAddressAdded'
  24. );
  25. const isWhitelisted = await 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. mock.addAddressesToWhitelist(whitelistedAddresses, { from: owner }),
  31. 'WhitelistedAddressAdded'
  32. );
  33. for (let addr of whitelistedAddresses) {
  34. const isWhitelisted = await mock.whitelist(addr);
  35. isWhitelisted.should.be.equal(true);
  36. }
  37. });
  38. it('should not announce WhitelistedAddressAdded event if address is already in the whitelist', async function () {
  39. const { logs } = await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  40. logs.should.be.empty;
  41. });
  42. it('should remove address from the whitelist', async function () {
  43. await expectEvent.inTransaction(
  44. mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
  45. 'WhitelistedAddressRemoved'
  46. );
  47. let isWhitelisted = await mock.whitelist(whitelistedAddress1);
  48. isWhitelisted.should.be.equal(false);
  49. });
  50. it('should remove addresses from the the whitelist', async function () {
  51. await expectEvent.inTransaction(
  52. mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
  53. 'WhitelistedAddressRemoved'
  54. );
  55. for (let addr of whitelistedAddresses) {
  56. const isWhitelisted = await mock.whitelist(addr);
  57. isWhitelisted.should.be.equal(false);
  58. }
  59. });
  60. it('should not announce WhitelistedAddressRemoved event if address is not in the whitelist', async function () {
  61. const { logs } = await mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner });
  62. logs.should.be.empty;
  63. });
  64. it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => {
  65. await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  66. await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
  67. .should.be.fulfilled;
  68. });
  69. });
  70. context('in adversarial conditions', () => {
  71. it('should not allow "anyone" to add to the whitelist', async () => {
  72. await expectThrow(
  73. mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
  74. );
  75. });
  76. it('should not allow "anyone" to remove from the whitelist', async () => {
  77. await expectThrow(
  78. mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
  79. );
  80. });
  81. it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async () => {
  82. await expectThrow(
  83. mock.onlyWhitelistedCanDoThis({ from: anyone })
  84. );
  85. });
  86. });
  87. });