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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -7,4 +7,6 @@ async function getChainId() {
|
||||
|
||||
module.exports = {
|
||||
getChainId,
|
||||
// TODO: when tests are ready to support bigint chainId
|
||||
// getChainId: ethers.provider.getNetwork().then(network => network.chainId),
|
||||
};
|
||||
|
||||
@ -1,22 +1,6 @@
|
||||
const RLP = require('rlp');
|
||||
|
||||
function computeCreateAddress(deployer, nonce) {
|
||||
return web3.utils.toChecksumAddress(web3.utils.sha3(RLP.encode([deployer.address ?? deployer, nonce])).slice(-40));
|
||||
}
|
||||
|
||||
function computeCreate2Address(saltHex, bytecode, deployer) {
|
||||
return web3.utils.toChecksumAddress(
|
||||
web3.utils
|
||||
.sha3(
|
||||
`0x${['ff', deployer.address ?? deployer, saltHex, web3.utils.soliditySha3(bytecode)]
|
||||
.map(x => x.replace(/0x/, ''))
|
||||
.join('')}`,
|
||||
)
|
||||
.slice(-40),
|
||||
);
|
||||
}
|
||||
const { ethers } = require('hardhat');
|
||||
|
||||
module.exports = {
|
||||
computeCreateAddress,
|
||||
computeCreate2Address,
|
||||
computeCreateAddress: (from, nonce) => ethers.getCreateAddress({ from, nonce }),
|
||||
computeCreate2Address: (salt, bytecode, from) => ethers.getCreate2Address(from, salt, ethers.keccak256(bytecode)),
|
||||
};
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
// DEPRECATED: replace with hardhat-toolbox chai matchers.
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
/** Revert handler that supports custom errors. */
|
||||
|
||||
40
test/sanity.test.js
Normal file
40
test/sanity.test.js
Normal file
@ -0,0 +1,40 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture, mine } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
async function fixture() {
|
||||
const signers = await ethers.getSigners();
|
||||
const addresses = await Promise.all(signers.map(s => s.getAddress()));
|
||||
return { signers, addresses };
|
||||
}
|
||||
|
||||
contract('Environment sanity', function (accounts) {
|
||||
beforeEach(async function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('[skip-on-coverage] signers', function () {
|
||||
it('match accounts', async function () {
|
||||
expect(this.addresses).to.deep.equal(accounts);
|
||||
});
|
||||
|
||||
it('signer #0 is skipped', async function () {
|
||||
const signer = await ethers.provider.getSigner(0);
|
||||
expect(this.addresses).to.not.include(await signer.getAddress());
|
||||
});
|
||||
});
|
||||
|
||||
describe('snapshot', function () {
|
||||
let blockNumberBefore;
|
||||
|
||||
it('cache and mine', async function () {
|
||||
blockNumberBefore = await ethers.provider.getBlockNumber();
|
||||
await mine();
|
||||
expect(await ethers.provider.getBlockNumber()).to.be.equal(blockNumberBefore + 1);
|
||||
});
|
||||
|
||||
it('check snapshot', async function () {
|
||||
expect(await ethers.provider.getBlockNumber()).to.be.equal(blockNumberBefore);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -108,7 +108,7 @@ function shouldBehaveLikeERC2981() {
|
||||
const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
|
||||
|
||||
// must be different even at the same this.salePrice
|
||||
expect(token1Info[1]).to.not.be.equal(token2Info.royaltyFraction);
|
||||
expect(token1Info[1]).to.not.be.bignumber.equal(token2Info[1]);
|
||||
});
|
||||
|
||||
it('reverts if invalid parameters', async function () {
|
||||
|
||||
Reference in New Issue
Block a user