AccessManager.behavior.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const {
  2. LIKE_COMMON_IS_EXECUTING,
  3. LIKE_COMMON_GET_ACCESS,
  4. LIKE_COMMON_SCHEDULABLE,
  5. testAsSchedulableOperation,
  6. testAsRestrictedOperation,
  7. testAsDelayedOperation,
  8. testAsCanCall,
  9. testAsHasRole,
  10. } = require('./AccessManager.predicate');
  11. // ============ ADMIN OPERATION ============
  12. /**
  13. * @requires this.{manager,roles,calldata,role}
  14. */
  15. function shouldBehaveLikeDelayedAdminOperation() {
  16. const getAccessPath = LIKE_COMMON_GET_ACCESS;
  17. testAsDelayedOperation.mineDelay = true;
  18. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay =
  19. testAsDelayedOperation;
  20. getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = function () {
  21. beforeEach('set execution delay', async function () {
  22. this.scheduleIn = this.executionDelay; // For testAsDelayedOperation
  23. });
  24. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  25. };
  26. beforeEach('set target as manager', function () {
  27. this.target = this.manager;
  28. });
  29. testAsRestrictedOperation({
  30. callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
  31. callerIsNotTheManager() {
  32. testAsHasRole({
  33. publicRoleIsRequired() {
  34. it('reverts as AccessManagerUnauthorizedAccount', async function () {
  35. await expect(this.caller.sendTransaction({ to: this.target, data: this.calldata }))
  36. .to.be.revertedWithCustomError(this.target, 'AccessManagerUnauthorizedAccount')
  37. .withArgs(
  38. this.caller.address,
  39. this.roles.ADMIN.id, // Although PUBLIC is required, target function role doesn't apply to admin ops
  40. );
  41. });
  42. },
  43. specificRoleIsRequired: getAccessPath,
  44. });
  45. },
  46. });
  47. }
  48. /**
  49. * @requires this.{manager,roles,calldata,role}
  50. */
  51. function shouldBehaveLikeNotDelayedAdminOperation() {
  52. const getAccessPath = LIKE_COMMON_GET_ACCESS;
  53. function testScheduleOperation(mineDelay) {
  54. return function self() {
  55. self.mineDelay = mineDelay;
  56. beforeEach('set execution delay', async function () {
  57. this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
  58. });
  59. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  60. };
  61. }
  62. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay =
  63. testScheduleOperation(true);
  64. getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = testScheduleOperation(false);
  65. beforeEach('set target as manager', function () {
  66. this.target = this.manager;
  67. });
  68. testAsRestrictedOperation({
  69. callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
  70. callerIsNotTheManager() {
  71. testAsHasRole({
  72. publicRoleIsRequired() {
  73. it('reverts as AccessManagerUnauthorizedAccount', async function () {
  74. await expect(this.caller.sendTransaction({ to: this.target, data: this.calldata }))
  75. .to.be.revertedWithCustomError(this.target, 'AccessManagerUnauthorizedAccount')
  76. .withArgs(
  77. this.caller.address,
  78. this.roles.ADMIN.id, // Although PUBLIC_ROLE is required, admin ops are not subject to target function roles
  79. );
  80. });
  81. },
  82. specificRoleIsRequired: getAccessPath,
  83. });
  84. },
  85. });
  86. }
  87. /**
  88. * @requires this.{manager,roles,calldata,role}
  89. */
  90. function shouldBehaveLikeRoleAdminOperation(roleAdmin) {
  91. const getAccessPath = LIKE_COMMON_GET_ACCESS;
  92. function afterGrantDelay() {
  93. afterGrantDelay.mineDelay = true;
  94. beforeEach('set execution delay', async function () {
  95. this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
  96. });
  97. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  98. }
  99. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay = afterGrantDelay;
  100. getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = afterGrantDelay;
  101. beforeEach('set target as manager', function () {
  102. this.target = this.manager;
  103. });
  104. testAsRestrictedOperation({
  105. callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
  106. callerIsNotTheManager() {
  107. testAsHasRole({
  108. publicRoleIsRequired() {
  109. it('reverts as AccessManagerUnauthorizedAccount', async function () {
  110. await expect(this.caller.sendTransaction({ to: this.target, data: this.calldata }))
  111. .to.be.revertedWithCustomError(this.target, 'AccessManagerUnauthorizedAccount')
  112. .withArgs(this.caller.address, roleAdmin);
  113. });
  114. },
  115. specificRoleIsRequired: getAccessPath,
  116. });
  117. },
  118. });
  119. }
  120. // ============ RESTRICTED OPERATION ============
  121. /**
  122. * @requires this.{manager,roles,calldata,role}
  123. */
  124. function shouldBehaveLikeAManagedRestrictedOperation() {
  125. function revertUnauthorized() {
  126. it('reverts as AccessManagedUnauthorized', async function () {
  127. await expect(this.caller.sendTransaction({ to: this.target, data: this.calldata }))
  128. .to.be.revertedWithCustomError(this.target, 'AccessManagedUnauthorized')
  129. .withArgs(this.caller.address);
  130. });
  131. }
  132. const getAccessPath = LIKE_COMMON_GET_ACCESS;
  133. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.beforeGrantDelay =
  134. revertUnauthorized;
  135. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasNoExecutionDelay.beforeGrantDelay =
  136. revertUnauthorized;
  137. getAccessPath.requiredRoleIsNotGranted = revertUnauthorized;
  138. function testScheduleOperation(mineDelay) {
  139. return function self() {
  140. self.mineDelay = mineDelay;
  141. beforeEach('sets execution delay', async function () {
  142. this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
  143. });
  144. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  145. };
  146. }
  147. getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay =
  148. testScheduleOperation(true);
  149. getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = testScheduleOperation(false);
  150. const isExecutingPath = LIKE_COMMON_IS_EXECUTING;
  151. isExecutingPath.notExecuting = revertUnauthorized;
  152. testAsCanCall({
  153. closed: revertUnauthorized,
  154. open: {
  155. callerIsTheManager: isExecutingPath,
  156. callerIsNotTheManager: {
  157. publicRoleIsRequired() {
  158. it('succeeds called directly', async function () {
  159. await this.caller.sendTransaction({ to: this.target, data: this.calldata });
  160. });
  161. it('succeeds via execute', async function () {
  162. await this.manager.connect(this.caller).execute(this.target, this.calldata);
  163. });
  164. },
  165. specificRoleIsRequired: getAccessPath,
  166. },
  167. },
  168. });
  169. }
  170. module.exports = {
  171. shouldBehaveLikeDelayedAdminOperation,
  172. shouldBehaveLikeNotDelayedAdminOperation,
  173. shouldBehaveLikeRoleAdminOperation,
  174. shouldBehaveLikeAManagedRestrictedOperation,
  175. };