PublicRole.behavior.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const expectEvent = require('../../helpers/expectEvent');
  2. require('chai')
  3. .should();
  4. function capitalize (str) {
  5. return str.replace(/\b\w/g, l => l.toUpperCase());
  6. }
  7. function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], rolename) {
  8. rolename = capitalize(rolename);
  9. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  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. describe('add', function () {
  17. it('adds role to a new account', async function () {
  18. await this.contract[`add${rolename}`](anyone, { from: authorized });
  19. (await this.contract[`is${rolename}`](anyone)).should.equal(true);
  20. });
  21. it(`emits a ${rolename}Added event`, async function () {
  22. const { logs } = await this.contract[`add${rolename}`](anyone, { from: authorized });
  23. expectEvent.inLogs(logs, `${rolename}Added`, { account: anyone });
  24. });
  25. it('adds role to an already-assigned account', async function () {
  26. await this.contract[`add${rolename}`](authorized, { from: authorized });
  27. (await this.contract[`is${rolename}`](authorized)).should.equal(true);
  28. });
  29. it('doesn\'t revert when adding role to the null account', async function () {
  30. await this.contract[`add${rolename}`](ZERO_ADDRESS, { from: authorized });
  31. });
  32. });
  33. describe('remove', function () {
  34. it('removes role from an already assigned account', async function () {
  35. await this.contract[`remove${rolename}`](authorized);
  36. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  37. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  38. });
  39. it(`emits a ${rolename}Removed event`, async function () {
  40. const { logs } = await this.contract[`remove${rolename}`](authorized);
  41. expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
  42. });
  43. it('doesn\'t revert when removing from an unassigned account', async function () {
  44. await this.contract[`remove${rolename}`](anyone);
  45. });
  46. it('doesn\'t revert when removing role from the null account', async function () {
  47. await this.contract[`remove${rolename}`](ZERO_ADDRESS);
  48. });
  49. });
  50. describe('renouncing roles', function () {
  51. it('renounces an assigned role', async function () {
  52. await this.contract[`renounce${rolename}`]({ from: authorized });
  53. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  54. });
  55. it('doesn\'t revert when renouncing unassigned role', async function () {
  56. await this.contract[`renounce${rolename}`]({ from: anyone });
  57. });
  58. });
  59. });
  60. }
  61. module.exports = {
  62. shouldBehaveLikePublicRole,
  63. };