PublicRole.behavior.js 4.2 KB

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