Skip to content

Commit

Permalink
test: add unit 'transferOwnership' tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielstoica committed Jul 25, 2024
1 parent 5bb6c02 commit f11b6f5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enableModule.t.sol
disableModule.t.sol
├── when the caller IS NOT the container owner
│ └── it should revert with the {Unauthorized} error
└── when the caller IS the container owner
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

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

contract TransferOwnership_Unit_Concrete_Test is Container_Unit_Concrete_Test {
function setUp() public virtual override {
Container_Unit_Concrete_Test.setUp();
}

function test_RevertWhen_CallerNotOwner() external {
// Make Bob the caller for this test suite who is not the owner of the container
vm.startPrank({ msgSender: users.bob });

// Expect the next call to revert with the {Unauthorized} error
vm.expectRevert(Errors.Unauthorized.selector);

// Run the test
container.transferOwnership({ newOwner: users.eve });
}

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

function test_RevertWhen_InvalidOwnerZeroAddress() external whenCallerOwner {
// Expect the next call to revert with the {InvalidOwnerZeroAddress}
vm.expectRevert(Errors.InvalidOwnerZeroAddress.selector);

// Run the test
container.transferOwnership({ newOwner: address(0) });
}

modifier whenNonZeroOwnerAddress() {
_;
}

function test_TransferOwnership() external whenCallerOwner whenNonZeroOwnerAddress {
// Expect the {OwnershipTransferred} to be emitted
vm.expectEmit();
emit Events.OwnershipTransferred({ oldOwner: users.eve, newOwner: users.bob });

// Run the test
container.transferOwnership({ newOwner: users.bob });

// Assert the actual and expected owner
address actualOwner = container.owner();
assertEq(actualOwner, users.bob);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
transferOwnership.t.sol
├── when the caller IS NOT the container owner
│ └── it should revert with the {Unauthorized} error
└── when the caller IS the container owner
├── when the new owner address IS the zero address
│ └── it should revert with the {InvalidOwnerZeroAddress} error
└── when the new owner address IS NOT the zero address
├── it should update the owner
└── it should emit a {OwnershipTransferred} event
7 changes: 7 additions & 0 deletions test/utils/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ library Errors {

/// @notice Thrown when `msg.sender` is not the stream's sender
error SablierV2Lockup_Unauthorized(uint256 streamId, address caller);

/*//////////////////////////////////////////////////////////////////////////
OWNABLE
//////////////////////////////////////////////////////////////////////////*/

/// @notice Thrown when attempting to transfer ownership to the zero address
error InvalidOwnerZeroAddress();
}
9 changes: 9 additions & 0 deletions test/utils/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ abstract contract Events {
/// @notice Emitted when an invoice is canceled
/// @param id The ID of the invoice
event InvoiceCanceled(uint256 indexed id);

/*//////////////////////////////////////////////////////////////////////////
OWNABLE
//////////////////////////////////////////////////////////////////////////*/

/// @notice Emitted when the address of the owner is updated
/// @param oldOwner The address of the previous owner
/// @param newOwner The address of the new owner
event OwnershipTransferred(address indexed oldOwner, address newOwner);
}

0 comments on commit f11b6f5

Please sign in to comment.