PublicRole.behavior.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const { assertRevert } = require('../../helpers/assertRevert');
  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('reverts when querying roles for the null account', async function () {
  18. await assertRevert(this.contract[`is${rolename}`](ZERO_ADDRESS));
  19. });
  20. describe('access control', function () {
  21. context('from authorized account', function () {
  22. const from = authorized;
  23. it('allows access', async function () {
  24. await this.contract[`only${rolename}Mock`]({ from });
  25. });
  26. });
  27. context('from unauthorized account', function () {
  28. const from = anyone;
  29. it('reverts', async function () {
  30. await assertRevert(this.contract[`only${rolename}Mock`]({ from }));
  31. });
  32. });
  33. });
  34. describe('add', function () {
  35. it('adds role to a new account', async function () {
  36. await this.contract[`add${rolename}`](anyone, { from: authorized });
  37. (await this.contract[`is${rolename}`](anyone)).should.equal(true);
  38. });
  39. it(`emits a ${rolename}Added event`, async function () {
  40. const { logs } = await this.contract[`add${rolename}`](anyone, { from: authorized });
  41. expectEvent.inLogs(logs, `${rolename}Added`, { account: anyone });
  42. });
  43. it('adds role to an already-assigned account', async function () {
  44. await this.contract[`add${rolename}`](authorized, { from: authorized });
  45. (await this.contract[`is${rolename}`](authorized)).should.equal(true);
  46. });
  47. it('reverts when adding role to the null account', async function () {
  48. await assertRevert(this.contract[`add${rolename}`](ZERO_ADDRESS, { from: authorized }));
  49. });
  50. });
  51. describe('remove', function () {
  52. it('removes role from an already assigned account', async function () {
  53. await this.contract[`remove${rolename}`](authorized);
  54. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  55. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  56. });
  57. it(`emits a ${rolename}Removed event`, async function () {
  58. const { logs } = await this.contract[`remove${rolename}`](authorized);
  59. expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
  60. });
  61. it('doesn\'t revert when removing from an unassigned account', async function () {
  62. await this.contract[`remove${rolename}`](anyone);
  63. });
  64. it('reverts when removing role from the null account', async function () {
  65. await assertRevert(this.contract[`remove${rolename}`](ZERO_ADDRESS));
  66. });
  67. });
  68. describe('renouncing roles', function () {
  69. it('renounces an assigned role', async function () {
  70. await this.contract[`renounce${rolename}`]({ from: authorized });
  71. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  72. });
  73. it(`emits a ${rolename}Removed event`, async function () {
  74. const { logs } = await this.contract[`renounce${rolename}`]({ from: authorized });
  75. expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
  76. });
  77. it('doesn\'t revert when renouncing unassigned role', async function () {
  78. await this.contract[`renounce${rolename}`]({ from: anyone });
  79. });
  80. });
  81. });
  82. }
  83. module.exports = {
  84. shouldBehaveLikePublicRole,
  85. };