Roles.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { expectRevert, constants } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const RolesMock = artifacts.require('RolesMock');
  5. contract('Roles', function ([_, authorized, otherAuthorized, other]) {
  6. beforeEach(async function () {
  7. this.roles = await RolesMock.new();
  8. });
  9. it('reverts when querying roles for the zero account', async function () {
  10. await expectRevert(this.roles.has(ZERO_ADDRESS), 'Roles: account is the zero address');
  11. });
  12. context('initially', function () {
  13. it('doesn\'t pre-assign roles', async function () {
  14. expect(await this.roles.has(authorized)).to.equal(false);
  15. expect(await this.roles.has(otherAuthorized)).to.equal(false);
  16. expect(await this.roles.has(other)).to.equal(false);
  17. });
  18. describe('adding roles', function () {
  19. it('adds roles to a single account', async function () {
  20. await this.roles.add(authorized);
  21. expect(await this.roles.has(authorized)).to.equal(true);
  22. expect(await this.roles.has(other)).to.equal(false);
  23. });
  24. it('reverts when adding roles to an already assigned account', async function () {
  25. await this.roles.add(authorized);
  26. await expectRevert(this.roles.add(authorized), 'Roles: account already has role');
  27. });
  28. it('reverts when adding roles to the zero account', async function () {
  29. await expectRevert(this.roles.add(ZERO_ADDRESS), 'Roles: account is the zero address');
  30. });
  31. });
  32. });
  33. context('with added roles', function () {
  34. beforeEach(async function () {
  35. await this.roles.add(authorized);
  36. await this.roles.add(otherAuthorized);
  37. });
  38. describe('removing roles', function () {
  39. it('removes a single role', async function () {
  40. await this.roles.remove(authorized);
  41. expect(await this.roles.has(authorized)).to.equal(false);
  42. expect(await this.roles.has(otherAuthorized)).to.equal(true);
  43. });
  44. it('reverts when removing unassigned roles', async function () {
  45. await expectRevert(this.roles.remove(other), 'Roles: account does not have role');
  46. });
  47. it('reverts when removing roles from the zero account', async function () {
  48. await expectRevert(this.roles.remove(ZERO_ADDRESS), 'Roles: account is the zero address');
  49. });
  50. });
  51. });
  52. });