AccessManager.test.js 49 KB

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