From 0c6aaf90552f6f70591a1ae35ca2963b99ed04fb Mon Sep 17 00:00:00 2001 From: JohnGuilding Date: Wed, 19 Jun 2024 13:29:06 +0100 Subject: [PATCH] allowValidatorRecovery tests --- test/unit/UnitBase.t.sol | 5 +- .../allowValidatorRecovery.t.sol | 102 +++++++++++++++++- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/test/unit/UnitBase.t.sol b/test/unit/UnitBase.t.sol index 05751b8..5f40a08 100644 --- a/test/unit/UnitBase.t.sol +++ b/test/unit/UnitBase.t.sol @@ -43,6 +43,8 @@ abstract contract UnitBase is RhinestoneModuleKit, Test { EmailRecoveryFactory emailRecoveryFactory; EmailRecoverySubjectHandler emailRecoveryHandler; EmailRecoveryManagerHarness emailRecoveryManager; + EmailRecoveryModule emailRecoveryModule; + // EmailRecoveryManager emailRecoveryManager; address emailRecoveryManagerAddress; address recoveryModuleAddress; @@ -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); diff --git a/test/unit/modules/EmailRecoveryModule/allowValidatorRecovery.t.sol b/test/unit/modules/EmailRecoveryModule/allowValidatorRecovery.t.sol index 7b40dd3..6ee6e2b 100644 --- a/test/unit/modules/EmailRecoveryModule/allowValidatorRecovery.t.sol +++ b/test/unit/modules/EmailRecoveryModule/allowValidatorRecovery.t.sol @@ -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); + } }