Migrate Ownable tests (#4657)
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,72 +1,73 @@
|
||||
const { constants, expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expectRevertCustomError } = require('../helpers/customError');
|
||||
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
const Ownable = artifacts.require('$Ownable');
|
||||
|
||||
contract('Ownable', function (accounts) {
|
||||
const [owner, other] = accounts;
|
||||
async function fixture() {
|
||||
const [owner, other] = await ethers.getSigners();
|
||||
const ownable = await ethers.deployContract('$Ownable', [owner]);
|
||||
return { owner, other, ownable };
|
||||
}
|
||||
|
||||
describe('Ownable', function () {
|
||||
beforeEach(async function () {
|
||||
this.ownable = await Ownable.new(owner);
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
it('rejects zero address for initialOwner', async function () {
|
||||
await expectRevertCustomError(Ownable.new(constants.ZERO_ADDRESS), 'OwnableInvalidOwner', [constants.ZERO_ADDRESS]);
|
||||
await expect(ethers.deployContract('$Ownable', [ethers.ZeroAddress]))
|
||||
.to.be.revertedWithCustomError({ interface: this.ownable.interface }, 'OwnableInvalidOwner')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('has an owner', async function () {
|
||||
expect(await this.ownable.owner()).to.equal(owner);
|
||||
expect(await this.ownable.owner()).to.equal(this.owner.address);
|
||||
});
|
||||
|
||||
describe('transfer ownership', function () {
|
||||
it('changes owner after transfer', async function () {
|
||||
const receipt = await this.ownable.transferOwnership(other, { from: owner });
|
||||
expectEvent(receipt, 'OwnershipTransferred');
|
||||
await expect(this.ownable.connect(this.owner).transferOwnership(this.other))
|
||||
.to.emit(this.ownable, 'OwnershipTransferred')
|
||||
.withArgs(this.owner.address, this.other.address);
|
||||
|
||||
expect(await this.ownable.owner()).to.equal(other);
|
||||
expect(await this.ownable.owner()).to.equal(this.other.address);
|
||||
});
|
||||
|
||||
it('prevents non-owners from transferring', async function () {
|
||||
await expectRevertCustomError(
|
||||
this.ownable.transferOwnership(other, { from: other }),
|
||||
'OwnableUnauthorizedAccount',
|
||||
[other],
|
||||
);
|
||||
await expect(this.ownable.connect(this.other).transferOwnership(this.other))
|
||||
.to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
|
||||
.withArgs(this.other.address);
|
||||
});
|
||||
|
||||
it('guards ownership against stuck state', async function () {
|
||||
await expectRevertCustomError(
|
||||
this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
|
||||
'OwnableInvalidOwner',
|
||||
[ZERO_ADDRESS],
|
||||
);
|
||||
await expect(this.ownable.connect(this.owner).transferOwnership(ethers.ZeroAddress))
|
||||
.to.be.revertedWithCustomError(this.ownable, 'OwnableInvalidOwner')
|
||||
.withArgs(ethers.ZeroAddress);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renounce ownership', function () {
|
||||
it('loses ownership after renouncement', async function () {
|
||||
const receipt = await this.ownable.renounceOwnership({ from: owner });
|
||||
expectEvent(receipt, 'OwnershipTransferred');
|
||||
await expect(this.ownable.connect(this.owner).renounceOwnership())
|
||||
.to.emit(this.ownable, 'OwnershipTransferred')
|
||||
.withArgs(this.owner.address, ethers.ZeroAddress);
|
||||
|
||||
expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
|
||||
expect(await this.ownable.owner()).to.equal(ethers.ZeroAddress);
|
||||
});
|
||||
|
||||
it('prevents non-owners from renouncement', async function () {
|
||||
await expectRevertCustomError(this.ownable.renounceOwnership({ from: other }), 'OwnableUnauthorizedAccount', [
|
||||
other,
|
||||
]);
|
||||
await expect(this.ownable.connect(this.other).renounceOwnership())
|
||||
.to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
|
||||
.withArgs(this.other.address);
|
||||
});
|
||||
|
||||
it('allows to recover access using the internal _transferOwnership', async function () {
|
||||
await this.ownable.renounceOwnership({ from: owner });
|
||||
const receipt = await this.ownable.$_transferOwnership(other);
|
||||
expectEvent(receipt, 'OwnershipTransferred');
|
||||
await this.ownable.connect(this.owner).renounceOwnership();
|
||||
|
||||
expect(await this.ownable.owner()).to.equal(other);
|
||||
await expect(this.ownable.$_transferOwnership(this.other))
|
||||
.to.emit(this.ownable, 'OwnershipTransferred')
|
||||
.withArgs(ethers.ZeroAddress, this.other.address);
|
||||
|
||||
expect(await this.ownable.owner()).to.equal(this.other.address);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user