-
-
Save coffiasd/340241d63980dc9d423e4ece2f9b20db to your computer and use it in GitHub Desktop.
Role-Based Access Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.13; | |
import "forge-std/Test.sol"; | |
import {MyToken2} from "../src/Access2.sol"; | |
contract Access2Test is Test { | |
MyToken2 public myToken; | |
address owner = address(0x100); | |
address arbitrary = address(0x101); | |
function setUp() public { | |
//create the contract. | |
myToken = new MyToken2(owner); | |
} | |
function testAccessControl() public { | |
//use arbitrary address invoke mint() function. | |
vm.prank(arbitrary); | |
vm.expectRevert("Caller is not a minter"); | |
myToken.mint(address(this), 1); | |
//switch to owner. | |
vm.prank(owner); | |
myToken.mint(address(this), 1); | |
vm.prank(owner); | |
myToken.grantRole(keccak256("MINTER_ROLE"), arbitrary); | |
vm.prank(arbitrary); | |
myToken.mint(address(this), 1); | |
vm.prank(arbitrary); | |
vm.expectRevert(); | |
myToken.grantRole(keccak256("MINTER_ROLE"), address(0x1001)); | |
vm.prank(owner); | |
myToken.revokeRole(keccak256("MINTER_ROLE"), arbitrary); | |
vm.prank(arbitrary); | |
vm.expectRevert("Caller is not a minter"); | |
myToken.mint(address(this), 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment