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