Roles.test.js 2.1 KB

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