PublicRole.behavior.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. require('chai')
  2. .should();
  3. function capitalize (str) {
  4. return str.replace(/\b\w/g, l => l.toUpperCase());
  5. }
  6. function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], rolename) {
  7. rolename = capitalize(rolename);
  8. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  9. describe('should behave like public role', function () {
  10. beforeEach('check preconditions', async function () {
  11. (await this.contract[`is${rolename}`](authorized)).should.equal(true);
  12. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  13. (await this.contract[`is${rolename}`](anyone)).should.equal(false);
  14. });
  15. describe('add', function () {
  16. it('adds role to a new account', async function () {
  17. await this.contract[`add${rolename}`](anyone, { from: authorized });
  18. (await this.contract[`is${rolename}`](anyone)).should.equal(true);
  19. });
  20. it('adds role to an already-assigned account', async function () {
  21. await this.contract[`add${rolename}`](authorized, { from: authorized });
  22. (await this.contract[`is${rolename}`](authorized)).should.equal(true);
  23. });
  24. it('doesn\'t revert when adding role to the null account', async function () {
  25. await this.contract[`add${rolename}`](ZERO_ADDRESS, { from: authorized });
  26. });
  27. });
  28. describe('remove', function () {
  29. it('removes role from an already assigned account', async function () {
  30. await this.contract[`remove${rolename}`](authorized);
  31. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  32. (await this.contract[`is${rolename}`](otherAuthorized)).should.equal(true);
  33. });
  34. it('doesn\'t revert when removing from an unassigned account', async function () {
  35. await this.contract[`remove${rolename}`](anyone);
  36. });
  37. it('doesn\'t revert when removing role from the null account', async function () {
  38. await this.contract[`remove${rolename}`](ZERO_ADDRESS);
  39. });
  40. });
  41. describe('renouncing roles', function () {
  42. it('renounces an assigned role', async function () {
  43. await this.contract[`renounce${rolename}`]({ from: authorized });
  44. (await this.contract[`is${rolename}`](authorized)).should.equal(false);
  45. });
  46. it('doesn\'t revert when renouncing unassigned role', async function () {
  47. await this.contract[`renounce${rolename}`]({ from: anyone });
  48. });
  49. });
  50. });
  51. }
  52. module.exports = {
  53. shouldBehaveLikePublicRole,
  54. };