Skip to content

Commit 08a92dd

Browse files
committed
Add upgrade unit test
1 parent a11ae61 commit 08a92dd

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
/******************************************************************************
4+
* Copyright 2024-2025 IEXEC BLOCKCHAIN TECH *
5+
* *
6+
* Licensed under the Apache License, Version 2.0 (the "License"); *
7+
* you may not use this file except in compliance with the License. *
8+
* You may obtain a copy of the License at *
9+
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, software *
13+
* distributed under the License is distributed on an "AS IS" BASIS, *
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15+
* See the License for the specific language governing permissions and *
16+
* limitations under the License. *
17+
******************************************************************************/
18+
19+
pragma solidity ^0.8.24;
20+
21+
import {ERC721BurnableUpgradeable, ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721BurnableUpgradeable.sol";
22+
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
23+
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
24+
import {MulticallUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";
25+
import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
26+
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
27+
import {DataProtectorSharing} from "../DataProtectorSharing.sol";
28+
import {AddOnlyAppWhitelistRegistry, IAddOnlyAppWhitelist} from "../registry/AddOnlyAppWhitelistRegistry.sol";
29+
import {IRegistry} from "../interfaces/IRegistry.sol";
30+
import {ManageOrders} from "../ManageOrders.sol";
31+
32+
/**
33+
* @notice This contract is for upgradeability testing purposes only.
34+
*/
35+
36+
contract DataProtectorSharingV2Mock is DataProtectorSharing {
37+
38+
string public newStorage;
39+
40+
/// @custom:oz-upgrades-unsafe-allow constructor
41+
constructor() DataProtectorSharing(
42+
address(0),
43+
IRegistry(address(0)),
44+
AddOnlyAppWhitelistRegistry(address(0))
45+
) {}
46+
47+
function initializeV2(string calldata foo) public reinitializer(2) {
48+
newStorage = foo;
49+
}
50+
}

packages/sharing-smart-contract/hardhat.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('@nomicfoundation/hardhat-foundry');
22
require('@nomicfoundation/hardhat-toolbox');
33
require('@openzeppelin/hardhat-upgrades');
44
require('hardhat-contract-sizer');
5+
require('@openzeppelin/hardhat-upgrades');
56
require('dotenv').config();
67

78
const { WALLET_PRIVATE_KEY } = process.env;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { loadFixture } from '@nomicfoundation/hardhat-toolbox/network-helpers.js';
2+
import { expect } from 'chai';
3+
import hardhat from 'hardhat';
4+
const { ethers, upgrades } = hardhat;
5+
import { createCollection, deploySCFixture } from './utils/loadFixture.test.js';
6+
7+
// TODO convert to ts.
8+
// TODO use typechain for factories.
9+
10+
let dataProtectorSharingContract;
11+
let contractAddress;
12+
let owner, addr1;
13+
14+
describe('DataProtectorSharing', function () {
15+
beforeEach('Deploy fixture', async function () {
16+
({ dataProtectorSharingContract, owner, addr1 } = await loadFixture(deploySCFixture));
17+
contractAddress = await dataProtectorSharingContract.getAddress();
18+
await dataProtectorSharingContract.createCollection(owner).then((tx) => tx.wait());
19+
});
20+
21+
describe('Upgrade', function () {
22+
it('Should upgrade', async function () {
23+
// Update the state of the original proxy.
24+
const { collectionTokenId } = await createCollection();
25+
// Upgrade the proxy.
26+
const newImplementationFactory = await ethers.getContractFactory(
27+
'DataProtectorSharingV2Mock',
28+
owner,
29+
);
30+
await upgrades
31+
.upgradeProxy(contractAddress, newImplementationFactory)
32+
.then((contract) => contract.waitForDeployment());
33+
const dataProtectorSharingContractV2 = await ethers.getContractAt(
34+
'DataProtectorSharingV2Mock',
35+
contractAddress,
36+
);
37+
await dataProtectorSharingContractV2.initializeV2('bar');
38+
// Check V1 storage.
39+
expect(await dataProtectorSharingContractV2.ownerOf(collectionTokenId)).to.equal(
40+
addr1.address,
41+
);
42+
// Check V2 storage.
43+
expect(await dataProtectorSharingContractV2.newStorage()).to.equal('bar');
44+
// Check address.
45+
expect(await dataProtectorSharingContractV2.getAddress()).to.equal(contractAddress);
46+
});
47+
48+
it('TODO - Should not upgrade when storage is broken', async function () {});
49+
});
50+
});

0 commit comments

Comments
 (0)