AccessManager.test.js 49 KB

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