From d87ce845cf026f58f6aaa00377518101b04176f9 Mon Sep 17 00:00:00 2001 From: gabrielstoica Date: Wed, 10 Jul 2024 12:40:22 +0300 Subject: [PATCH] test: fix base 'setUp' method to account Sablier v2 streams and small improvements --- test/Base.t.sol | 28 +++++++++++++++---- test/unit/concrete/container/Container.t.sol | 6 ++-- .../enable-module/enableModule.t.sol | 4 +-- .../concrete/container/execute/execute.t.sol | 23 ++++++--------- test/utils/Helpers.sol | 10 +++---- test/utils/Types.sol | 2 ++ 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/test/Base.t.sol b/test/Base.t.sol index e4ceafa..2481709 100644 --- a/test/Base.t.sol +++ b/test/Base.t.sol @@ -8,6 +8,8 @@ import { MockERC20NoReturn } from "./mocks/MockERC20NoReturn.sol"; import { MockModule } from "./mocks/MockModule.sol"; import { Container } from "./../src/Container.sol"; import { InvoiceModule } from "./../src/modules/invoice-module/InvoiceModule.sol"; +import { SablierV2LockupLinear } from "@sablier/v2-core/src/SablierV2LockupLinear.sol"; +import { NFTDescriptorMock } from "@sablier/v2-core/test/mocks/NFTDescriptorMock.sol"; abstract contract Base_Test is Test, Events { /*////////////////////////////////////////////////////////////////////////// @@ -25,22 +27,36 @@ abstract contract Base_Test is Test, Events { MockERC20NoReturn internal usdt; MockModule internal mockModule; + // Sablier V2 related test contracts + NFTDescriptorMock internal mockNFTDescriptor; + SablierV2LockupLinear internal sablierV2LockupLinear; + /*////////////////////////////////////////////////////////////////////////// SET-UP FUNCTION //////////////////////////////////////////////////////////////////////////*/ function setUp() public virtual { - // Deploy test contracts + // Deploy the mock USDT contract to deal it to the users usdt = new MockERC20NoReturn("Tether USD", "USDT", 6); - invoiceModule = new InvoiceModule(); + + // Create test users + users = Users({ admin: createUser("admin"), eve: createUser("eve"), bob: createUser("bob") }); + + // Deploy test contracts + mockNFTDescriptor = new NFTDescriptorMock(); + sablierV2LockupLinear = new SablierV2LockupLinear({ + initialAdmin: users.admin, + initialNFTDescriptor: mockNFTDescriptor + }); + invoiceModule = new InvoiceModule({ + _brokerAdmin: users.admin, + _sablierLockupDeployment: sablierV2LockupLinear + }); mockModule = new MockModule(); // Label the test contracts so we can easily track them vm.label({ account: address(usdt), newLabel: "USDT" }); vm.label({ account: address(invoiceModule), newLabel: "InvoiceModule" }); - - // Create test users - users = Users({ eve: createUser("eve"), bob: createUser("bob") }); } /*////////////////////////////////////////////////////////////////////////// @@ -60,7 +76,7 @@ abstract contract Base_Test is Test, Events { function createUser(string memory name) internal returns (address payable) { address payable user = payable(makeAddr(name)); vm.deal({ account: user, newBalance: 100 ether }); - deal({ token: address(usdt), to: user, give: 1000000e16 }); + deal({ token: address(usdt), to: user, give: 1000000e6 }); return user; } diff --git a/test/unit/concrete/container/Container.t.sol b/test/unit/concrete/container/Container.t.sol index 73a5453..25a4b09 100644 --- a/test/unit/concrete/container/Container.t.sol +++ b/test/unit/concrete/container/Container.t.sol @@ -2,14 +2,14 @@ pragma solidity ^0.8.26; import { Base_Test } from "../../../Base.t.sol"; -import { InvoiceModule } from "./../../../../src/modules/invoice-module/InvoiceModule.sol"; contract Container_Unit_Concrete_Test is Base_Test { function setUp() public virtual override { Base_Test.setUp(); - address[] memory modules = new address[](1); - modules[0] = address(invoiceModule); + address[] memory modules = new address[](2); + modules[0] = address(mockModule); + modules[1] = address(invoiceModule); container = deployContainer({ owner: users.eve, initialModules: modules }); } diff --git a/test/unit/concrete/container/enable-module/enableModule.t.sol b/test/unit/concrete/container/enable-module/enableModule.t.sol index 1121980..716d776 100644 --- a/test/unit/concrete/container/enable-module/enableModule.t.sol +++ b/test/unit/concrete/container/enable-module/enableModule.t.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.26; import { Container_Unit_Concrete_Test } from "../Container.t.sol"; -import { InvoiceModule } from "./../../../../../src/modules/invoice-module/InvoiceModule.sol"; +import { MockModule } from "../../../../mocks/MockModule.sol"; import { Events } from "../../../../utils/Events.sol"; import { Errors } from "../../../../utils/Errors.sol"; @@ -42,7 +42,7 @@ contract EnableModule_Unit_Concrete_Test is Container_Unit_Concrete_Test { function test_EnableModule() external whenCallerOwner whenNonZeroCodeModule { // Create a new mock module - InvoiceModule mockModule = new InvoiceModule(); + MockModule mockModule = new MockModule(); // Expect the {ModuleEnabled} to be emitted vm.expectEmit(); diff --git a/test/unit/concrete/container/execute/execute.t.sol b/test/unit/concrete/container/execute/execute.t.sol index 7aa73d3..86c262f 100644 --- a/test/unit/concrete/container/execute/execute.t.sol +++ b/test/unit/concrete/container/execute/execute.t.sol @@ -42,31 +42,24 @@ contract Execute_Unit_Concrete_Test is Container_Unit_Concrete_Test { } function test_Execute() external whenCallerOwner whenModuleEnabled { - // Create the mock invoice and calldata for the module execution - InvoiceModuleTypes.Invoice memory invoice = Helpers.createInvoiceDataType({ recipient: address(container) }); - bytes memory data = abi.encodeWithSignature( - "createInvoice((address,uint8,uint8,uint40,uint40,(uint8,uint8,uint24,address,uint256)))", - invoice - ); + // Create the calldata for the mock module execution + bytes memory data = abi.encodeWithSignature("createModuleItem()", ""); // Expect the {ModuleExecutionSucceded} event to be emitted vm.expectEmit(); - emit Events.ModuleExecutionSucceded({ module: address(invoiceModule), value: 0, data: data }); + emit Events.ModuleExecutionSucceded({ module: address(mockModule), value: 0, data: data }); // Run the test - container.execute({ module: address(invoiceModule), value: 0, data: data }); + container.execute({ module: address(mockModule), value: 0, data: data }); - // Alter the `createInvoice` method signature by removing the `payment.amount` field - bytes memory wrongData = abi.encodeWithSignature( - "createInvoice((address,uint8,uint8,uint40,uint40,(uint8,uint8,uint24,address)))", - invoice - ); + // Alter the `createModuleItem` method signature by adding an invalid `uint256` field + bytes memory wrongData = abi.encodeWithSignature("createModuleItem(uint256)", 1); // Expect the {ModuleExecutionFailed} event to be emitted vm.expectEmit(); - emit Events.ModuleExecutionFailed({ module: address(invoiceModule), value: 0, data: wrongData }); + emit Events.ModuleExecutionFailed({ module: address(mockModule), value: 0, data: wrongData }); // Run the test - container.execute({ module: address(invoiceModule), value: 0, data: wrongData }); + container.execute({ module: address(mockModule), value: 0, data: wrongData }); } } diff --git a/test/utils/Helpers.sol b/test/utils/Helpers.sol index 67016d5..a4cedd5 100644 --- a/test/utils/Helpers.sol +++ b/test/utils/Helpers.sol @@ -8,16 +8,16 @@ library Helpers { return InvoiceModulesTypes.Invoice({ recipient: recipient, - status: InvoiceModulesTypes.Status.Active, + status: InvoiceModulesTypes.Status.Pending, frequency: InvoiceModulesTypes.Frequency.Regular, startTime: 0, - endTime: uint40(block.timestamp) + 150, + endTime: uint40(block.timestamp) + 1 weeks, payment: InvoiceModulesTypes.Payment({ - recurrence: InvoiceModulesTypes.Recurrence.OneTime, method: InvoiceModulesTypes.Method.Transfer, - amount: 1 ether, + recurrence: InvoiceModulesTypes.Recurrence.OneTime, + paymentsLeft: 1, asset: address(0), - paymentsLeft: 1 + amount: uint128(1 ether) }) }); } diff --git a/test/utils/Types.sol b/test/utils/Types.sol index 7069b09..fcff751 100644 --- a/test/utils/Types.sol +++ b/test/utils/Types.sol @@ -2,6 +2,8 @@ pragma solidity ^0.8.26; struct Users { + // Account with administrative permissions + address admin; // Account to interact with the protocol address eve; // Account to interact with the protocol