Skip to content

Commit

Permalink
Implement and test a method to update the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
apbendi committed Jan 4, 2024
1 parent a5d84f9 commit deb36a4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
24 changes: 19 additions & 5 deletions src/V3FactoryOwner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
pragma solidity 0.8.23;

contract V3FactoryOwner {
address public admin;
address public admin;

constructor(address _admin) {
admin = _admin;
}
}
event AdminUpdated(address indexed oldAmin, address indexed newAdmin);

error V3FactoryOwner__Unauthorized();

constructor(address _admin) {
admin = _admin;
}

function setAdmin(address _newAdmin) external {
_revertIfNotAdmin();
emit AdminUpdated(admin, _newAdmin);
admin = _newAdmin;
}

function _revertIfNotAdmin() internal view {
if (msg.sender != admin) revert V3FactoryOwner__Unauthorized();
}
}
58 changes: 43 additions & 15 deletions test/V3FactoryOwner.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,52 @@ import {Test, console2} from "forge-std/Test.sol";
import {V3FactoryOwner} from "src/V3FactoryOwner.sol";

contract V3FactoryOwnerTest is Test {
V3FactoryOwner factoryOwner;
address admin = address(0xb055beef);
V3FactoryOwner factoryOwner;
address admin = address(0xb055beef);

function setUp() public {
vm.label(admin, "Admin");
event AdminUpdated(address indexed oldAmin, address indexed newAdmin);

factoryOwner = new V3FactoryOwner(admin);
vm.label(address(factoryOwner), "Factory Owner");
}
function setUp() public {
vm.label(admin, "Admin");

factoryOwner = new V3FactoryOwner(admin);
vm.label(address(factoryOwner), "Factory Owner");
}
}

contract Constructor is V3FactoryOwnerTest {
function test_SetsTheAdmin() public {
assertEq(factoryOwner.admin(), admin);
}

function testFuzz_SetTheAdminToAnArbitraryAddress(address _admin) public {
V3FactoryOwner _factoryOwner = new V3FactoryOwner(_admin);
assertEq(_factoryOwner.admin(), _admin);
}
function test_SetsTheAdmin() public {
assertEq(factoryOwner.admin(), admin);
}

function testFuzz_SetTheAdminToAnArbitraryAddress(address _admin) public {
V3FactoryOwner _factoryOwner = new V3FactoryOwner(_admin);
assertEq(_factoryOwner.admin(), _admin);
}
}

contract SetAdmin is V3FactoryOwnerTest {
function testFuzz_UpdatesTheAdminWhenCalledByTheCurrentAdmin(address _newAdmin) public {
vm.prank(admin);
factoryOwner.setAdmin(_newAdmin);

assertEq(factoryOwner.admin(), _newAdmin);
}

function testFuzz_EmitsAnEventWhenUpdatingTheAdmin(address _newAdmin) public {
vm.expectEmit(true, true, true, true);
vm.prank(admin);
emit AdminUpdated(admin, _newAdmin);
factoryOwner.setAdmin(_newAdmin);
}

function testFuzz_RevertIf_TheCallerIsNotTheCurrentAdmin(address _notAdmin, address _newAdmin)
public
{
vm.assume(_notAdmin != admin);

vm.expectRevert(V3FactoryOwner.V3FactoryOwner__Unauthorized.selector);
vm.prank(_notAdmin);
factoryOwner.setAdmin(_newAdmin);
}
}

0 comments on commit deb36a4

Please sign in to comment.