Skip to content

Commit 50d7587

Browse files
committed
test(evm): test IAuth and Boostcore passthrough auth
1 parent acf2ef1 commit 50d7587

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

packages/evm/contracts/auth/IAuth.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ interface IAuth {
88
/// @param addr The address to check for authorization
99
/// @return bool Returns true if the address is authorized, false otherwise
1010
function isAuthorized(address addr) external view returns (bool);
11-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// SPDX-License-Identifier: GPL-3.0
22
pragma solidity ^0.8.24;
3+
34
import {IAuth} from "contracts/auth/IAuth.sol";
45
/// @title Passthrough Authorization Contract
56
/// @dev Implements the IAuth interface, always authorizing access.
7+
68
contract PassthroughAuth is IAuth {
79
/// @notice Checks if an address is authorized
810
/// @dev In this implementation, all addresses are authorized.
@@ -11,4 +13,4 @@ contract PassthroughAuth is IAuth {
1113
function isAuthorized(address user) public view override returns (bool) {
1214
return true;
1315
}
14-
}
16+
}

packages/evm/contracts/shared/Mocks.sol

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {LibString} from "@solady/utils/LibString.sol";
55
import {ERC20} from "@solady/tokens/ERC20.sol";
66
import {ERC721} from "@solady/tokens/ERC721.sol";
77
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
8+
import {IAuth} from "contracts/auth/IAuth.sol";
89

910
/**
1011
* 🚨 WARNING: The mocks in this file are for testing purposes only. DO NOT use
@@ -72,3 +73,27 @@ contract MockERC1155 is ERC1155 {
7273
_burn(from, id, amount);
7374
}
7475
}
76+
77+
/// @title Mock Authorization Contract
78+
/// @dev Mock implementation of the IAuth interface for testing purposes.
79+
/// Allows setting authorized addresses via the constructor.
80+
contract MockAuth is IAuth {
81+
mapping(address => bool) private _isAuthorized;
82+
83+
/// @notice Initializes the contract with a list of authorized addresses.
84+
/// @param authorizedAddresses An array of addresses to be marked as authorized.
85+
/// @dev Addresses not included in the list will default to unauthorized.
86+
constructor(address[] memory authorizedAddresses) {
87+
for (uint256 i = 0; i < authorizedAddresses.length; i++) {
88+
_isAuthorized[authorizedAddresses[i]] = true;
89+
}
90+
}
91+
92+
/// @notice Checks if an address is authorized.
93+
/// @param addr The address to check for authorization.
94+
/// @return bool Returns true if the address is authorized, false otherwise.
95+
/// @dev This function overrides the isAuthorized function in the IAuth interface.
96+
function isAuthorized(address addr) external view override returns (bool) {
97+
return _isAuthorized[addr];
98+
}
99+
}

packages/evm/test/BoostCore.t.sol

+41-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma solidity ^0.8.24;
33

44
import {Test, console} from "lib/forge-std/src/Test.sol";
5-
import {MockERC20, MockERC721} from "contracts/shared/Mocks.sol";
5+
import {MockERC20, MockERC721, MockAuth} from "contracts/shared/Mocks.sol";
66

77
import {LibClone} from "@solady/utils/LibClone.sol";
88
import {LibZip} from "@solady/utils/LibZip.sol";
@@ -41,6 +41,8 @@ contract BoostCoreTest is Test {
4141

4242
MockERC20 mockERC20 = new MockERC20();
4343
MockERC721 mockERC721 = new MockERC721();
44+
MockAuth mockAuth;
45+
address[] mockAddresses;
4446

4547
BoostCore boostCore = new BoostCore(new BoostRegistry(), address(1));
4648
BoostLib.Target action = _makeAction(address(mockERC721), MockERC721.mint.selector, mockERC721.mintPrice());
@@ -78,6 +80,8 @@ contract BoostCoreTest is Test {
7880
})
7981
)
8082
);
83+
mockAddresses.push(address(this));
84+
mockAuth = new MockAuth(mockAddresses);
8185
}
8286

8387
/////////////////////////////
@@ -242,6 +246,42 @@ contract BoostCoreTest is Test {
242246
boostCore.createBoost(invalidActionCalldata);
243247
}
244248

249+
//////////////////////////////////
250+
// BoostCore.setCreateBoostAuth //
251+
/////////////////////////////////
252+
253+
function testSetAuthToMockAuth() public {
254+
// Assuming BoostCore has a function to set the auth strategy
255+
boostCore.setCreateBoostAuth(address(mockAuth));
256+
assertTrue(address(boostCore.createBoostAuth()) == address(mockAuth), "Auth strategy not set correctly");
257+
}
258+
259+
function testAuthorizedUserCanCreateBoost() public {
260+
// Set the auth strategy to MockAuth
261+
boostCore.setCreateBoostAuth(address(mockAuth));
262+
263+
// Use an authorized address (this contract)
264+
boostCore.createBoost(validCreateCalldata);
265+
266+
// Verify the boost was created
267+
assertEq(1, boostCore.getBoostCount(), "Authorized user should be able to create boost");
268+
}
269+
270+
function testUnauthorizedUserCannotCreateBoost() public {
271+
// Set the auth strategy to MockAuth
272+
boostCore.setCreateBoostAuth(address(mockAuth));
273+
274+
// Use an unauthorized address
275+
vm.prank(makeAddr("unauthorizedBoostCreator"));
276+
277+
// Expect a revert due to unauthorized access
278+
vm.expectRevert(BoostError.Unauthorized.selector);
279+
boostCore.createBoost(validCreateCalldata);
280+
281+
// Verify no boost was created
282+
assertEq(0, boostCore.getBoostCount(), "Unauthorized user should not be able to create boost");
283+
}
284+
245285
///////////////////////////
246286
// Test Helper Functions //
247287
///////////////////////////

packages/evm/test/shared/Mocks.t.sol

+17
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ pragma solidity ^0.8.24;
33

44
import {Test, console} from "lib/forge-std/src/Test.sol";
55
import {MockERC20, MockERC721} from "contracts/shared/Mocks.sol";
6+
import {MockAuth} from "contracts/shared/Mocks.sol"; // Add this import at the top with the others
67

78
contract MocksTest is Test {
89
MockERC20 mockERC20;
910
MockERC721 mockERC721;
11+
MockAuth mockAuth;
12+
address[] mockAddresses;
13+
address authorizedBoostCreator = makeAddr("authorizedBoostCreator");
1014

1115
function setUp() public {
1216
mockERC20 = new MockERC20();
1317
mockERC721 = new MockERC721();
18+
mockAddresses.push(authorizedBoostCreator);
19+
mockAuth = new MockAuth(mockAddresses);
1420
}
1521

1622
///////////////
@@ -88,4 +94,15 @@ contract MocksTest is Test {
8894
"https://example.com/token/115792089237316195423570985008687907853269984665640564039457584007913129639935"
8995
);
9096
}
97+
98+
//////////////
99+
// MockAuth //
100+
//////////////
101+
function testMockAuthIsAuthorized() public {
102+
assertTrue(mockAuth.isAuthorized(authorizedBoostCreator));
103+
}
104+
105+
function testMockAuthIsNotAuthorized() public {
106+
assertFalse(mockAuth.isAuthorized(address(this)));
107+
}
91108
}

0 commit comments

Comments
 (0)