Roles.test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const RolesMock = artifacts.require('RolesMock');
  3. require('chai')
  4. .should();
  5. contract('Roles', function ([_, authorized, otherAuthorized, anyone]) {
  6. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  7. beforeEach(async function () {
  8. this.roles = await RolesMock.new();
  9. });
  10. it('reverts when querying roles for the null account', async function () {
  11. await assertRevert(this.roles.has(ZERO_ADDRESS));
  12. });
  13. context('initially', function () {
  14. it('doesn\'t pre-assign roles', async function () {
  15. (await this.roles.has(authorized)).should.equal(false);
  16. (await this.roles.has(otherAuthorized)).should.equal(false);
  17. (await this.roles.has(anyone)).should.equal(false);
  18. });
  19. describe('adding roles', function () {
  20. it('adds roles to a single account', async function () {
  21. await this.roles.add(authorized);
  22. (await this.roles.has(authorized)).should.equal(true);
  23. (await this.roles.has(anyone)).should.equal(false);
  24. });
  25. it('adds roles to an already-assigned account', async function () {
  26. await this.roles.add(authorized);
  27. await this.roles.add(authorized);
  28. (await this.roles.has(authorized)).should.equal(true);
  29. });
  30. it('reverts when adding roles to the null account', async function () {
  31. await assertRevert(this.roles.add(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. (await this.roles.has(authorized)).should.equal(false);
  44. (await this.roles.has(otherAuthorized)).should.equal(true);
  45. });
  46. it('doesn\'t revert when removing unassigned roles', async function () {
  47. await this.roles.remove(anyone);
  48. });
  49. it('reverts when removing roles from the null account', async function () {
  50. await assertRevert(this.roles.remove(ZERO_ADDRESS));
  51. });
  52. });
  53. });
  54. });