AccessManagedAdapter.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const { constants, time } = require('@openzeppelin/test-helpers');
  2. const { expectRevertCustomError } = require('../../../helpers/customError');
  3. const { AccessMode } = require('../../../helpers/enums');
  4. const { selector } = require('../../../helpers/methods');
  5. const AccessManager = artifacts.require('$AccessManager');
  6. const AccessManagedAdapter = artifacts.require('AccessManagedAdapter');
  7. const Ownable = artifacts.require('$Ownable');
  8. const groupId = web3.utils.toBN(1);
  9. contract('AccessManagedAdapter', function (accounts) {
  10. const [admin, user, other] = accounts;
  11. beforeEach(async function () {
  12. this.manager = await AccessManager.new(admin);
  13. this.adapter = await AccessManagedAdapter.new(this.manager.address);
  14. this.ownable = await Ownable.new(this.adapter.address);
  15. // add user to group
  16. await this.manager.$_grantGroup(groupId, user, 0, 0);
  17. });
  18. it('initial state', async function () {
  19. expect(await this.adapter.authority()).to.be.equal(this.manager.address);
  20. expect(await this.ownable.owner()).to.be.equal(this.adapter.address);
  21. });
  22. describe('Contract is Closed', function () {
  23. beforeEach(async function () {
  24. await this.manager.$_setContractMode(this.ownable.address, AccessMode.Closed);
  25. });
  26. it('directly call: reverts', async function () {
  27. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  28. });
  29. it('relayed call (with group): reverts', async function () {
  30. await expectRevertCustomError(
  31. this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  32. 'AccessManagedUnauthorized',
  33. [user],
  34. );
  35. });
  36. it('relayed call (without group): reverts', async function () {
  37. await expectRevertCustomError(
  38. this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  39. 'AccessManagedUnauthorized',
  40. [other],
  41. );
  42. });
  43. });
  44. describe('Contract is Open', function () {
  45. beforeEach(async function () {
  46. await this.manager.$_setContractMode(this.ownable.address, AccessMode.Open);
  47. });
  48. it('directly call: reverts', async function () {
  49. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  50. });
  51. it('relayed call (with group): success', async function () {
  52. await this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: user });
  53. });
  54. it('relayed call (without group): success', async function () {
  55. await this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: other });
  56. });
  57. });
  58. describe('Contract is in Custom mode', function () {
  59. beforeEach(async function () {
  60. await this.manager.$_setContractMode(this.ownable.address, AccessMode.Custom);
  61. });
  62. describe('function is open to specific group', function () {
  63. beforeEach(async function () {
  64. await this.manager.$_setFunctionAllowedGroup(this.ownable.address, selector('$_checkOwner()'), groupId);
  65. });
  66. it('directly call: reverts', async function () {
  67. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  68. });
  69. it('relayed call (with group): success', async function () {
  70. await this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: user });
  71. });
  72. it('relayed call (without group): reverts', async function () {
  73. await expectRevertCustomError(
  74. this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  75. 'AccessManagedUnauthorized',
  76. [other],
  77. );
  78. });
  79. });
  80. describe('function is open to public group', function () {
  81. beforeEach(async function () {
  82. await this.manager.$_setFunctionAllowedGroup(
  83. this.ownable.address,
  84. selector('$_checkOwner()'),
  85. constants.MAX_UINT256,
  86. );
  87. });
  88. it('directly call: reverts', async function () {
  89. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  90. });
  91. it('relayed call (with group): success', async function () {
  92. await this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: user });
  93. });
  94. it('relayed call (without group): success', async function () {
  95. await this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: other });
  96. });
  97. });
  98. describe('function is available with execution delay', function () {
  99. const delay = 10;
  100. beforeEach(async function () {
  101. await this.manager.$_setExecuteDelay(groupId, user, delay);
  102. await this.manager.$_setFunctionAllowedGroup(this.ownable.address, selector('$_checkOwner()'), groupId);
  103. });
  104. it('unscheduled call reverts', async function () {
  105. await expectRevertCustomError(
  106. this.adapter.relay(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  107. 'AccessManagedRequiredDelay',
  108. [user, delay],
  109. );
  110. });
  111. it('scheduled call succeeds', async function () {
  112. await this.manager.schedule(this.ownable.address, selector('$_checkOwner()'), { from: user });
  113. await time.increase(delay);
  114. await this.manager.relayViaAdapter(this.ownable.address, selector('$_checkOwner()'), this.adapter.address, {
  115. from: user,
  116. });
  117. });
  118. });
  119. });
  120. it('bubble revert reasons', async function () {
  121. const { address } = await Ownable.new(admin);
  122. await this.manager.$_setContractMode(address, AccessMode.Open);
  123. await expectRevertCustomError(
  124. this.adapter.relay(address, selector('$_checkOwner()')),
  125. 'OwnableUnauthorizedAccount',
  126. [this.adapter.address],
  127. );
  128. });
  129. });