Ownable2Step.test.js 2.6 KB

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