From a1eb1d87fe66408f399fec33f859bb39a3fcd93e Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 13 Nov 2024 23:41:08 +0100 Subject: [PATCH] fix: change some tests (#46) * fix: change some tests * fix: ups * fix: improve tests --- test/OwnableWithGuardian.t.sol | 44 ++++++++++++++----- ...l => UpgradeableOwnableWithGuardian.t.sol} | 0 test/create3Test.t.sol | 15 +++---- 3 files changed, 39 insertions(+), 20 deletions(-) rename test/{UpgradeableOwnableWithGuardian.sol => UpgradeableOwnableWithGuardian.t.sol} (100%) diff --git a/test/OwnableWithGuardian.t.sol b/test/OwnableWithGuardian.t.sol index b71ed91..02d794f 100644 --- a/test/OwnableWithGuardian.t.sol +++ b/test/OwnableWithGuardian.t.sol @@ -4,29 +4,53 @@ pragma solidity ^0.8.0; import 'forge-std/Test.sol'; import {OwnableWithGuardian} from '../src/contracts/access-control/OwnableWithGuardian.sol'; -contract ImplOwnableWithGuardian is OwnableWithGuardian {} +contract ImplOwnableWithGuardian is OwnableWithGuardian { + function mock_onlyGuardian() external onlyGuardian {} + + function mock_onlyOwnerOrGuardian() external onlyOwnerOrGuardian {} +} contract TestOfOwnableWithGuardian is Test { - OwnableWithGuardian public withGuardian; + ImplOwnableWithGuardian public withGuardian; + + address owner = makeAddr('owner'); + address guardian = makeAddr('guardian'); function setUp() public { withGuardian = new ImplOwnableWithGuardian(); - } - - function testConstructorLogic() external { assertEq(withGuardian.owner(), address(this)); assertEq(withGuardian.guardian(), address(this)); + withGuardian.transferOwnership(owner); + withGuardian.updateGuardian(guardian); } - function testGuardianUpdate(address guardian) external { - withGuardian.updateGuardian(guardian); + function testConstructorLogic() external view { + assertEq(withGuardian.owner(), owner); + assertEq(withGuardian.guardian(), guardian); + } + + function testGuardianUpdateViaGuardian(address newGuardian) external { + vm.startPrank(guardian); + withGuardian.updateGuardian(newGuardian); } - function testGuardianUpdateNoAccess(address guardian) external { - vm.assume(guardian != address(this)); + function testGuardianUpdateViaOwner(address newGuardian) external { + vm.prank(owner); + withGuardian.updateGuardian(newGuardian); + } - vm.prank(guardian); + function testGuardianUpdateNoAccess() external { vm.expectRevert('ONLY_BY_OWNER_OR_GUARDIAN'); withGuardian.updateGuardian(guardian); } + + function test_onlyGuardianGuard() external { + vm.prank(guardian); + withGuardian.mock_onlyGuardian(); + } + + function test_onlyGuardianGuard_shouldRevert() external { + vm.expectRevert('ONLY_BY_GUARDIAN'); + withGuardian.mock_onlyGuardian(); + } } diff --git a/test/UpgradeableOwnableWithGuardian.sol b/test/UpgradeableOwnableWithGuardian.t.sol similarity index 100% rename from test/UpgradeableOwnableWithGuardian.sol rename to test/UpgradeableOwnableWithGuardian.t.sol diff --git a/test/create3Test.t.sol b/test/create3Test.t.sol index b66a900..7f7dec6 100644 --- a/test/create3Test.t.sol +++ b/test/create3Test.t.sol @@ -32,11 +32,7 @@ contract Create3FactoryTest is Test { factory = new Create3Factory{salt: CREATE3_FACTORY_SALT}(); } - function testCreate3WithoutValue( - address someAddress, - address owner, - bytes32 salt - ) public { + function testCreate3WithoutValue(address someAddress, address owner, bytes32 salt) public { bytes memory encodedParams = abi.encode(someAddress, owner); bytes memory code = type(MockContract).creationCode; // deploy Voting portal @@ -52,13 +48,12 @@ contract Create3FactoryTest is Test { ); assertEq(MockContract(votingPortal).owner(), owner); assertEq(MockContract(votingPortal).SOME_ADDRESS(), someAddress); + + vm.expectRevert(abi.encodeWithSelector(Create3.TargetAlreadyExists.selector)); + factory.create(salt, abi.encodePacked(code, encodedParams)); } - function testCreate3WithValue( - address someAddress, - address owner, - address creator - ) public { + function testCreate3WithValue(address someAddress, address owner, address creator) public { bytes memory encodedParams = abi.encode(someAddress, owner); bytes memory code = type(MockContract).creationCode; bytes32 salt = keccak256(bytes('Voting portal eth-avax-2'));