Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikesymmetry committed Oct 16, 2024
1 parent b3e9117 commit 1ef6d0d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {SpendPermissionManager} from "../../../src/SpendPermissionManager.sol";
import {SpendPermissionManagerBase} from "../../base/SpendPermissionManagerBase.sol";
import {CoinbaseSmartWallet} from "smart-wallet/CoinbaseSmartWallet.sol";

contract PermitTest is SpendPermissionManagerBase {
contract ApproveWithSignatureTest is SpendPermissionManagerBase {
function setUp() public {
_initializeSpendPermissionManager();
vm.prank(owner);
account.addOwnerAddress(address(mockSpendPermissionManager));
}

function test_permit_revert_unauthorizedSpendPermission(
function test_approveWithSignature_revert_invalidSignature(
uint128 invalidPk,
address spender,
address token,
Expand All @@ -35,11 +35,11 @@ contract PermitTest is SpendPermissionManagerBase {
});

bytes memory invalidSignature = _signSpendPermission(spendPermission, invalidPk, 0);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.UnauthorizedSpendPermission.selector));
mockSpendPermissionManager.permit(spendPermission, invalidSignature);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.InvalidSignature.selector));
mockSpendPermissionManager.approveWithSignature(spendPermission, invalidSignature);
}

function test_permit_revert_invalidStartEnd(
function test_approveWithSignature_revert_invalidStartEnd(
address spender,
uint48 start,
uint48 end,
Expand All @@ -60,10 +60,12 @@ contract PermitTest is SpendPermissionManagerBase {

bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.InvalidStartEnd.selector, start, end));
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
}

function test_permit_revert_zeroPeriod(address spender, uint48 start, uint48 end, uint160 allowance) public {
function test_approveWithSignature_revert_zeroPeriod(address spender, uint48 start, uint48 end, uint160 allowance)
public
{
vm.assume(start < end);

SpendPermissionManager.SpendPermission memory spendPermission = SpendPermissionManager.SpendPermission({
Expand All @@ -78,10 +80,12 @@ contract PermitTest is SpendPermissionManagerBase {

bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.ZeroPeriod.selector));
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
}

function test_permit_revert_zeroAllowance(address spender, uint48 start, uint48 end, uint48 period) public {
function test_approveWithSignature_revert_zeroAllowance(address spender, uint48 start, uint48 end, uint48 period)
public
{
vm.assume(start < end);
vm.assume(period > 0);

Expand All @@ -97,10 +101,10 @@ contract PermitTest is SpendPermissionManagerBase {

bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.ZeroAllowance.selector));
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
}

function test_permit_success_isApproved(
function test_approveWithSignature_success_isApproved(
address spender,
address token,
uint48 start,
Expand All @@ -123,11 +127,11 @@ contract PermitTest is SpendPermissionManagerBase {
});

bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
vm.assertTrue(mockSpendPermissionManager.isApproved(spendPermission));
}

function test_permit_success_emitsEvent(
function test_approveWithSignature_success_emitsEvent(
address spender,
address token,
uint48 start,
Expand Down Expand Up @@ -156,10 +160,10 @@ contract PermitTest is SpendPermissionManagerBase {
account: address(account),
spendPermission: spendPermission
});
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
}

function test_permit_success_erc6492SignaturePreDeploy(
function test_approveWithSignature_success_erc6492SignaturePreDeploy(
uint128 ownerPk,
address spender,
address token,
Expand Down Expand Up @@ -194,14 +198,14 @@ contract PermitTest is SpendPermissionManagerBase {
vm.assertEq(counterfactualAccount.code.length, 0);

// submit the spend permission with the signature, see permit succeed
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);

// verify that the account is now deployed (has code) and that a call to isValidSignature returns true
vm.assertGt(counterfactualAccount.code.length, 0);
vm.assertTrue(mockSpendPermissionManager.isApproved(spendPermission));
}

function test_permit_success_erc6492SignatureAlreadyDeployed(
function test_approveWithSignature_success_erc6492SignatureAlreadyDeployed(
uint128 ownerPk,
address spender,
address token,
Expand Down Expand Up @@ -237,7 +241,7 @@ contract PermitTest is SpendPermissionManagerBase {
vm.assertGt(counterfactualAccount.code.length, 0);

// submit the spend permission with the signature, see permit succeed
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
vm.assertTrue(mockSpendPermissionManager.isApproved(spendPermission));
}
}
2 changes: 1 addition & 1 deletion test/src/SpendPermissions/spend.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ contract SpendTest is SpendPermissionManagerBase {
vm.warp(start);

vm.startPrank(spender);
mockSpendPermissionManager.permit(spendPermission, signature);
mockSpendPermissionManager.approveWithSignature(spendPermission, signature);
mockSpendPermissionManager.spend(spendPermission, recipient, spend);

assertEq(address(account).balance, allowance - spend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {SpendPermissionManager} from "../../../src/SpendPermissionManager.sol";

import {SpendPermissionManagerBase} from "../../base/SpendPermissionManagerBase.sol";

contract PermitAndSpendTest is SpendPermissionManagerBase {
contract SpendWithSignatureTest is SpendPermissionManagerBase {
MockERC20 mockERC20 = new MockERC20("mockERC20", "TEST", 18);

function setUp() public {
Expand All @@ -16,7 +16,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
account.addOwnerAddress(address(mockSpendPermissionManager));
}

function test_permitAndSpend_revert_invalidSender(
function test_spendWithSignature_revert_invalidSender(
address sender,
address spender,
address recipient,
Expand Down Expand Up @@ -45,11 +45,11 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
vm.startPrank(sender);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.InvalidSender.selector, sender, spender));
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);
mockSpendPermissionManager.spendWithSignature(spendPermission, signature, recipient, spend);
vm.stopPrank();
}

function test_permitAndSpend_revert_unauthorizedSpendPermission(
function test_spendWithSignature_revert_invalidSignature(
uint128 invalidPk,
address sender,
address recipient,
Expand Down Expand Up @@ -79,12 +79,12 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
bytes memory invalidSignature = _signSpendPermission(spendPermission, invalidPk, 0);
vm.warp(start);
vm.startPrank(sender);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.UnauthorizedSpendPermission.selector));
mockSpendPermissionManager.permitAndSpend(spendPermission, invalidSignature, recipient, spend);
vm.expectRevert(abi.encodeWithSelector(SpendPermissionManager.InvalidSignature.selector));
mockSpendPermissionManager.spendWithSignature(spendPermission, invalidSignature, recipient, spend);
vm.stopPrank();
}

function test_permitAndSpend_success_ether(
function test_spendWithSignature_success_ether(
address sender,
address recipient,
uint48 start,
Expand Down Expand Up @@ -121,7 +121,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
vm.warp(start);

vm.startPrank(sender);
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);
mockSpendPermissionManager.spendWithSignature(spendPermission, signature, recipient, spend);

assertEq(address(account).balance, allowance - spend);
assertEq(recipient.balance, spend);
Expand All @@ -131,7 +131,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(usage.spend, spend);
}

function test_permitAndSpend_success_ether_alreadyInitialized(
function test_spendWithSignature_success_ether_alreadyInitialized(
address sender,
address recipient,
uint48 start,
Expand Down Expand Up @@ -169,7 +169,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(recipient.balance, 0);
bytes memory signature = _signSpendPermission(spendPermission, ownerPk, 0);
vm.prank(sender);
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);
mockSpendPermissionManager.spendWithSignature(spendPermission, signature, recipient, spend);
assertEq(address(account).balance, allowance - spend);
assertEq(recipient.balance, spend);
SpendPermissionManager.PeriodSpend memory usage = mockSpendPermissionManager.getCurrentPeriod(spendPermission);
Expand All @@ -178,7 +178,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(usage.spend, spend);
}

function test_permitAndSpend_success_ERC20(
function test_spendWithSignature_success_ERC20(
address sender,
address recipient,
uint48 start,
Expand Down Expand Up @@ -213,7 +213,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(mockERC20.balanceOf(recipient), 0);

vm.prank(sender);
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);
mockSpendPermissionManager.spendWithSignature(spendPermission, signature, recipient, spend);

assertEq(mockERC20.balanceOf(address(account)), allowance - spend);
assertEq(mockERC20.balanceOf(recipient), spend);
Expand All @@ -223,7 +223,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(usage.spend, spend);
}

function test_permitAndSpend_success_ether_erc6492PreDeploy(
function test_spendWithSignature_success_ether_erc6492PreDeploy(
uint128 ownerPk,
address spender,
address recipient,
Expand Down Expand Up @@ -271,7 +271,7 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
vm.warp(start);

vm.startPrank(spender);
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);
mockSpendPermissionManager.spendWithSignature(spendPermission, signature, recipient, spend);

assertEq(counterfactualAccount.balance, allowance - spend);
assertEq(recipient.balance, spend);
Expand Down

0 comments on commit 1ef6d0d

Please sign in to comment.