Migrate Ownable tests (#4657)

Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Hadrien Croubois
2023-10-17 10:05:58 +02:00
committed by GitHub
parent aed22fbc22
commit 149e1b79fe
13 changed files with 1248 additions and 168 deletions

View File

@ -1,70 +1,85 @@
const { constants, expectEvent } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { expectRevertCustomError } = require('../helpers/customError');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const Ownable2Step = artifacts.require('$Ownable2Step');
contract('Ownable2Step', function (accounts) {
const [owner, accountA, accountB] = accounts;
async function fixture() {
const [owner, accountA, accountB] = await ethers.getSigners();
const ownable2Step = await ethers.deployContract('$Ownable2Step', [owner]);
return {
ownable2Step,
owner,
accountA,
accountB,
};
}
describe('Ownable2Step', function () {
beforeEach(async function () {
this.ownable2Step = await Ownable2Step.new(owner);
Object.assign(this, await loadFixture(fixture));
});
describe('transfer ownership', function () {
it('starting a transfer does not change owner', async function () {
const receipt = await this.ownable2Step.transferOwnership(accountA, { from: owner });
expectEvent(receipt, 'OwnershipTransferStarted', { previousOwner: owner, newOwner: accountA });
expect(await this.ownable2Step.owner()).to.equal(owner);
expect(await this.ownable2Step.pendingOwner()).to.equal(accountA);
await expect(this.ownable2Step.connect(this.owner).transferOwnership(this.accountA))
.to.emit(this.ownable2Step, 'OwnershipTransferStarted')
.withArgs(this.owner.address, this.accountA.address);
expect(await this.ownable2Step.owner()).to.equal(this.owner.address);
expect(await this.ownable2Step.pendingOwner()).to.equal(this.accountA.address);
});
it('changes owner after transfer', async function () {
await this.ownable2Step.transferOwnership(accountA, { from: owner });
const receipt = await this.ownable2Step.acceptOwnership({ from: accountA });
expectEvent(receipt, 'OwnershipTransferred', { previousOwner: owner, newOwner: accountA });
expect(await this.ownable2Step.owner()).to.equal(accountA);
expect(await this.ownable2Step.pendingOwner()).to.not.equal(accountA);
await this.ownable2Step.connect(this.owner).transferOwnership(this.accountA);
await expect(this.ownable2Step.connect(this.accountA).acceptOwnership())
.to.emit(this.ownable2Step, 'OwnershipTransferred')
.withArgs(this.owner.address, this.accountA.address);
expect(await this.ownable2Step.owner()).to.equal(this.accountA.address);
expect(await this.ownable2Step.pendingOwner()).to.equal(ethers.ZeroAddress);
});
it('guards transfer against invalid user', async function () {
await this.ownable2Step.transferOwnership(accountA, { from: owner });
await expectRevertCustomError(
this.ownable2Step.acceptOwnership({ from: accountB }),
'OwnableUnauthorizedAccount',
[accountB],
);
await this.ownable2Step.connect(this.owner).transferOwnership(this.accountA);
await expect(this.ownable2Step.connect(this.accountB).acceptOwnership())
.to.be.revertedWithCustomError(this.ownable2Step, 'OwnableUnauthorizedAccount')
.withArgs(this.accountB.address);
});
});
describe('renouncing ownership', async function () {
it('changes owner after renouncing ownership', async function () {
await this.ownable2Step.renounceOwnership({ from: owner });
await expect(this.ownable2Step.connect(this.owner).renounceOwnership())
.to.emit(this.ownable2Step, 'OwnershipTransferred')
.withArgs(this.owner.address, ethers.ZeroAddress);
// If renounceOwnership is removed from parent an alternative is needed ...
// without it is difficult to cleanly renounce with the two step process
// see: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3620#discussion_r957930388
expect(await this.ownable2Step.owner()).to.equal(ZERO_ADDRESS);
expect(await this.ownable2Step.owner()).to.equal(ethers.ZeroAddress);
});
it('pending owner resets after renouncing ownership', async function () {
await this.ownable2Step.transferOwnership(accountA, { from: owner });
expect(await this.ownable2Step.pendingOwner()).to.equal(accountA);
await this.ownable2Step.renounceOwnership({ from: owner });
expect(await this.ownable2Step.pendingOwner()).to.equal(ZERO_ADDRESS);
await expectRevertCustomError(
this.ownable2Step.acceptOwnership({ from: accountA }),
'OwnableUnauthorizedAccount',
[accountA],
);
await this.ownable2Step.connect(this.owner).transferOwnership(this.accountA);
expect(await this.ownable2Step.pendingOwner()).to.equal(this.accountA.address);
await this.ownable2Step.connect(this.owner).renounceOwnership();
expect(await this.ownable2Step.pendingOwner()).to.equal(ethers.ZeroAddress);
await expect(this.ownable2Step.connect(this.accountA).acceptOwnership())
.to.be.revertedWithCustomError(this.ownable2Step, 'OwnableUnauthorizedAccount')
.withArgs(this.accountA.address);
});
it('allows to recover access using the internal _transferOwnership', async function () {
await this.ownable2Step.renounceOwnership({ from: owner });
const receipt = await this.ownable2Step.$_transferOwnership(accountA);
expectEvent(receipt, 'OwnershipTransferred');
await this.ownable2Step.connect(this.owner).renounceOwnership();
expect(await this.ownable2Step.owner()).to.equal(accountA);
await expect(this.ownable2Step.$_transferOwnership(this.accountA))
.to.emit(this.ownable2Step, 'OwnershipTransferred')
.withArgs(ethers.ZeroAddress, this.accountA.address);
expect(await this.ownable2Step.owner()).to.equal(this.accountA.address);
});
});
});