AccessControl.test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
  2. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const AccessControlMock = contract.fromArtifact('AccessControlMock');
  5. describe('AccessControl', function () {
  6. const [ admin, authorized, otherAuthorized, other, otherAdmin ] = accounts;
  7. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  8. const ROLE = web3.utils.soliditySha3('ROLE');
  9. const OTHER_ROLE = web3.utils.soliditySha3('OTHER_ROLE');
  10. beforeEach(async function () {
  11. this.accessControl = await AccessControlMock.new({ from: admin });
  12. });
  13. describe('default admin', function () {
  14. it('deployer has default admin role', async function () {
  15. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true);
  16. });
  17. it('other roles\'s admin is the default admin role', async function () {
  18. expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  19. });
  20. it('default admin role\'s admin is itself', async function () {
  21. expect(await this.accessControl.getRoleAdmin(DEFAULT_ADMIN_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  22. });
  23. });
  24. describe('granting', function () {
  25. it('admin can grant role to other accounts', async function () {
  26. const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  27. expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: admin });
  28. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(true);
  29. });
  30. it('non-admin cannot grant role to other accounts', async function () {
  31. await expectRevert(
  32. this.accessControl.grantRole(ROLE, authorized, { from: other }),
  33. 'AccessControl: sender must be an admin to grant'
  34. );
  35. });
  36. it('accounts can be granted a role multiple times', async function () {
  37. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  38. const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  39. await expectEvent.not.inTransaction(receipt.tx, AccessControlMock, 'RoleGranted');
  40. });
  41. });
  42. describe('revoking', function () {
  43. it('roles that are not had can be revoked', async function () {
  44. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  45. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  46. await expectEvent.not.inTransaction(receipt.tx, AccessControlMock, 'RoleRevoked');
  47. });
  48. context('with granted role', function () {
  49. beforeEach(async function () {
  50. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  51. });
  52. it('admin can revoke role', async function () {
  53. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  54. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: admin });
  55. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  56. });
  57. it('non-admin cannot revoke role', async function () {
  58. await expectRevert(
  59. this.accessControl.revokeRole(ROLE, authorized, { from: other }),
  60. 'AccessControl: sender must be an admin to revoke'
  61. );
  62. });
  63. it('a role can be revoked multiple times', async function () {
  64. await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  65. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  66. await expectEvent.not.inTransaction(receipt.tx, AccessControlMock, 'RoleRevoked');
  67. });
  68. });
  69. });
  70. describe('renouncing', function () {
  71. it('roles that are not had can be renounced', async function () {
  72. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  73. await expectEvent.not.inTransaction(receipt.tx, AccessControlMock, 'RoleRevoked');
  74. });
  75. context('with granted role', function () {
  76. beforeEach(async function () {
  77. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  78. });
  79. it('bearer can renounce role', async function () {
  80. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  81. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: authorized });
  82. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  83. });
  84. it('only the sender can renounce their roles', async function () {
  85. await expectRevert(
  86. this.accessControl.renounceRole(ROLE, authorized, { from: admin }),
  87. 'AccessControl: can only renounce roles for self'
  88. );
  89. });
  90. it('a role can be renounced multiple times', async function () {
  91. await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  92. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  93. await expectEvent.not.inTransaction(receipt.tx, AccessControlMock, 'RoleRevoked');
  94. });
  95. });
  96. });
  97. describe('enumerating', function () {
  98. it('role bearers can be enumerated', async function () {
  99. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  100. await this.accessControl.grantRole(ROLE, otherAuthorized, { from: admin });
  101. const memberCount = await this.accessControl.getRoleMemberCount(ROLE);
  102. expect(memberCount).to.bignumber.equal('2');
  103. const bearers = [];
  104. for (let i = 0; i < memberCount; ++i) {
  105. bearers.push(await this.accessControl.getRoleMember(ROLE, i));
  106. }
  107. expect(bearers).to.have.members([authorized, otherAuthorized]);
  108. });
  109. });
  110. describe('setting role admin', function () {
  111. beforeEach(async function () {
  112. const receipt = await this.accessControl.setRoleAdmin(ROLE, OTHER_ROLE);
  113. expectEvent(receipt, 'RoleAdminChanged', {
  114. role: ROLE,
  115. previousAdminRole: DEFAULT_ADMIN_ROLE,
  116. newAdminRole: OTHER_ROLE,
  117. });
  118. await this.accessControl.grantRole(OTHER_ROLE, otherAdmin, { from: admin });
  119. });
  120. it('a role\'s admin role can be changed', async function () {
  121. expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(OTHER_ROLE);
  122. });
  123. it('the new admin can grant roles', async function () {
  124. const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
  125. expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: otherAdmin });
  126. });
  127. it('the new admin can revoke roles', async function () {
  128. await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
  129. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: otherAdmin });
  130. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: otherAdmin });
  131. });
  132. it('a role\'s previous admins no longer grant roles', async function () {
  133. await expectRevert(
  134. this.accessControl.grantRole(ROLE, authorized, { from: admin }),
  135. 'AccessControl: sender must be an admin to grant'
  136. );
  137. });
  138. it('a role\'s previous admins no longer revoke roles', async function () {
  139. await expectRevert(
  140. this.accessControl.revokeRole(ROLE, authorized, { from: admin }),
  141. 'AccessControl: sender must be an admin to revoke'
  142. );
  143. });
  144. });
  145. });