PublicRole.behavior.js 3.6 KB

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