mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openzeppelin-contracts/test/proxy/transparent/ProxyAdmin.test.js

83 lines
3.1 KiB

4 months ago
const { ethers } = require('hardhat');
1 year ago
const { expect } = require('chai');
4 months ago
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
1 year ago
4 months ago
const { getAddressInSlot, ImplementationSlot } = require('../../helpers/storage');
1 year ago
4 months ago
async function fixture() {
const [admin, other] = await ethers.getSigners();
1 year ago
4 months ago
const v1 = await ethers.deployContract('DummyImplementation');
const v2 = await ethers.deployContract('DummyImplementationV2');
1 year ago
4 months ago
const proxy = await ethers
.deployContract('TransparentUpgradeableProxy', [v1, admin, '0x'])
.then(instance => ethers.getContractAt('ITransparentUpgradeableProxy', instance));
const proxyAdmin = await ethers.getContractAt(
'ProxyAdmin',
ethers.getCreateAddress({ from: proxy.target, nonce: 1n }),
);
1 year ago
4 months ago
return { admin, other, v1, v2, proxy, proxyAdmin };
}
1 year ago
4 months ago
describe('ProxyAdmin', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
1 year ago
});
it('has an owner', async function () {
4 months ago
expect(await this.proxyAdmin.owner()).to.equal(this.admin);
1 year ago
});
it('has an interface version', async function () {
expect(await this.proxyAdmin.UPGRADE_INTERFACE_VERSION()).to.equal('5.0.0');
});
describe('without data', function () {
4 months ago
describe('with unauthorized account', function () {
1 year ago
it('fails to upgrade', async function () {
4 months ago
await expect(this.proxyAdmin.connect(this.other).upgradeAndCall(this.proxy, this.v2, '0x'))
.to.be.revertedWithCustomError(this.proxyAdmin, 'OwnableUnauthorizedAccount')
.withArgs(this.other);
1 year ago
});
});
4 months ago
describe('with authorized account', function () {
1 year ago
it('upgrades implementation', async function () {
4 months ago
await this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, '0x');
expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.v2);
1 year ago
});
});
});
describe('with data', function () {
4 months ago
describe('with unauthorized account', function () {
1 year ago
it('fails to upgrade', async function () {
4 months ago
const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [1337n]);
await expect(this.proxyAdmin.connect(this.other).upgradeAndCall(this.proxy, this.v2, data))
.to.be.revertedWithCustomError(this.proxyAdmin, 'OwnableUnauthorizedAccount')
.withArgs(this.other);
1 year ago
});
});
4 months ago
describe('with authorized account', function () {
describe('with invalid callData', function () {
1 year ago
it('fails to upgrade', async function () {
4 months ago
const data = '0x12345678';
await expect(this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, data)).to.be.reverted;
1 year ago
});
});
4 months ago
describe('with valid callData', function () {
1 year ago
it('upgrades implementation', async function () {
4 months ago
const data = this.v2.interface.encodeFunctionData('initializeNonPayableWithValue', [1337n]);
await this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, data);
expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.v2);
1 year ago
});
});
});
});
});