PublicRole.behavior.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const shouldFail = require('../../helpers/shouldFail');
  2. const { ZERO_ADDRESS } = require('../../helpers/constants');
  3. const expectEvent = require('../../helpers/expectEvent');
  4. require('chai')
  5. .should();
  6. function capitalize (str) {
  7. return str.replace(/\b\w/g, l => l.toUpperCase());
  8. }
  9. function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], rolename) {
  10. rolename = capitalize(rolename);
  11. describe('should behave like public role', function () {
  12. beforeEach('check preconditions', async function () {
  13. (await this.contract[`is${rolename}`](authorized)).should.equal(true);
  14. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  15. (await this.contract[`is${rolename}`](anyone)).should.equal(false);
  16. });
  17. it('emits events during construction', async function () {
  18. await expectEvent.inConstruction(this.contract, `${rolename}Added`, {
  19. account: authorized,
  20. });
  21. });
  22. it('reverts when querying roles for the null account', async function () {
  23. await shouldFail.reverting(this.contract[`is${rolename}`](ZERO_ADDRESS));
  24. });
  25. describe('access control', function () {
  26. context('from authorized account', function () {
  27. const from = authorized;
  28. it('allows access', async function () {
  29. await this.contract[`only${rolename}Mock`]({ from });
  30. });
  31. });
  32. context('from unauthorized account', function () {
  33. const from = anyone;
  34. it('reverts', async function () {
  35. await shouldFail.reverting(this.contract[`only${rolename}Mock`]({ from }));
  36. });
  37. });
  38. });
  39. describe('add', function () {
  40. it('adds role to a new account', async function () {
  41. await this.contract[`add${rolename}`](anyone, { from: authorized });
  42. (await this.contract[`is${rolename}`](anyone)).should.equal(true);
  43. });
  44. it(`emits a ${rolename}Added event`, async function () {
  45. const { logs } = await this.contract[`add${rolename}`](anyone, { from: authorized });
  46. expectEvent.inLogs(logs, `${rolename}Added`, { account: anyone });
  47. });
  48. it('reverts when adding role to an already assigned account', async function () {
  49. await shouldFail.reverting(this.contract[`add${rolename}`](authorized, { from: authorized }));
  50. });
  51. it('reverts when adding role to the null account', async function () {
  52. await shouldFail.reverting(this.contract[`add${rolename}`](ZERO_ADDRESS, { from: authorized }));
  53. });
  54. });
  55. describe('remove', function () {
  56. it('removes role from an already assigned account', async function () {
  57. await this.contract[`remove${rolename}`](authorized);
  58. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  59. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  60. });
  61. it(`emits a ${rolename}Removed event`, async function () {
  62. const { logs } = await this.contract[`remove${rolename}`](authorized);
  63. expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
  64. });
  65. it('reverts when removing from an unassigned account', async function () {
  66. await shouldFail.reverting(this.contract[`remove${rolename}`](anyone));
  67. });
  68. it('reverts when removing role from the null account', async function () {
  69. await shouldFail.reverting(this.contract[`remove${rolename}`](ZERO_ADDRESS));
  70. });
  71. });
  72. describe('renouncing roles', function () {
  73. it('renounces an assigned role', async function () {
  74. await this.contract[`renounce${rolename}`]({ from: authorized });
  75. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  76. });
  77. it(`emits a ${rolename}Removed event`, async function () {
  78. const { logs } = await this.contract[`renounce${rolename}`]({ from: authorized });
  79. expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
  80. });
  81. it('reverts when renouncing unassigned role', async function () {
  82. await shouldFail.reverting(this.contract[`renounce${rolename}`]({ from: anyone }));
  83. });
  84. });
  85. });
  86. }
  87. module.exports = {
  88. shouldBehaveLikePublicRole,
  89. };