Skip to content

Instantly share code, notes, and snippets.

@coffiasd
Created July 26, 2023 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coffiasd/84552b5a33845fa567bfc3aa5204d460 to your computer and use it in GitHub Desktop.
Save coffiasd/84552b5a33845fa567bfc3aa5204d460 to your computer and use it in GitHub Desktop.
how to master openzeppelin access control
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import {MyContract} from "../src/Access.sol";
contract AccessTest is Test {
MyContract public myContract;
address owner = address(0x100);
address arbitrary = address(0x101);
function setUp() public {
//let's set address owner to msg.sender.
vm.prank(owner);
//create the contract.
myContract = new MyContract();
}
function testAccessControl() public {
//check the .owner() eq owner.
assert(owner == myContract.owner());
//use arbitrary address invoke specialThing() function.
vm.prank(arbitrary);
vm.expectRevert("Ownable: caller is not the owner");
myContract.specialThing();
//switch to owner.
vm.prank(owner);
myContract.specialThing();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment