Skip to content

Commit

Permalink
test: add WhitelistAccessModule unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xorsal committed Nov 27, 2024
1 parent 69a8be9 commit 728e0e8
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions solidity/test/unit/modules/access/WhitelistAccessModule.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import 'forge-std/Test.sol';

import {Helpers} from '../../../utils/Helpers.sol';

import {IModule} from '@defi-wonderland/prophet-core/solidity/interfaces/IModule.sol';

Check warning on line 8 in solidity/test/unit/modules/access/WhitelistAccessModule.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

imported name IModule is not used
import {IOracle} from '@defi-wonderland/prophet-core/solidity/interfaces/IOracle.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';

Check warning on line 10 in solidity/test/unit/modules/access/WhitelistAccessModule.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

imported name IERC20 is not used

import {
IAccessModule,
IWhitelistAccessModule,

Check warning on line 14 in solidity/test/unit/modules/access/WhitelistAccessModule.t.sol

View workflow job for this annotation

GitHub Actions / Run Linters (16.x)

imported name IWhitelistAccessModule is not used
WhitelistAccessModule
} from '../../../../contracts/examples/modules/access/WhitelistAccessModule.sol';

contract ForTest_WhitelistAccessModule is WhitelistAccessModule {
constructor(IOracle _oracle) WhitelistAccessModule(_oracle) {}

function forTest_setSenderApproval(address _user, address _allowedSender, bool _status) public {
whitelist[_user][_allowedSender] = _status;
}
}

/**
* @title Whitelist Access Module unit tests
*/
contract BaseTest is Test, Helpers {
// The target contract
ForTest_WhitelistAccessModule public whitelistAccessModule;
// A mock oracle
IOracle public oracle;

event SetApproval(address _user, address _sender, bool _status);

function setUp() public {
oracle = IOracle(makeAddr('Oracle'));
vm.etch(address(oracle), hex'069420');
//
whitelistAccessModule = new ForTest_WhitelistAccessModule(oracle);
}
}

contract WhitelistAccessModule_Unit_ModuleData is BaseTest {
function test_decodeAccessControlParameters_decodesSuccessfully(IAccessModule.AccessControlParameters memory _params)
public
view
{
// Test: decode the given access control parameters
IAccessModule.AccessControlParameters memory _decodedParams =
whitelistAccessModule.decodeAccessControlParameters(abi.encode(_params));

// Check: decoded values match original values?
assertEq(_decodedParams.sender, _params.sender);
assertEq(_decodedParams.typehash, _params.typehash);
assertEq(_decodedParams.typehashParams, _params.typehashParams);
assertEq(_decodedParams.accessControl.user, _params.accessControl.user);
assertEq(_decodedParams.accessControl.data, _params.accessControl.data);
}

function test_moduleName_ReturnsCorrectName() public view {
assertEq(whitelistAccessModule.moduleName(), 'WhitelistAccessModule');
}
}

contract WhitelistAccessModule_Unit_HasAccess is BaseTest {
function test_hasAccess_authorizedSender(IAccessModule.AccessControlParameters memory _params) public {
// set _params.sender` as an allowed sender for `_params.accessControl.user`
whitelistAccessModule.forTest_setSenderApproval(_params.accessControl.user, _params.sender, true);

assertTrue(whitelistAccessModule.hasAccess(abi.encode(_params)));
}

function test_hasAccess_unauthorizedSender(IAccessModule.AccessControlParameters memory _params) public {
// set _params.sender` as an unallowed sender for `_params.accessControl.user`
whitelistAccessModule.forTest_setSenderApproval(_params.accessControl.user, _params.sender, false);

assertFalse(whitelistAccessModule.hasAccess(abi.encode(_params)));
}

/// @notice By default, authorization is denied
function test_hasAccess_unauthorizedByDefault(IAccessModule.AccessControlParameters memory _params) public {
assertFalse(whitelistAccessModule.hasAccess(abi.encode(_params)));
}
}

contract WhitelistAccessModule_Unit_setSenderApproval is BaseTest {
function test_setSenderApproval_updatesState(address _user, address _sender, bool _status) public {
vm.prank(_user);
whitelistAccessModule.setSenderApproval(_sender, _status);

assertEq(whitelistAccessModule.whitelist(_user, _sender), _status);
}

function test_setSenderApproval_emitsEvent(address _user, address _sender, bool _status) public {
// Expect the event with correct parameters
vm.expectEmit(address(whitelistAccessModule));
emit SetApproval(_user, _sender, _status);

// Test: call setSenderApproval
vm.prank(_user);
whitelistAccessModule.setSenderApproval(_sender, _status);
}

function test_setSenderApproval_CanUpdateStatus(address _user, address _sender, bool _status) public {
// Expect the event with correct parameters
vm.expectEmit(address(whitelistAccessModule));
emit SetApproval(_user, _sender, _status);

// sets `_status` for `_sender`
vm.prank(_user);
whitelistAccessModule.setSenderApproval(_sender, _status);

vm.expectEmit(address(whitelistAccessModule));
emit SetApproval(_user, _sender, !_status);

// sets `!_status` for `_sender`
vm.prank(_user);
whitelistAccessModule.setSenderApproval(_sender, !_status);
}
}

0 comments on commit 728e0e8

Please sign in to comment.