Skip to content

Commit

Permalink
test: add 'withdrawNative' native withdraw failed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielstoica committed Aug 12, 2024
1 parent 5dffa0b commit 92babaf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ abstract contract Base_Test is Test, Events {
vm.prank({ msgSender: _owner });
_container =
Container(payable(dockRegistry.createContainer({ dockId: _dockId, initialModules: _initialModules })));
vm.stopPrank();
}

function allowlistModule(address _module) internal {
Expand Down
47 changes: 41 additions & 6 deletions test/unit/concrete/container/withdraw-native/withdrawNative.t.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

import { MockBadReceiver } from "../../../../mocks/MockBadReceiver.sol";
import { Container_Unit_Concrete_Test } from "../Container.t.sol";
import { Container } from "./../../../../../src/Container.sol";
import { Errors } from "../../../../utils/Errors.sol";
import { Events } from "../../../../utils/Events.sol";

contract WithdrawNative_Unit_Concrete_Test is Container_Unit_Concrete_Test {
address badReceiver;
Container badContainer;

function setUp() public virtual override {
Container_Unit_Concrete_Test.setUp();

// Create a bad receiver contract as the owner of the `badContainer` to test for the `NativeWithdrawFailed` error
badReceiver = address(new MockBadReceiver());
vm.deal({ account: badReceiver, newBalance: 100 ether });

// Deploy the `badContainer` container
address[] memory modules = new address[](1);
modules[0] = address(mockModule);
badContainer = deployContainer({ _owner: address(badReceiver), _dockId: 0, _initialModules: modules });
}

function test_RevertWhen_CallerNotOwner() external {
Expand All @@ -21,28 +35,49 @@ contract WithdrawNative_Unit_Concrete_Test is Container_Unit_Concrete_Test {
container.withdrawNative({ amount: 2 ether });
}

modifier whenCallerOwner() {
// Make Eve the caller for the next test suite as she's the owner of the container
vm.startPrank({ msgSender: users.eve });
modifier whenCallerOwner(address caller) {
// Make `caller` the caller for the next test suite as she's the owner of the container
vm.startPrank({ msgSender: caller });
_;
}

function test_RevertWhen_InsufficientNativeToWithdraw() external whenCallerOwner {
function test_RevertWhen_InsufficientNativeToWithdraw() external whenCallerOwner(users.eve) {
// Expect the next call to revert with the {InsufficientNativeToWithdraw} error
vm.expectRevert(Errors.InsufficientNativeToWithdraw.selector);

// Run the test
container.withdrawNative({ amount: 2 ether });
}

modifier whenSufficientNativeToWithdraw() {
modifier whenSufficientNativeToWithdraw(Container container) {
// Deposit sufficient native tokens (ETH) into the container to enable the withdrawal
(bool success,) = payable(container).call{ value: 2 ether }("");
if (!success) revert();
_;
}

function test_WithdrawNative() external whenCallerOwner whenSufficientNativeToWithdraw {
function test_RevertWhen_NativeWithdrawFailed()
external
whenCallerOwner(badReceiver)
whenSufficientNativeToWithdraw(badContainer)
{
// Expect the next call to revert with the {NativeWithdrawFailed} error
vm.expectRevert(Errors.NativeWithdrawFailed.selector);

// Run the test
badContainer.withdrawNative({ amount: 1 ether });
}

modifier whenNativeWithdrawSucceeds() {
_;
}

function test_WithdrawNative()
external
whenCallerOwner(users.eve)
whenSufficientNativeToWithdraw(container)
whenNativeWithdrawSucceeds
{
// Store the ETH balance of Eve and {Container} contract before withdrawal
uint256 balanceOfContainerBefore = address(container).balance;
uint256 balanceOfEveBefore = address(users.eve).balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ withdrawNative.t.sol
├── when container native token (ETH) balance IS INSUFFICIENT to support the withdrawal
│ └── it should revert with the {InsufficientERC20ToWithdraw} error
└── when container native token (ETH) balance IS SUFFICIENT to support the withdrawal
├── it should transfer the native tokens to the caller
└── it should emit an {AssetWithdrawn} event
├── when native token transfer fails
│ └── it should revert with the {NativeWithdrawFailed} error
└── when native token transfer succeeds
├── it should transfer the native tokens to the caller
└── it should emit an {AssetWithdrawn} event

0 comments on commit 92babaf

Please sign in to comment.