Roles.test.js 2.4 KB

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