From 2aecdcec8d559f6b407ffffb9b3f89df42fcae9d Mon Sep 17 00:00:00 2001 From: marcomariscal <42938673+marcomariscal@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:46:20 -0800 Subject: [PATCH] feat: update tests to handle new auth functionality --- l2-contracts/test/ZkCappedMinterV2.t.sol | 53 ++++++++++++++----- .../test/ZkCappedMinterV2Factory.t.sol | 2 +- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/l2-contracts/test/ZkCappedMinterV2.t.sol b/l2-contracts/test/ZkCappedMinterV2.t.sol index 91aa90e..1e86c48 100644 --- a/l2-contracts/test/ZkCappedMinterV2.t.sol +++ b/l2-contracts/test/ZkCappedMinterV2.t.sol @@ -26,7 +26,7 @@ contract Constructor is ZkCappedMinterV2Test { _cap = bound(_cap, 0, MAX_MINT_SUPPLY); ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap); assertEq(address(cappedMinter.TOKEN()), address(token)); - assertEq(cappedMinter.ADMIN(), _cappedMinterAdmin); + assertEq(cappedMinter.hasRole(DEFAULT_ADMIN_ROLE, _cappedMinterAdmin), true); assertEq(cappedMinter.CAP(), _cap); } } @@ -34,6 +34,7 @@ contract Constructor is ZkCappedMinterV2Test { contract Mint is ZkCappedMinterV2Test { function testFuzz_MintsNewTokensWhenTheAmountRequestedIsBelowTheCap( address _cappedMinterAdmin, + address _minter, address _receiver, uint256 _cap, uint256 _amount @@ -42,14 +43,21 @@ contract Mint is ZkCappedMinterV2Test { _amount = bound(_amount, 1, MAX_MINT_SUPPLY); vm.assume(_cap > _amount); vm.assume(_receiver != address(0) && _receiver != initMintReceiver); + vm.assume(_minter != address(0)); + ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap); + vm.prank(_cappedMinterAdmin); + cappedMinter.grantRole(MINTER_ROLE, _minter); + + vm.prank(_minter); cappedMinter.mint(_receiver, _amount); assertEq(token.balanceOf(_receiver), _amount); } function testFuzz_MintsNewTokensInSuccessionToDifferentAccountsWhileRemainingBelowCap( address _cappedMinterAdmin, + address _minter, address _receiver1, address _receiver2, uint256 _cap, @@ -63,38 +71,57 @@ contract Mint is ZkCappedMinterV2Test { vm.assume(_receiver1 != address(0) && _receiver1 != initMintReceiver); vm.assume(_receiver2 != address(0) && _receiver2 != initMintReceiver); vm.assume(_receiver1 != _receiver2); + vm.assume(_minter != address(0)); + ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap); - vm.startPrank(_cappedMinterAdmin); + + vm.prank(_cappedMinterAdmin); + cappedMinter.grantRole(MINTER_ROLE, _minter); + + vm.startPrank(_minter); cappedMinter.mint(_receiver1, _amount1); cappedMinter.mint(_receiver2, _amount2); vm.stopPrank(); + assertEq(token.balanceOf(_receiver1), _amount1); assertEq(token.balanceOf(_receiver2), _amount2); } - function testFuzz_RevertIf_MintAttemptedByNonAdmin(address _cappedMinterAdmin, uint256 _cap, address _nonAdmin) + function testFuzz_RevertIf_MintAttemptedByNonMinter(address _cappedMinterAdmin, address _nonMinter, uint256 _cap) public { _cap = bound(_cap, 0, MAX_MINT_SUPPLY); - vm.assume(_nonAdmin != _cappedMinterAdmin); - ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap); - vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _nonAdmin)); - vm.startPrank(_nonAdmin); - cappedMinter.mint(_nonAdmin, _cap); + + vm.assume(_nonMinter != address(0)); + vm.assume(!cappedMinter.hasRole(MINTER_ROLE, _nonMinter)); + + vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _nonMinter)); + vm.prank(_nonMinter); + cappedMinter.mint(_nonMinter, _cap); } - function testFuzz_RevertIf_CapExceededOnMint(address _cappedMinterAdmin, address _receiver, uint256 _cap) public { + function testFuzz_RevertIf_CapExceededOnMint( + address _cappedMinterAdmin, + address _minter, + address _receiver, + uint256 _cap + ) public { _cap = bound(_cap, 4, MAX_MINT_SUPPLY); vm.assume(_receiver != address(0) && _receiver != initMintReceiver); + vm.assume(_minter != address(0)); + ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap); + vm.prank(_cappedMinterAdmin); + cappedMinter.grantRole(MINTER_ROLE, _minter); + + vm.prank(_minter); cappedMinter.mint(_receiver, _cap); assertEq(token.balanceOf(_receiver), _cap); - vm.expectRevert( - abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__CapExceeded.selector, _cappedMinterAdmin, _cap) - ); - vm.prank(_cappedMinterAdmin); + + vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__CapExceeded.selector, _minter, _cap)); + vm.prank(_minter); cappedMinter.mint(_receiver, _cap); } } diff --git a/l2-contracts/test/ZkCappedMinterV2Factory.t.sol b/l2-contracts/test/ZkCappedMinterV2Factory.t.sol index d111e82..5a87ff9 100644 --- a/l2-contracts/test/ZkCappedMinterV2Factory.t.sol +++ b/l2-contracts/test/ZkCappedMinterV2Factory.t.sol @@ -44,7 +44,7 @@ contract CreateCappedMinter is ZkCappedMinterV2FactoryTest { ZkCappedMinterV2 minter = ZkCappedMinterV2(minterAddress); assertEq(address(minter.TOKEN()), address(token)); - assertEq(minter.ADMIN(), _cappedMinterAdmin); + assertEq(minter.hasRole(DEFAULT_ADMIN_ROLE, _cappedMinterAdmin), true); assertEq(minter.CAP(), _cap); }