Whitelist.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 remove address from the whitelist', async function () {
  39. await expectEvent.inTransaction(
  40. mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
  41. 'WhitelistedAddressRemoved'
  42. );
  43. let isWhitelisted = await mock.whitelist(whitelistedAddress1);
  44. isWhitelisted.should.be.equal(false);
  45. });
  46. it('should remove addresses from the the whitelist', async function () {
  47. await expectEvent.inTransaction(
  48. mock.removeAddressesFromWhitelist(whitelistedAddresses, { from: owner }),
  49. 'WhitelistedAddressRemoved'
  50. );
  51. for (let addr of whitelistedAddresses) {
  52. const isWhitelisted = await mock.whitelist(addr);
  53. isWhitelisted.should.be.equal(false);
  54. }
  55. });
  56. it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => {
  57. await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
  58. await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
  59. .should.be.fulfilled;
  60. });
  61. });
  62. context('in adversarial conditions', () => {
  63. it('should not allow "anyone" to add to the whitelist', async () => {
  64. await expectThrow(
  65. mock.addAddressToWhitelist(whitelistedAddress1, { from: anyone })
  66. );
  67. });
  68. it('should not allow "anyone" to remove from the whitelist', async () => {
  69. await expectThrow(
  70. mock.removeAddressFromWhitelist(whitelistedAddress1, { from: anyone })
  71. );
  72. });
  73. it('should not allow "anyone" to call #onlyWhitelistedCanDoThis', async () => {
  74. await expectThrow(
  75. mock.onlyWhitelistedCanDoThis({ from: anyone })
  76. );
  77. });
  78. });
  79. });