generated from storyprotocol/solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Raul
committed
Nov 29, 2023
1 parent
240cf6f
commit f5c0f85
Showing
10 changed files
with
192 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: BUSDL-1.1 | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Test.sol"; | ||
import { AccessControlHelper } from "test/foundry/utils/AccessControlHelper.sol"; | ||
import { Errors } from "contracts/lib/Errors.sol"; | ||
import { AccessControl } from "contracts/lib/AccessControl.sol"; | ||
import { AccessControlled } from "contracts/access-control/AccessControlled.sol"; | ||
import { AccessControlSingleton } from "contracts/access-control/AccessControlSingleton.sol"; | ||
import { MockAccessControlled } from "test/foundry/mocks/MockAccessControlled.sol"; | ||
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol"; | ||
|
||
contract AccessControlledTest is Test, AccessControlHelper { | ||
|
||
event AccessControlUpdated(address indexed accessControl); | ||
|
||
MockAccessControlled accessControlled; | ||
|
||
function setUp() public { | ||
_setupAccessControl(); | ||
accessControlled = new MockAccessControlled(address(accessControl)); | ||
} | ||
|
||
function test_AccessControlled_onlyRole() public { | ||
bytes32 role = keccak256("TEST_ROLE"); | ||
_grantRole(vm, role, address(this)); | ||
accessControlled.exposeOnlyRole(role); | ||
} | ||
|
||
function test_AccessControlled_revert_onlyRole() public { | ||
bytes32 role = keccak256("TEST_ROLE"); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
Errors.MissingRole.selector, | ||
role, | ||
address(this) | ||
) | ||
); | ||
accessControlled.exposeOnlyRole(role); | ||
} | ||
|
||
function test_AccessControlled_setAccessControl() public { | ||
AccessControlSingleton ac2 = new AccessControlSingleton(); | ||
vm.expectEmit(true, true, true, true); | ||
emit AccessControlUpdated(address(ac2)); | ||
vm.prank(admin); | ||
accessControlled.setAccessControl(address(ac2)); | ||
} | ||
|
||
function test_AccessControlled_revert_setAccessControlNotProtocolAdmin() public { | ||
AccessControlSingleton ac2 = new AccessControlSingleton(); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
Errors.MissingRole.selector, | ||
AccessControl.PROTOCOL_ADMIN_ROLE, | ||
address(this) | ||
) | ||
); | ||
accessControlled.setAccessControl(address(ac2)); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
test/foundry/access-control/AccessControlledUpgradeable.t.sol
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: BUSDL-1.1 | ||
pragma solidity ^0.8.13; | ||
|
||
import "forge-std/Test.sol"; | ||
import { AccessControlHelper } from "test/foundry/utils/AccessControlHelper.sol"; | ||
import { Errors } from "contracts/lib/Errors.sol"; | ||
import { AccessControl } from "contracts/lib/AccessControl.sol"; | ||
import { AccessControlledUpgradeable } from "contracts/access-control/AccessControlledUpgradeable.sol"; | ||
import { AccessControlSingleton } from "contracts/access-control/AccessControlSingleton.sol"; | ||
import { MockAccessControlledUpgradeable } from "test/foundry/mocks/MockAccessControlledUpgradeable.sol"; | ||
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol"; | ||
|
||
contract AccessControlledUpgradeableTest is Test, AccessControlHelper { | ||
|
||
event AccessControlUpdated(address indexed accessControl); | ||
|
||
MockAccessControlledUpgradeable accessControlled; | ||
|
||
function setUp() public { | ||
_setupAccessControl(); | ||
accessControlled = MockAccessControlledUpgradeable( | ||
_deployUUPSProxy( | ||
address(new MockAccessControlledUpgradeable()), | ||
abi.encodeWithSelector( | ||
bytes4(keccak256(bytes("initialize(address)"))), | ||
address(accessControl) | ||
) | ||
) | ||
); | ||
} | ||
|
||
function test_AccessControlled_onlyRole() public { | ||
bytes32 role = keccak256("TEST_ROLE"); | ||
_grantRole(vm, role, address(this)); | ||
accessControlled.exposeOnlyRole(role); | ||
} | ||
|
||
function test_AccessControlled_revert_onlyRole() public { | ||
bytes32 role = keccak256("TEST_ROLE"); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
Errors.MissingRole.selector, | ||
role, | ||
address(this) | ||
) | ||
); | ||
accessControlled.exposeOnlyRole(role); | ||
} | ||
|
||
function test_AccessControlled_setAccessControl() public { | ||
AccessControlSingleton ac2 = new AccessControlSingleton(); | ||
vm.expectEmit(true, true, true, true); | ||
emit AccessControlUpdated(address(ac2)); | ||
vm.prank(admin); | ||
accessControlled.setAccessControl(address(ac2)); | ||
} | ||
|
||
function test_AccessControlled_revert_setAccessControlNotProtocolAdmin() public { | ||
AccessControlSingleton ac2 = new AccessControlSingleton(); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
Errors.MissingRole.selector, | ||
AccessControl.PROTOCOL_ADMIN_ROLE, | ||
address(this) | ||
) | ||
); | ||
accessControlled.setAccessControl(address(ac2)); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: BUSDL-1.1 | ||
pragma solidity ^0.8.13; | ||
|
||
import { AccessControlled } from "contracts/access-control/AccessControlled.sol"; | ||
|
||
contract MockAccessControlled is AccessControlled { | ||
constructor(address accessControl) AccessControlled(accessControl) {} | ||
|
||
function exposeOnlyRole(bytes32 role) public onlyRole(role) {} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: BUSDL-1.1 | ||
pragma solidity ^0.8.13; | ||
|
||
import { AccessControlledUpgradeable } from "contracts/access-control/AccessControlledUpgradeable.sol"; | ||
|
||
contract MockAccessControlledUpgradeable is AccessControlledUpgradeable { | ||
|
||
function initialize(address accessControl) public initializer { | ||
__AccessControlledUpgradeable_init(accessControl); | ||
} | ||
|
||
function exposeOnlyRole(bytes32 role) public onlyRole(role) {} | ||
|
||
function _authorizeUpgrade(address newImplementation) internal virtual override {} | ||
} |
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