Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaryash90 committed Feb 23, 2024
1 parent cd5b7ea commit a634d23
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 124 deletions.
80 changes: 79 additions & 1 deletion src/test/ContractPublisher.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "contracts/infra/interface/IContractPublisher.sol";
import "contracts/infra/TWRegistry.sol";

// Test helpers
import { BaseTest } from "./utils/BaseTest.sol";
import { BaseTest, MockContractPublisher } from "./utils/BaseTest.sol";
import "@openzeppelin/contracts/utils/Create2.sol";

contract MockCustomContract {
Expand Down Expand Up @@ -114,6 +114,84 @@ contract ContractPublisherTest is BaseTest, IContractPublisherData {
// assertEq(customContract.implementation, address(0));
// }

function test_state_setPrevPublisher() public {
// === when prevPublisher address is address(0)
vm.prank(factoryAdmin);
byoc.setPrevPublisher(IContractPublisher(address(0)));

assertEq(byoc.getAllPublishedContracts(publisher).length, 0);
assertEq(address(byoc.prevPublisher()), address(0));

string memory contractId = "MyContract";
vm.prank(publisher);
byoc.publishContract(
publisher,
contractId,
publishMetadataUri,
compilerMetadataUri,
keccak256(type(MockCustomContract).creationCode),
address(0)
);
IContractPublisher.CustomContractInstance[] memory contracts = byoc.getAllPublishedContracts(publisher);
assertEq(contracts.length, 1);
assertEq(contracts[0].contractId, "MyContract");

// === when prevPublisher address is set to MockPublisher
address mock = address(new MockContractPublisher());
vm.prank(factoryAdmin);
byoc.setPrevPublisher(IContractPublisher(mock));

contracts = byoc.getAllPublishedContracts(publisher);
assertEq(contracts.length, 2);
assertEq(address(byoc.prevPublisher()), mock);
assertEq(contracts[0].contractId, "MockContract");
assertEq(contracts[1].contractId, "MyContract");
}

function test_revert_setPrevPublisher() public {
vm.expectRevert("Not authorized");
byoc.setPrevPublisher(IContractPublisher(address(0)));
}

function test_state_setPublisherProfileUri() public {
address user = address(0x123);
string memory uriOne = "ipfs://one";
string memory uriTwo = "ipfs://two";

// user updating for self
vm.prank(user);
byoc.setPublisherProfileUri(user, uriOne);
assertEq(byoc.getPublisherProfileUri(user), uriOne);

// random caller
vm.prank(address(0x345));
vm.expectRevert("Registry paused or caller not authorized");
byoc.setPublisherProfileUri(user, uriOne);

// MIGRATION_ROLE holder updating for a user
vm.prank(factoryAdmin);
byoc.setPublisherProfileUri(user, uriTwo);
assertEq(byoc.getPublisherProfileUri(user), uriTwo);
}

function test_state_setPublisherProfileUri_whenPaused() public {
vm.prank(factoryAdmin);
byoc.setPause(true);
address user = address(0x123);
string memory uriOne = "ipfs://one";
string memory uriTwo = "ipfs://two";

// user updating for self
vm.prank(user);
vm.expectRevert("Registry paused or caller not authorized");
byoc.setPublisherProfileUri(user, uriOne);

// MIGRATION_ROLE holder updating for a user
vm.prank(factoryAdmin);
byoc.setPublisherProfileUri(user, uriTwo);
assertEq(byoc.getPublisherProfileUri(user), uriTwo);
}

function test_publish_revert_unapprovedCaller() public {
string memory contractId = "MyContract";

Expand Down
6 changes: 3 additions & 3 deletions src/test/LoyaltyCard.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "@ds-test/test.sol";
import "./utils/BaseTest.sol";
import "contracts/infra/TWProxy.sol";
import { Strings } from "contracts/lib/Strings.sol";
import { LoyaltyCard } from "contracts/prebuilts/loyalty/LoyaltyCard.sol";
import { LoyaltyCard, NFTMetadata } from "contracts/prebuilts/loyalty/LoyaltyCard.sol";

contract LoyaltyCardTest is BaseTest {
LoyaltyCard internal loyaltyCard;
Expand Down Expand Up @@ -320,7 +320,7 @@ contract LoyaltyCardTest is BaseTest {
function test_setTokenURI_revert_NotAuthorized() public {
string memory uri = "uri_string";

vm.expectRevert("NFTMetadata: not authorized to set metadata.");
vm.expectRevert(abi.encodeWithSelector(NFTMetadata.NFTMetadataUnauthorized.selector));
vm.prank(address(0x1));
loyaltyCard.setTokenURI(0, uri);
}
Expand All @@ -331,7 +331,7 @@ contract LoyaltyCardTest is BaseTest {
vm.startPrank(signer);
loyaltyCard.freezeMetadata();

vm.expectRevert("NFTMetadata: metadata is frozen.");
vm.expectRevert(abi.encodeWithSelector(NFTMetadata.NFTMetadataFrozen.selector, 0));
loyaltyCard.setTokenURI(0, uri);
}

Expand Down
72 changes: 27 additions & 45 deletions src/test/Multiwrap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.0;

import { Multiwrap } from "contracts/prebuilts/multiwrap/Multiwrap.sol";
import { ITokenBundle } from "contracts/extension/interface/ITokenBundle.sol";
import { CurrencyTransferLib } from "contracts/lib/CurrencyTransferLib.sol";

// Test imports
import { MockERC20 } from "./mocks/MockERC20.sol";
Expand Down Expand Up @@ -112,14 +113,7 @@ contract MultiwrapTest is BaseTest {
bytes32 role = keccak256("MINTER_ROLE");

vm.prank(caller);
vm.expectRevert(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(caller), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
)
);
vm.expectRevert(abi.encodeWithSelector(Permissions.PermissionsUnauthorizedAccount.selector, caller, role));

multiwrap.renounceRole(role, caller);
}
Expand All @@ -132,14 +126,7 @@ contract MultiwrapTest is BaseTest {
bytes32 role = keccak256("MINTER_ROLE");

vm.prank(deployer);
vm.expectRevert(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(target), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
)
);
vm.expectRevert(abi.encodeWithSelector(Permissions.PermissionsUnauthorizedAccount.selector, target, role));

multiwrap.revokeRole(role, target);
}
Expand Down Expand Up @@ -334,17 +321,14 @@ contract MultiwrapTest is BaseTest {

address recipient = address(0x123);

string memory errorMsg = string(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(wrappedContent[0].assetContract), 20),
" is missing role ",
Strings.toHexString(uint256(keccak256("ASSET_ROLE")), 32)
vm.prank(address(tokenOwner));
vm.expectRevert(
abi.encodeWithSelector(
Permissions.PermissionsUnauthorizedAccount.selector,
address(erc20),
keccak256("ASSET_ROLE")
)
);

vm.prank(address(tokenOwner));
vm.expectRevert(bytes(errorMsg));
multiwrap.wrap(wrappedContent, uriForWrappedToken, recipient);
}

Expand All @@ -357,17 +341,14 @@ contract MultiwrapTest is BaseTest {

address recipient = address(0x123);

string memory errorMsg = string(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(address(tokenOwner)), 20),
" is missing role ",
Strings.toHexString(uint256(keccak256("MINTER_ROLE")), 32)
vm.prank(address(tokenOwner));
vm.expectRevert(
abi.encodeWithSelector(
Permissions.PermissionsUnauthorizedAccount.selector,
address(tokenOwner),
keccak256("MINTER_ROLE")
)
);

vm.prank(address(tokenOwner));
vm.expectRevert(bytes(errorMsg));
multiwrap.wrap(wrappedContent, uriForWrappedToken, recipient);
}

Expand All @@ -388,7 +369,9 @@ contract MultiwrapTest is BaseTest {
});

vm.prank(address(tokenOwner));
vm.expectRevert("msg.value != amount");
vm.expectRevert(
abi.encodeWithSelector(CurrencyTransferLib.CurrencyTransferLibMismatchedValue.selector, 0, 10 ether)
);
multiwrap.wrap(nativeTokenContentToWrap, uriForWrappedToken, recipient);
}

Expand Down Expand Up @@ -529,7 +512,9 @@ contract MultiwrapTest is BaseTest {
});

vm.prank(address(tokenOwner));
vm.expectRevert("msg.value != amount");
vm.expectRevert(
abi.encodeWithSelector(CurrencyTransferLib.CurrencyTransferLibMismatchedValue.selector, 10 ether, 20 ether)
);
multiwrap.wrap{ value: 10 ether }(nativeTokenContentToWrap, uriForWrappedToken, recipient);

assertEq(address(multiwrap).balance, 10 ether);
Expand Down Expand Up @@ -743,17 +728,14 @@ contract MultiwrapTest is BaseTest {
vm.prank(deployer);
multiwrap.revokeRole(keccak256("UNWRAP_ROLE"), address(0));

string memory errorMsg = string(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(recipient), 20),
" is missing role ",
Strings.toHexString(uint256(keccak256("UNWRAP_ROLE")), 32)
vm.prank(recipient);
vm.expectRevert(
abi.encodeWithSelector(
Permissions.PermissionsUnauthorizedAccount.selector,
recipient,
keccak256("UNWRAP_ROLE")
)
);

vm.prank(recipient);
vm.expectRevert(bytes(errorMsg));
multiwrap.unwrap(expectedIdForWrappedToken, recipient);
}

Expand Down
Loading

0 comments on commit a634d23

Please sign in to comment.