Ownable2Step.test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const { constants, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const { expectRevertCustomError } = require('../helpers/customError');
  5. const Ownable2Step = artifacts.require('$Ownable2Step');
  6. contract('Ownable2Step', function (accounts) {
  7. const [owner, accountA, accountB] = accounts;
  8. beforeEach(async function () {
  9. this.ownable2Step = await Ownable2Step.new(owner);
  10. });
  11. describe('transfer ownership', function () {
  12. it('starting a transfer does not change owner', async function () {
  13. const receipt = await this.ownable2Step.transferOwnership(accountA, { from: owner });
  14. expectEvent(receipt, 'OwnershipTransferStarted', { previousOwner: owner, newOwner: accountA });
  15. expect(await this.ownable2Step.owner()).to.equal(owner);
  16. expect(await this.ownable2Step.pendingOwner()).to.equal(accountA);
  17. });
  18. it('changes owner after transfer', async function () {
  19. await this.ownable2Step.transferOwnership(accountA, { from: owner });
  20. const receipt = await this.ownable2Step.acceptOwnership({ from: accountA });
  21. expectEvent(receipt, 'OwnershipTransferred', { previousOwner: owner, newOwner: accountA });
  22. expect(await this.ownable2Step.owner()).to.equal(accountA);
  23. expect(await this.ownable2Step.pendingOwner()).to.not.equal(accountA);
  24. });
  25. it('guards transfer against invalid user', async function () {
  26. await this.ownable2Step.transferOwnership(accountA, { from: owner });
  27. await expectRevertCustomError(
  28. this.ownable2Step.acceptOwnership({ from: accountB }),
  29. 'OwnableUnauthorizedAccount',
  30. [accountB],
  31. );
  32. });
  33. });
  34. describe('renouncing ownership', async function () {
  35. it('changes owner after renouncing ownership', async function () {
  36. await this.ownable2Step.renounceOwnership({ from: owner });
  37. // If renounceOwnership is removed from parent an alternative is needed ...
  38. // without it is difficult to cleanly renounce with the two step process
  39. // see: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3620#discussion_r957930388
  40. expect(await this.ownable2Step.owner()).to.equal(ZERO_ADDRESS);
  41. });
  42. it('pending owner resets after renouncing ownership', async function () {
  43. await this.ownable2Step.transferOwnership(accountA, { from: owner });
  44. expect(await this.ownable2Step.pendingOwner()).to.equal(accountA);
  45. await this.ownable2Step.renounceOwnership({ from: owner });
  46. expect(await this.ownable2Step.pendingOwner()).to.equal(ZERO_ADDRESS);
  47. await expectRevertCustomError(
  48. this.ownable2Step.acceptOwnership({ from: accountA }),
  49. 'OwnableUnauthorizedAccount',
  50. [accountA],
  51. );
  52. });
  53. it('allows to recover access using the internal _transferOwnership', async function () {
  54. await this.ownable2Step.renounceOwnership({ from: owner });
  55. const receipt = await this.ownable2Step.$_transferOwnership(accountA);
  56. expectEvent(receipt, 'OwnershipTransferred');
  57. expect(await this.ownable2Step.owner()).to.equal(accountA);
  58. });
  59. });
  60. });