AccessManager.test.js 49 KB

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