AccessManager.test.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. const { web3 } = require('hardhat');
  2. const { constants, expectEvent, time } = require('@openzeppelin/test-helpers');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const { selector } = require('../../helpers/methods');
  5. const { clockFromReceipt } = require('../../helpers/time');
  6. const { product } = require('../../helpers/iterate');
  7. const AccessManager = artifacts.require('$AccessManager');
  8. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  9. const Ownable = artifacts.require('$Ownable');
  10. const MAX_UINT64 = web3.utils.toBN((2n ** 64n - 1n).toString());
  11. const ROLES = {
  12. ADMIN: web3.utils.toBN(0),
  13. SOME_ADMIN: web3.utils.toBN(17),
  14. SOME: web3.utils.toBN(42),
  15. PUBLIC: MAX_UINT64,
  16. };
  17. Object.assign(ROLES, Object.fromEntries(Object.entries(ROLES).map(([key, value]) => [value, key])));
  18. const executeDelay = web3.utils.toBN(10);
  19. const grantDelay = web3.utils.toBN(10);
  20. const MINSETBACK = time.duration.days(5);
  21. const formatAccess = access => [access[0], access[1].toString()];
  22. contract('AccessManager', function (accounts) {
  23. const [admin, manager, member, user, other] = accounts;
  24. beforeEach(async function () {
  25. this.manager = await AccessManager.new(admin);
  26. // add member to role
  27. await this.manager.$_setRoleAdmin(ROLES.SOME, ROLES.SOME_ADMIN);
  28. await this.manager.$_setRoleGuardian(ROLES.SOME, ROLES.SOME_ADMIN);
  29. await this.manager.$_grantRole(ROLES.SOME_ADMIN, manager, 0, 0);
  30. await this.manager.$_grantRole(ROLES.SOME, member, 0, 0);
  31. });
  32. it('rejects zero address for initialAdmin', async function () {
  33. await expectRevertCustomError(AccessManager.new(constants.ZERO_ADDRESS), 'AccessManagerInvalidInitialAdmin', [
  34. constants.ZERO_ADDRESS,
  35. ]);
  36. });
  37. it('default minsetback is 1 day', async function () {
  38. expect(await this.manager.minSetback()).to.be.bignumber.equal(MINSETBACK);
  39. });
  40. it('roles are correctly initialized', async function () {
  41. // role admin
  42. expect(await this.manager.getRoleAdmin(ROLES.ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  43. expect(await this.manager.getRoleAdmin(ROLES.SOME_ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  44. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  45. expect(await this.manager.getRoleAdmin(ROLES.PUBLIC)).to.be.bignumber.equal(ROLES.ADMIN);
  46. // role guardian
  47. expect(await this.manager.getRoleGuardian(ROLES.ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  48. expect(await this.manager.getRoleGuardian(ROLES.SOME_ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  49. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  50. expect(await this.manager.getRoleGuardian(ROLES.PUBLIC)).to.be.bignumber.equal(ROLES.ADMIN);
  51. // role members
  52. expect(await this.manager.hasRole(ROLES.ADMIN, admin).then(formatAccess)).to.be.deep.equal([true, '0']);
  53. expect(await this.manager.hasRole(ROLES.ADMIN, manager).then(formatAccess)).to.be.deep.equal([false, '0']);
  54. expect(await this.manager.hasRole(ROLES.ADMIN, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  55. expect(await this.manager.hasRole(ROLES.ADMIN, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  56. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, admin).then(formatAccess)).to.be.deep.equal([false, '0']);
  57. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, manager).then(formatAccess)).to.be.deep.equal([true, '0']);
  58. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  59. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  60. expect(await this.manager.hasRole(ROLES.SOME, admin).then(formatAccess)).to.be.deep.equal([false, '0']);
  61. expect(await this.manager.hasRole(ROLES.SOME, manager).then(formatAccess)).to.be.deep.equal([false, '0']);
  62. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  63. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  64. expect(await this.manager.hasRole(ROLES.PUBLIC, admin).then(formatAccess)).to.be.deep.equal([true, '0']);
  65. expect(await this.manager.hasRole(ROLES.PUBLIC, manager).then(formatAccess)).to.be.deep.equal([true, '0']);
  66. expect(await this.manager.hasRole(ROLES.PUBLIC, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  67. expect(await this.manager.hasRole(ROLES.PUBLIC, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  68. });
  69. describe('Roles management', function () {
  70. describe('label role', function () {
  71. it('admin can emit a label event', async function () {
  72. expectEvent(await this.manager.labelRole(ROLES.SOME, 'Some label', { from: admin }), 'RoleLabel', {
  73. roleId: ROLES.SOME,
  74. label: 'Some label',
  75. });
  76. });
  77. it('admin can re-emit a label event', async function () {
  78. await this.manager.labelRole(ROLES.SOME, 'Some label', { from: admin });
  79. expectEvent(await this.manager.labelRole(ROLES.SOME, 'Updated label', { from: admin }), 'RoleLabel', {
  80. roleId: ROLES.SOME,
  81. label: 'Updated label',
  82. });
  83. });
  84. it('emitting a label is restricted', async function () {
  85. await expectRevertCustomError(
  86. this.manager.labelRole(ROLES.SOME, 'Invalid label', { from: other }),
  87. 'AccessManagerUnauthorizedAccount',
  88. [other, ROLES.ADMIN],
  89. );
  90. });
  91. });
  92. describe('grant role', function () {
  93. describe('without a grant delay', function () {
  94. it('without an execute delay', async function () {
  95. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  96. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  97. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  98. expectEvent(receipt, 'RoleGranted', {
  99. roleId: ROLES.SOME,
  100. account: user,
  101. since: timestamp,
  102. delay: '0',
  103. newMember: true,
  104. });
  105. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  106. const access = await this.manager.getAccess(ROLES.SOME, user);
  107. expect(access[0]).to.be.bignumber.equal(timestamp); // inRoleSince
  108. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  109. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  110. expect(access[3]).to.be.bignumber.equal('0'); // effect
  111. });
  112. it('with an execute delay', async function () {
  113. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  114. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, executeDelay, { from: manager });
  115. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  116. expectEvent(receipt, 'RoleGranted', {
  117. roleId: ROLES.SOME,
  118. account: user,
  119. since: timestamp,
  120. delay: executeDelay,
  121. newMember: true,
  122. });
  123. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([
  124. true,
  125. executeDelay.toString(),
  126. ]);
  127. const access = await this.manager.getAccess(ROLES.SOME, user);
  128. expect(access[0]).to.be.bignumber.equal(timestamp); // inRoleSince
  129. expect(access[1]).to.be.bignumber.equal(executeDelay); // currentDelay
  130. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  131. expect(access[3]).to.be.bignumber.equal('0'); // effect
  132. });
  133. it('to a user that is already in the role', async function () {
  134. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  135. await this.manager.grantRole(ROLES.SOME, member, 0, { from: manager });
  136. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  137. });
  138. it('to a user that is scheduled for joining the role', async function () {
  139. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  140. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  141. await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  142. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  143. });
  144. it('grant role is restricted', async function () {
  145. await expectRevertCustomError(
  146. this.manager.grantRole(ROLES.SOME, user, 0, { from: other }),
  147. 'AccessManagerUnauthorizedAccount',
  148. [other, ROLES.SOME_ADMIN],
  149. );
  150. });
  151. });
  152. describe('with a grant delay', function () {
  153. beforeEach(async function () {
  154. await this.manager.$_setGrantDelay(ROLES.SOME, grantDelay);
  155. await time.increase(MINSETBACK);
  156. });
  157. it('granted role is not active immediately', async function () {
  158. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  159. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  160. expectEvent(receipt, 'RoleGranted', {
  161. roleId: ROLES.SOME,
  162. account: user,
  163. since: timestamp.add(grantDelay),
  164. delay: '0',
  165. newMember: true,
  166. });
  167. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  168. const access = await this.manager.getAccess(ROLES.SOME, user);
  169. expect(access[0]).to.be.bignumber.equal(timestamp.add(grantDelay)); // inRoleSince
  170. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  171. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  172. expect(access[3]).to.be.bignumber.equal('0'); // effect
  173. });
  174. it('granted role is active after the delay', async function () {
  175. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  176. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  177. expectEvent(receipt, 'RoleGranted', {
  178. roleId: ROLES.SOME,
  179. account: user,
  180. since: timestamp.add(grantDelay),
  181. delay: '0',
  182. newMember: true,
  183. });
  184. await time.increase(grantDelay);
  185. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  186. const access = await this.manager.getAccess(ROLES.SOME, user);
  187. expect(access[0]).to.be.bignumber.equal(timestamp.add(grantDelay)); // inRoleSince
  188. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  189. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  190. expect(access[3]).to.be.bignumber.equal('0'); // effect
  191. });
  192. });
  193. it('cannot grant public role', async function () {
  194. await expectRevertCustomError(
  195. this.manager.$_grantRole(ROLES.PUBLIC, other, 0, executeDelay, { from: manager }),
  196. 'AccessManagerLockedRole',
  197. [ROLES.PUBLIC],
  198. );
  199. });
  200. });
  201. describe('revoke role', function () {
  202. it('from a user that is already in the role', async function () {
  203. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  204. const { receipt } = await this.manager.revokeRole(ROLES.SOME, member, { from: manager });
  205. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: member });
  206. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  207. const access = await this.manager.getAccess(ROLES.SOME, user);
  208. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  209. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  210. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  211. expect(access[3]).to.be.bignumber.equal('0'); // effect
  212. });
  213. it('from a user that is scheduled for joining the role', async function () {
  214. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  215. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  216. const { receipt } = await this.manager.revokeRole(ROLES.SOME, user, { from: manager });
  217. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: user });
  218. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  219. const access = await this.manager.getAccess(ROLES.SOME, user);
  220. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  221. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  222. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  223. expect(access[3]).to.be.bignumber.equal('0'); // effect
  224. });
  225. it('from a user that is not in the role', async function () {
  226. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  227. await this.manager.revokeRole(ROLES.SOME, user, { from: manager });
  228. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  229. });
  230. it('revoke role is restricted', async function () {
  231. await expectRevertCustomError(
  232. this.manager.revokeRole(ROLES.SOME, member, { from: other }),
  233. 'AccessManagerUnauthorizedAccount',
  234. [other, ROLES.SOME_ADMIN],
  235. );
  236. });
  237. });
  238. describe('renounce role', function () {
  239. it('for a user that is already in the role', async function () {
  240. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  241. const { receipt } = await this.manager.renounceRole(ROLES.SOME, member, { from: member });
  242. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: member });
  243. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  244. const access = await this.manager.getAccess(ROLES.SOME, member);
  245. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  246. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  247. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  248. expect(access[3]).to.be.bignumber.equal('0'); // effect
  249. });
  250. it('for a user that is schedule for joining the role', async function () {
  251. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  252. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  253. const { receipt } = await this.manager.renounceRole(ROLES.SOME, user, { from: user });
  254. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: user });
  255. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  256. const access = await this.manager.getAccess(ROLES.SOME, user);
  257. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  258. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  259. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  260. expect(access[3]).to.be.bignumber.equal('0'); // effect
  261. });
  262. it('for a user that is not in the role', async function () {
  263. await this.manager.renounceRole(ROLES.SOME, user, { from: user });
  264. });
  265. it('bad user confirmation', async function () {
  266. await expectRevertCustomError(
  267. this.manager.renounceRole(ROLES.SOME, member, { from: user }),
  268. 'AccessManagerBadConfirmation',
  269. [],
  270. );
  271. });
  272. });
  273. describe('change role admin', function () {
  274. it("admin can set any role's admin", async function () {
  275. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  276. const { receipt } = await this.manager.setRoleAdmin(ROLES.SOME, ROLES.ADMIN, { from: admin });
  277. expectEvent(receipt, 'RoleAdminChanged', { roleId: ROLES.SOME, admin: ROLES.ADMIN });
  278. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.ADMIN);
  279. });
  280. it("setting a role's admin is restricted", async function () {
  281. await expectRevertCustomError(
  282. this.manager.setRoleAdmin(ROLES.SOME, ROLES.SOME, { from: manager }),
  283. 'AccessManagerUnauthorizedAccount',
  284. [manager, ROLES.ADMIN],
  285. );
  286. });
  287. });
  288. describe('change role guardian', function () {
  289. it("admin can set any role's admin", async function () {
  290. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  291. const { receipt } = await this.manager.setRoleGuardian(ROLES.SOME, ROLES.ADMIN, { from: admin });
  292. expectEvent(receipt, 'RoleGuardianChanged', { roleId: ROLES.SOME, guardian: ROLES.ADMIN });
  293. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.ADMIN);
  294. });
  295. it("setting a role's admin is restricted", async function () {
  296. await expectRevertCustomError(
  297. this.manager.setRoleGuardian(ROLES.SOME, ROLES.SOME, { from: other }),
  298. 'AccessManagerUnauthorizedAccount',
  299. [other, ROLES.ADMIN],
  300. );
  301. });
  302. });
  303. describe('change execution delay', function () {
  304. it('increasing the delay has immediate effect', async function () {
  305. const oldDelay = web3.utils.toBN(10);
  306. const newDelay = web3.utils.toBN(100);
  307. // role is already granted (with no delay) in the initial setup. this update takes time.
  308. await this.manager.$_grantRole(ROLES.SOME, member, 0, oldDelay);
  309. const accessBefore = await this.manager.getAccess(ROLES.SOME, member);
  310. expect(accessBefore[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  311. expect(accessBefore[2]).to.be.bignumber.equal('0'); // pendingDelay
  312. expect(accessBefore[3]).to.be.bignumber.equal('0'); // effect
  313. const { receipt } = await this.manager.grantRole(ROLES.SOME, member, newDelay, {
  314. from: manager,
  315. });
  316. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  317. expectEvent(receipt, 'RoleGranted', {
  318. roleId: ROLES.SOME,
  319. account: member,
  320. since: timestamp,
  321. delay: newDelay,
  322. newMember: false,
  323. });
  324. // immediate effect
  325. const accessAfter = await this.manager.getAccess(ROLES.SOME, member);
  326. expect(accessAfter[1]).to.be.bignumber.equal(newDelay); // currentDelay
  327. expect(accessAfter[2]).to.be.bignumber.equal('0'); // pendingDelay
  328. expect(accessAfter[3]).to.be.bignumber.equal('0'); // effect
  329. });
  330. it('decreasing the delay takes time', async function () {
  331. const oldDelay = web3.utils.toBN(100);
  332. const newDelay = web3.utils.toBN(10);
  333. // role is already granted (with no delay) in the initial setup. this update takes time.
  334. await this.manager.$_grantRole(ROLES.SOME, member, 0, oldDelay);
  335. const accessBefore = await this.manager.getAccess(ROLES.SOME, member);
  336. expect(accessBefore[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  337. expect(accessBefore[2]).to.be.bignumber.equal('0'); // pendingDelay
  338. expect(accessBefore[3]).to.be.bignumber.equal('0'); // effect
  339. const { receipt } = await this.manager.grantRole(ROLES.SOME, member, newDelay, {
  340. from: manager,
  341. });
  342. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  343. const setback = oldDelay.sub(newDelay);
  344. expectEvent(receipt, 'RoleGranted', {
  345. roleId: ROLES.SOME,
  346. account: member,
  347. since: timestamp.add(setback),
  348. delay: newDelay,
  349. newMember: false,
  350. });
  351. // no immediate effect
  352. const accessAfter = await this.manager.getAccess(ROLES.SOME, member);
  353. expect(accessAfter[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  354. expect(accessAfter[2]).to.be.bignumber.equal(newDelay); // pendingDelay
  355. expect(accessAfter[3]).to.be.bignumber.equal(timestamp.add(setback)); // effect
  356. // delayed effect
  357. await time.increase(setback);
  358. const accessAfterSetback = await this.manager.getAccess(ROLES.SOME, member);
  359. expect(accessAfterSetback[1]).to.be.bignumber.equal(newDelay); // currentDelay
  360. expect(accessAfterSetback[2]).to.be.bignumber.equal('0'); // pendingDelay
  361. expect(accessAfterSetback[3]).to.be.bignumber.equal('0'); // effect
  362. });
  363. it('can set a user execution delay during the grant delay', async function () {
  364. await this.manager.$_grantRole(ROLES.SOME, other, 10, 0);
  365. // here: "other" is pending to get the role, but doesn't yet have it.
  366. const { receipt } = await this.manager.grantRole(ROLES.SOME, other, executeDelay, { from: manager });
  367. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  368. // increasing the execution delay from 0 to executeDelay is immediate
  369. expectEvent(receipt, 'RoleGranted', {
  370. roleId: ROLES.SOME,
  371. account: other,
  372. since: timestamp,
  373. delay: executeDelay,
  374. newMember: false,
  375. });
  376. });
  377. });
  378. describe('change grant delay', function () {
  379. it('increasing the delay has immediate effect', async function () {
  380. const oldDelay = web3.utils.toBN(10);
  381. const newDelay = web3.utils.toBN(100);
  382. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  383. await time.increase(MINSETBACK);
  384. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  385. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  386. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  387. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  388. expect(setback).to.be.bignumber.equal(MINSETBACK);
  389. expectEvent(receipt, 'RoleGrantDelayChanged', {
  390. roleId: ROLES.SOME,
  391. delay: newDelay,
  392. since: timestamp.add(setback),
  393. });
  394. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  395. await time.increase(setback);
  396. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  397. });
  398. it('increasing the delay has delay effect #1', async function () {
  399. const oldDelay = web3.utils.toBN(100);
  400. const newDelay = web3.utils.toBN(10);
  401. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  402. await time.increase(MINSETBACK);
  403. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  404. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  405. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  406. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  407. expect(setback).to.be.bignumber.equal(MINSETBACK);
  408. expectEvent(receipt, 'RoleGrantDelayChanged', {
  409. roleId: ROLES.SOME,
  410. delay: newDelay,
  411. since: timestamp.add(setback),
  412. });
  413. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  414. await time.increase(setback);
  415. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  416. });
  417. it('increasing the delay has delay effect #2', async function () {
  418. const oldDelay = time.duration.days(30); // more than the minsetback
  419. const newDelay = web3.utils.toBN(10);
  420. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  421. await time.increase(MINSETBACK);
  422. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  423. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  424. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  425. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  426. expect(setback).to.be.bignumber.gt(MINSETBACK);
  427. expectEvent(receipt, 'RoleGrantDelayChanged', {
  428. roleId: ROLES.SOME,
  429. delay: newDelay,
  430. since: timestamp.add(setback),
  431. });
  432. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  433. await time.increase(setback);
  434. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  435. });
  436. it('changing the grant delay is restricted', async function () {
  437. await expectRevertCustomError(
  438. this.manager.setGrantDelay(ROLES.SOME, grantDelay, { from: other }),
  439. 'AccessManagerUnauthorizedAccount',
  440. [ROLES.ADMIN, other],
  441. );
  442. });
  443. });
  444. });
  445. describe('with AccessManaged target contract', function () {
  446. beforeEach('deploy target contract', async function () {
  447. this.target = await AccessManagedTarget.new(this.manager.address);
  448. // helpers for indirect calls
  449. this.callData = selector('fnRestricted()');
  450. this.call = [this.target.address, this.callData];
  451. this.opId = web3.utils.keccak256(
  452. web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [user, ...this.call]),
  453. );
  454. this.direct = (opts = {}) => this.target.fnRestricted({ from: user, ...opts });
  455. this.schedule = (opts = {}) => this.manager.schedule(...this.call, 0, { from: user, ...opts });
  456. this.execute = (opts = {}) => this.manager.execute(...this.call, { from: user, ...opts });
  457. this.cancel = (opts = {}) => this.manager.cancel(user, ...this.call, { from: user, ...opts });
  458. });
  459. describe('Change function permissions', function () {
  460. const sigs = ['someFunction()', 'someOtherFunction(uint256)', 'oneMoreFunction(address,uint8)'].map(selector);
  461. it('admin can set function role', async function () {
  462. for (const sig of sigs) {
  463. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(ROLES.ADMIN);
  464. }
  465. const { receipt: receipt1 } = await this.manager.setTargetFunctionRole(this.target.address, sigs, ROLES.SOME, {
  466. from: admin,
  467. });
  468. for (const sig of sigs) {
  469. expectEvent(receipt1, 'TargetFunctionRoleUpdated', {
  470. target: this.target.address,
  471. selector: sig,
  472. roleId: ROLES.SOME,
  473. });
  474. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(ROLES.SOME);
  475. }
  476. const { receipt: receipt2 } = await this.manager.setTargetFunctionRole(
  477. this.target.address,
  478. [sigs[1]],
  479. ROLES.SOME_ADMIN,
  480. {
  481. from: admin,
  482. },
  483. );
  484. expectEvent(receipt2, 'TargetFunctionRoleUpdated', {
  485. target: this.target.address,
  486. selector: sigs[1],
  487. roleId: ROLES.SOME_ADMIN,
  488. });
  489. for (const sig of sigs) {
  490. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(
  491. sig == sigs[1] ? ROLES.SOME_ADMIN : ROLES.SOME,
  492. );
  493. }
  494. });
  495. it('non-admin cannot set function role', async function () {
  496. await expectRevertCustomError(
  497. this.manager.setTargetFunctionRole(this.target.address, sigs, ROLES.SOME, { from: other }),
  498. 'AccessManagerUnauthorizedAccount',
  499. [other, ROLES.ADMIN],
  500. );
  501. });
  502. });
  503. // WIP
  504. describe('Calling restricted & unrestricted functions', function () {
  505. for (const [callerRoles, fnRole, closed, delay] of product(
  506. [[], [ROLES.SOME]],
  507. [undefined, ROLES.ADMIN, ROLES.SOME, ROLES.PUBLIC],
  508. [false, true],
  509. [null, executeDelay],
  510. )) {
  511. // can we call with a delay ?
  512. const indirectSuccess = (fnRole == ROLES.PUBLIC || callerRoles.includes(fnRole)) && !closed;
  513. // can we call without a delay ?
  514. const directSuccess = (fnRole == ROLES.PUBLIC || (callerRoles.includes(fnRole) && !delay)) && !closed;
  515. const description = [
  516. 'Caller in roles',
  517. '[' + (callerRoles ?? []).map(roleId => ROLES[roleId]).join(', ') + ']',
  518. delay ? 'with a delay' : 'without a delay',
  519. '+',
  520. 'functions open to roles',
  521. '[' + (ROLES[fnRole] ?? '') + ']',
  522. closed ? `(closed)` : '',
  523. ].join(' ');
  524. describe(description, function () {
  525. beforeEach(async function () {
  526. // setup
  527. await Promise.all([
  528. this.manager.$_setTargetClosed(this.target.address, closed),
  529. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnRestricted()'), fnRole),
  530. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnUnrestricted()'), fnRole),
  531. ...callerRoles
  532. .filter(roleId => roleId != ROLES.PUBLIC)
  533. .map(roleId => this.manager.$_grantRole(roleId, user, 0, delay ?? 0)),
  534. ]);
  535. // post setup checks
  536. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(closed);
  537. if (fnRole) {
  538. expect(
  539. await this.manager.getTargetFunctionRole(this.target.address, selector('fnRestricted()')),
  540. ).to.be.bignumber.equal(fnRole);
  541. expect(
  542. await this.manager.getTargetFunctionRole(this.target.address, selector('fnUnrestricted()')),
  543. ).to.be.bignumber.equal(fnRole);
  544. }
  545. for (const roleId of callerRoles) {
  546. const access = await this.manager.getAccess(roleId, user);
  547. if (roleId == ROLES.PUBLIC) {
  548. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  549. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  550. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  551. expect(access[3]).to.be.bignumber.equal('0'); // effect
  552. } else {
  553. expect(access[0]).to.be.bignumber.gt('0'); // inRoleSince
  554. expect(access[1]).to.be.bignumber.eq(String(delay ?? 0)); // currentDelay
  555. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  556. expect(access[3]).to.be.bignumber.equal('0'); // effect
  557. }
  558. }
  559. });
  560. it('canCall', async function () {
  561. const result = await this.manager.canCall(user, this.target.address, selector('fnRestricted()'));
  562. expect(result[0]).to.be.equal(directSuccess);
  563. expect(result[1]).to.be.bignumber.equal(!directSuccess && indirectSuccess ? delay ?? '0' : '0');
  564. });
  565. it('Calling a non restricted function never revert', async function () {
  566. expectEvent(await this.target.fnUnrestricted({ from: user }), 'CalledUnrestricted', {
  567. caller: user,
  568. });
  569. });
  570. it(`Calling a restricted function directly should ${
  571. directSuccess ? 'succeed' : 'revert'
  572. }`, async function () {
  573. const promise = this.direct();
  574. if (directSuccess) {
  575. expectEvent(await promise, 'CalledRestricted', { caller: user });
  576. } else if (indirectSuccess) {
  577. await expectRevertCustomError(promise, 'AccessManagerNotScheduled', [this.opId]);
  578. } else {
  579. await expectRevertCustomError(promise, 'AccessManagedUnauthorized', [user]);
  580. }
  581. });
  582. it('Calling indirectly: only execute', async function () {
  583. // execute without schedule
  584. if (directSuccess) {
  585. const { receipt, tx } = await this.execute();
  586. expectEvent.notEmitted(receipt, 'OperationExecuted', { operationId: this.opId });
  587. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  588. } else if (indirectSuccess) {
  589. await expectRevertCustomError(this.execute(), 'AccessManagerNotScheduled', [this.opId]);
  590. } else {
  591. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  592. }
  593. });
  594. it('Calling indirectly: schedule and execute', async function () {
  595. if (directSuccess || indirectSuccess) {
  596. const { receipt } = await this.schedule();
  597. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  598. expectEvent(receipt, 'OperationScheduled', {
  599. operationId: this.opId,
  600. caller: user,
  601. target: this.call[0],
  602. data: this.call[1],
  603. });
  604. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  605. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  606. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  607. );
  608. // execute without wait
  609. if (directSuccess) {
  610. const { receipt, tx } = await this.execute();
  611. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  612. if (delay && fnRole !== ROLES.PUBLIC) {
  613. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  614. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  615. }
  616. } else if (indirectSuccess) {
  617. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  618. } else {
  619. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  620. }
  621. } else {
  622. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  623. }
  624. });
  625. it('Calling indirectly: schedule wait and execute', async function () {
  626. if (directSuccess || indirectSuccess) {
  627. const { receipt } = await this.schedule();
  628. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  629. expectEvent(receipt, 'OperationScheduled', {
  630. operationId: this.opId,
  631. caller: user,
  632. target: this.call[0],
  633. data: this.call[1],
  634. });
  635. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  636. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  637. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  638. );
  639. // wait
  640. await time.increase(delay ?? 0);
  641. // execute without wait
  642. if (directSuccess || indirectSuccess) {
  643. const { receipt, tx } = await this.execute();
  644. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  645. if (delay && fnRole !== ROLES.PUBLIC) {
  646. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  647. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  648. }
  649. } else {
  650. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  651. }
  652. } else {
  653. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  654. }
  655. });
  656. it('Calling directly: schedule and call', async function () {
  657. if (directSuccess || indirectSuccess) {
  658. const { receipt } = await this.schedule();
  659. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  660. expectEvent(receipt, 'OperationScheduled', {
  661. operationId: this.opId,
  662. caller: user,
  663. target: this.call[0],
  664. data: this.call[1],
  665. });
  666. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  667. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  668. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  669. // execute without wait
  670. const promise = this.direct();
  671. if (directSuccess) {
  672. expectEvent(await promise, 'CalledRestricted', { caller: user });
  673. // schedule is not reset
  674. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  675. } else if (indirectSuccess) {
  676. await expectRevertCustomError(promise, 'AccessManagerNotReady', [this.opId]);
  677. } else {
  678. await expectRevertCustomError(promise, 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  679. }
  680. } else {
  681. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  682. }
  683. });
  684. it('Calling directly: schedule wait and call', async function () {
  685. if (directSuccess || indirectSuccess) {
  686. const { receipt } = await this.schedule();
  687. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  688. expectEvent(receipt, 'OperationScheduled', {
  689. operationId: this.opId,
  690. caller: user,
  691. target: this.call[0],
  692. data: this.call[1],
  693. });
  694. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  695. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  696. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  697. // wait
  698. await time.increase(delay ?? 0);
  699. // execute without wait
  700. const promise = await this.direct();
  701. if (directSuccess) {
  702. expectEvent(await promise, 'CalledRestricted', { caller: user });
  703. // schedule is not reset
  704. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  705. } else if (indirectSuccess) {
  706. const receipt = await promise;
  707. expectEvent(receipt, 'CalledRestricted', { caller: user });
  708. await expectEvent.inTransaction(receipt.tx, this.manager, 'OperationExecuted', {
  709. operationId: this.opId,
  710. });
  711. // schedule is reset
  712. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  713. } else {
  714. await expectRevertCustomError(this.direct(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  715. }
  716. } else {
  717. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  718. }
  719. });
  720. it('Scheduling for later than needed'); // TODO
  721. });
  722. }
  723. });
  724. describe('Indirect execution corner-cases', async function () {
  725. beforeEach(async function () {
  726. await this.manager.$_setTargetFunctionRole(this.target.address, this.callData, ROLES.SOME);
  727. await this.manager.$_grantRole(ROLES.SOME, user, 0, executeDelay);
  728. });
  729. it('Checking canCall when caller is the manager depend on the _executionId', async function () {
  730. const result = await this.manager.canCall(this.manager.address, this.target.address, '0x00000000');
  731. expect(result[0]).to.be.false;
  732. expect(result[1]).to.be.bignumber.equal('0');
  733. });
  734. it('Cannot execute earlier', async function () {
  735. const { receipt } = await this.schedule();
  736. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  737. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(timestamp.add(executeDelay));
  738. // we need to set the clock 2 seconds before the value, because the increaseTo "consumes" the timestamp
  739. // and the next transaction will be one after that (see check below)
  740. await time.increaseTo(timestamp.add(executeDelay).subn(2));
  741. // too early
  742. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  743. // the revert happened one second before the execution delay expired
  744. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay).subn(1));
  745. // ok
  746. await this.execute();
  747. // the success happened when the delay was reached (earliest possible)
  748. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay));
  749. });
  750. it('Cannot schedule an already scheduled operation', async function () {
  751. const { receipt } = await this.schedule();
  752. expectEvent(receipt, 'OperationScheduled', {
  753. operationId: this.opId,
  754. caller: user,
  755. target: this.call[0],
  756. data: this.call[1],
  757. });
  758. await expectRevertCustomError(this.schedule(), 'AccessManagerAlreadyScheduled', [this.opId]);
  759. });
  760. it('Cannot cancel an operation that is not scheduled', async function () {
  761. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  762. });
  763. it('Cannot cancel an operation that is already executed', async function () {
  764. await this.schedule();
  765. await time.increase(executeDelay);
  766. await this.execute();
  767. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  768. });
  769. it('Scheduler can cancel', async function () {
  770. await this.schedule();
  771. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  772. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  773. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  774. });
  775. it('Guardian can cancel', async function () {
  776. await this.schedule();
  777. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  778. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  779. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  780. });
  781. it('Cancel is restricted', async function () {
  782. await this.schedule();
  783. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  784. await expectRevertCustomError(this.cancel({ from: other }), 'AccessManagerUnauthorizedCancel', [
  785. other,
  786. user,
  787. ...this.call,
  788. ]);
  789. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  790. });
  791. it('Can re-schedule after execution', async function () {
  792. await this.schedule();
  793. await time.increase(executeDelay);
  794. await this.execute();
  795. // reschedule
  796. const { receipt } = await this.schedule();
  797. expectEvent(receipt, 'OperationScheduled', {
  798. operationId: this.opId,
  799. caller: user,
  800. target: this.call[0],
  801. data: this.call[1],
  802. });
  803. });
  804. it('Can re-schedule after cancel', async function () {
  805. await this.schedule();
  806. await this.cancel();
  807. // reschedule
  808. const { receipt } = await this.schedule();
  809. expectEvent(receipt, 'OperationScheduled', {
  810. operationId: this.opId,
  811. caller: user,
  812. target: this.call[0],
  813. data: this.call[1],
  814. });
  815. });
  816. });
  817. });
  818. describe('with Ownable target contract', function () {
  819. const roleId = web3.utils.toBN(1);
  820. beforeEach(async function () {
  821. this.ownable = await Ownable.new(this.manager.address);
  822. // add user to role
  823. await this.manager.$_grantRole(roleId, user, 0, 0);
  824. });
  825. it('initial state', async function () {
  826. expect(await this.ownable.owner()).to.be.equal(this.manager.address);
  827. });
  828. describe('Contract is closed', function () {
  829. beforeEach(async function () {
  830. await this.manager.$_setTargetClosed(this.ownable.address, true);
  831. });
  832. it('directly call: reverts', async function () {
  833. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  834. });
  835. it('relayed call (with role): reverts', async function () {
  836. await expectRevertCustomError(
  837. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  838. 'AccessManagerUnauthorizedCall',
  839. [user, this.ownable.address, selector('$_checkOwner()')],
  840. );
  841. });
  842. it('relayed call (without role): reverts', async function () {
  843. await expectRevertCustomError(
  844. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  845. 'AccessManagerUnauthorizedCall',
  846. [other, this.ownable.address, selector('$_checkOwner()')],
  847. );
  848. });
  849. });
  850. describe('Contract is managed', function () {
  851. describe('function is open to specific role', function () {
  852. beforeEach(async function () {
  853. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), roleId);
  854. });
  855. it('directly call: reverts', async function () {
  856. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  857. user,
  858. ]);
  859. });
  860. it('relayed call (with role): success', async function () {
  861. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  862. });
  863. it('relayed call (without role): reverts', async function () {
  864. await expectRevertCustomError(
  865. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  866. 'AccessManagerUnauthorizedCall',
  867. [other, this.ownable.address, selector('$_checkOwner()')],
  868. );
  869. });
  870. });
  871. describe('function is open to public role', function () {
  872. beforeEach(async function () {
  873. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), ROLES.PUBLIC);
  874. });
  875. it('directly call: reverts', async function () {
  876. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  877. user,
  878. ]);
  879. });
  880. it('relayed call (with role): success', async function () {
  881. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  882. });
  883. it('relayed call (without role): success', async function () {
  884. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other });
  885. });
  886. });
  887. });
  888. });
  889. describe('authority update', function () {
  890. beforeEach(async function () {
  891. this.newManager = await AccessManager.new(admin);
  892. this.target = await AccessManagedTarget.new(this.manager.address);
  893. });
  894. it('admin can change authority', async function () {
  895. expect(await this.target.authority()).to.be.equal(this.manager.address);
  896. const { tx } = await this.manager.updateAuthority(this.target.address, this.newManager.address, { from: admin });
  897. await expectEvent.inTransaction(tx, this.target, 'AuthorityUpdated', { authority: this.newManager.address });
  898. expect(await this.target.authority()).to.be.equal(this.newManager.address);
  899. });
  900. it('cannot set an address without code as the authority', async function () {
  901. await expectRevertCustomError(
  902. this.manager.updateAuthority(this.target.address, user, { from: admin }),
  903. 'AccessManagedInvalidAuthority',
  904. [user],
  905. );
  906. });
  907. it('updateAuthority is restricted on manager', async function () {
  908. await expectRevertCustomError(
  909. this.manager.updateAuthority(this.target.address, this.newManager.address, { from: other }),
  910. 'AccessManagerUnauthorizedAccount',
  911. [other, ROLES.ADMIN],
  912. );
  913. });
  914. it('setAuthority is restricted on AccessManaged', async function () {
  915. await expectRevertCustomError(
  916. this.target.setAuthority(this.newManager.address, { from: admin }),
  917. 'AccessManagedUnauthorized',
  918. [admin],
  919. );
  920. });
  921. });
  922. // TODO:
  923. // - check opening/closing a contract
  924. // - check updating the contract delay
  925. // - check the delay applies to admin function
  926. describe.skip('contract modes', function () {});
  927. });