AccessManaged.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const {
  2. expectEvent,
  3. expectRevert,
  4. constants: { ZERO_ADDRESS },
  5. } = require('@openzeppelin/test-helpers');
  6. const AccessManaged = artifacts.require('$AccessManagedMock');
  7. const SimpleAuthority = artifacts.require('SimpleAuthority');
  8. contract('AccessManaged', function (accounts) {
  9. const [authority, other, user] = accounts;
  10. it('construction', async function () {
  11. const managed = await AccessManaged.new(authority);
  12. expectEvent.inConstruction(managed, 'AuthorityUpdated', {
  13. oldAuthority: ZERO_ADDRESS,
  14. newAuthority: authority,
  15. });
  16. expect(await managed.authority()).to.equal(authority);
  17. });
  18. describe('setAuthority', function () {
  19. it(`current authority can change managed's authority`, async function () {
  20. const managed = await AccessManaged.new(authority);
  21. const set = await managed.setAuthority(other, { from: authority });
  22. expectEvent(set, 'AuthorityUpdated', {
  23. sender: authority,
  24. newAuthority: other,
  25. });
  26. expect(await managed.authority()).to.equal(other);
  27. });
  28. it(`other account cannot change managed's authority`, async function () {
  29. const managed = await AccessManaged.new(authority);
  30. await expectRevert(managed.setAuthority(other, { from: other }), 'AccessManaged: not current authority');
  31. });
  32. });
  33. describe('restricted', function () {
  34. const selector = web3.eth.abi.encodeFunctionSignature('restrictedFunction()');
  35. it('allows if authority returns true', async function () {
  36. const authority = await SimpleAuthority.new();
  37. const managed = await AccessManaged.new(authority.address);
  38. await authority.setAllowed(user, managed.address, selector);
  39. const restricted = await managed.restrictedFunction({ from: user });
  40. expectEvent(restricted, 'RestrictedRan');
  41. });
  42. it('reverts if authority returns false', async function () {
  43. const authority = await SimpleAuthority.new();
  44. const managed = await AccessManaged.new(authority.address);
  45. await expectRevert(managed.restrictedFunction({ from: user }), 'AccessManaged: authority rejected');
  46. });
  47. });
  48. });