Skip to content

Commit

Permalink
allowValidatorRecovery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Jun 19, 2024
1 parent 56f5f2f commit 0c6aaf9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
5 changes: 3 additions & 2 deletions test/unit/UnitBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ abstract contract UnitBase is RhinestoneModuleKit, Test {
EmailRecoveryFactory emailRecoveryFactory;
EmailRecoverySubjectHandler emailRecoveryHandler;
EmailRecoveryManagerHarness emailRecoveryManager;
EmailRecoveryModule emailRecoveryModule;

// EmailRecoveryManager emailRecoveryManager;
address emailRecoveryManagerAddress;
address recoveryModuleAddress;
Expand Down Expand Up @@ -115,8 +117,7 @@ abstract contract UnitBase is RhinestoneModuleKit, Test {
);
emailRecoveryManagerAddress = address(emailRecoveryManager);

EmailRecoveryModule emailRecoveryModule =
new EmailRecoveryModule(emailRecoveryManagerAddress);
emailRecoveryModule = new EmailRecoveryModule(emailRecoveryManagerAddress);
recoveryModuleAddress = address(emailRecoveryModule);
emailRecoveryManager.initialize(recoveryModuleAddress);

Expand Down
102 changes: 99 additions & 3 deletions test/unit/modules/EmailRecoveryModule/allowValidatorRecovery.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,110 @@
pragma solidity ^0.8.25;

import "forge-std/console2.sol";
import { ModuleKitHelpers } from "modulekit/ModuleKit.sol";
import { MODULE_TYPE_EXECUTOR, MODULE_TYPE_VALIDATOR } from "modulekit/external/ERC7579.sol";
import { IModule } from "erc7579/interfaces/IERC7579Module.sol";
import { SentinelListLib } from "sentinellist/SentinelList.sol";
import { OwnableValidator } from "src/test/OwnableValidator.sol";
import { EmailRecoveryModule } from "src/modules/EmailRecoveryModule.sol";
import { UnitBase } from "../../UnitBase.t.sol";

contract EmailRecoveryModule_allowValidatorRecovery_Test is UnitBase {
using ModuleKitHelpers for *;

function setUp() public override {
super.setUp();
}

function test_AllowValidatorRecovery_RevertWhen_InvalidValidator() public view { }
function test_AllowValidatorRecovery_SucceedsWhenAlreadyInitialized() public view { }
function test_AllowValidatorRecovery_SucceedsWhenInitializing() public view { }
function test_AllowValidatorRecovery_RevertWhen_UnsafeOnInstallSelector() public {
vm.expectRevert(
abi.encodeWithSelector(
EmailRecoveryModule.InvalidSelector.selector, IModule.onInstall.selector
)
);
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
address(validator), bytes("0"), IModule.onInstall.selector
);
}

function test_AllowValidatorRecovery_RevertWhen_UnsafeOnUninstallSelector() public {
vm.expectRevert(
abi.encodeWithSelector(
EmailRecoveryModule.InvalidSelector.selector, IModule.onUninstall.selector
)
);
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
address(validator), bytes("0"), IModule.onUninstall.selector
);
}

function test_AllowValidatorRecovery_RevertWhen_InvalidValidator() public {
OwnableValidator newValidator = new OwnableValidator();
address newValidatorAddress = address(newValidator);

vm.expectRevert(
abi.encodeWithSelector(
EmailRecoveryModule.InvalidValidator.selector, newValidatorAddress
)
);
vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(
newValidatorAddress, bytes("0"), functionSelector
);
}

function test_AllowValidatorRecovery_RevertWhen_ValidatorAlreadyInList() public {
vm.startPrank(accountAddress);
vm.expectRevert(
abi.encodeWithSelector(
SentinelListLib.LinkedList_EntryAlreadyInList.selector, address(validator)
)
);
emailRecoveryModule.allowValidatorRecovery(address(validator), "", functionSelector);
}

function test_AllowValidatorRecovery_SucceedsWhenAlreadyInitialized() public {
// Deplopy and install new validator
OwnableValidator newValidator = new OwnableValidator();
address newValidatorAddress = address(newValidator);
instance.installModule({
moduleTypeId: MODULE_TYPE_VALIDATOR,
module: newValidatorAddress,
data: abi.encode(owner, recoveryModuleAddress)
});

vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(newValidatorAddress, "", functionSelector);

address[] memory allowedValidators =
emailRecoveryModule.getAllowedValidators(accountAddress);
bytes4[] memory allowedSelectors = emailRecoveryModule.getAllowedSelectors(accountAddress);

assertEq(allowedValidators.length, 2);
// TODO: think about if this ordering should be expected behaviour
assertEq(allowedValidators[0], newValidatorAddress);
assertEq(allowedValidators[1], address(validator));
assertEq(allowedSelectors.length, 2);
assertEq(allowedSelectors[0], functionSelector);
assertEq(allowedSelectors[1], functionSelector);
}

function test_AllowValidatorRecovery_SucceedsWhenInitializing() public {
// Uninstall module so state is reset
instance.uninstallModule(MODULE_TYPE_EXECUTOR, recoveryModuleAddress, "");

vm.startPrank(accountAddress);
emailRecoveryModule.allowValidatorRecovery(address(validator), "", functionSelector);

address[] memory allowedValidators =
emailRecoveryModule.getAllowedValidators(accountAddress);
bytes4[] memory allowedSelectors = emailRecoveryModule.getAllowedSelectors(accountAddress);

assertEq(allowedValidators.length, 1);
assertEq(allowedValidators[0], address(validator));
assertEq(allowedSelectors.length, 1);
assertEq(allowedSelectors[0], functionSelector);
}
}

0 comments on commit 0c6aaf9

Please sign in to comment.