AccessManager.test.js 51 KB

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