diff --git a/TestsList.md b/TestsList.md index a690ef7..0b09539 100644 --- a/TestsList.md +++ b/TestsList.md @@ -492,18 +492,18 @@ Scenarios Profile Metadata URI Generic Negatives -[ ] User two should fail to set profile metadata URI for a profile that is not theirs while they are not the dispatcher +[X] User two should fail to set profile metadata URI for a profile that is not theirs while they are not the dispatcher Scenarios -[ ] User should set user two as dispatcher, user two should set profile metadata URI for user one's profile, fetched data should be accurate -[ ] Setting profile metadata should emit the correct event -[ ] Setting profile metadata via dispatcher should emit the correct event +[X] User should set user two as dispatcher, user two should set profile metadata URI for user one's profile, fetched data should be accurate +[X] Setting profile metadata should emit the correct event +[X] Setting profile metadata via dispatcher should emit the correct event Meta-tx Negatives -[ ] TestWallet should fail to set profile metadata URI with sig with signature deadline mismatch -[ ] TestWallet should fail to set profile metadata URI with sig with invalid deadline -[ ] TestWallet should fail to set profile metadata URI with sig with invalid nonce +[X] TestWallet should fail to set profile metadata URI with sig with signature deadline mismatch +[X] TestWallet should fail to set profile metadata URI with sig with invalid deadline +[X] TestWallet should fail to set profile metadata URI with sig with invalid nonce Scenarios -[ ] TestWallet should set profile metadata URI with sig, fetched data should be accurate and correct event should be emitted +[X] TestWallet should set profile metadata URI with sig, fetched data should be accurate and correct event should be emitted Mock Profile Creation Proxy Negatives diff --git a/test/foundry/Misc.t.sol b/test/foundry/Misc.t.sol index b354f81..1dd2999 100644 --- a/test/foundry/Misc.t.sol +++ b/test/foundry/Misc.t.sol @@ -36,11 +36,6 @@ contract MiscTest is BaseTest { hub.setFollowNFTURI(newProfileId, MOCK_URI); } - function testSetProfileMetadataURINotExecutorFails() public { - vm.expectRevert(Errors.ExecutorInvalid.selector); - hub.setProfileMetadataURI(newProfileId, MOCK_URI); - } - // Positives function testExecutorSetDefaultProfile() public { assertEq(hub.getDefaultProfile(profileOwner), 0); @@ -72,16 +67,6 @@ contract MiscTest is BaseTest { assertEq(hub.getFollowNFTURI(newProfileId), 'test'); } - function testExecutorSetProfileMetadataURI() public { - assertEq(hub.getProfileMetadataURI(newProfileId), ''); - vm.prank(profileOwner); - hub.setDelegatedExecutorApproval(otherSigner, true); - - vm.prank(otherSigner); - hub.setProfileMetadataURI(newProfileId, MOCK_URI); - assertEq(hub.getProfileMetadataURI(newProfileId), MOCK_URI); - } - // Meta-tx // Negatives function testSetDefaultProfileWithSigInvalidSignerFails() public { @@ -200,48 +185,6 @@ contract MiscTest is BaseTest { ); } - function testSetProfileMetadataURIWithSigInvalidSignerFails() public { - uint256 nonce = 0; - uint256 deadline = type(uint256).max; - bytes32 digest = _getSetProfileMetadataURITypedDataHash( - newProfileId, - MOCK_URI, - nonce, - deadline - ); - - vm.expectRevert(Errors.SignatureInvalid.selector); - hub.setProfileMetadataURIWithSig( - DataTypes.SetProfileMetadataURIWithSigData({ - delegatedSigner: address(0), - profileId: newProfileId, - metadataURI: MOCK_URI, - sig: _getSigStruct(otherSignerKey, digest, deadline) - }) - ); - } - - function testSetProfileMetadataURIWithSigNotExecutorFails() public { - uint256 nonce = 0; - uint256 deadline = type(uint256).max; - bytes32 digest = _getSetProfileMetadataURITypedDataHash( - newProfileId, - MOCK_URI, - nonce, - deadline - ); - - vm.expectRevert(Errors.ExecutorInvalid.selector); - hub.setProfileMetadataURIWithSig( - DataTypes.SetProfileMetadataURIWithSigData({ - delegatedSigner: otherSigner, - profileId: newProfileId, - metadataURI: MOCK_URI, - sig: _getSigStruct(otherSignerKey, digest, deadline) - }) - ); - } - // Postivies function testSetDefaultProfileWithSig() public { uint256 nonce = 0; @@ -363,51 +306,4 @@ contract MiscTest is BaseTest { ); assertEq(hub.getFollowNFTURI(newProfileId), 'test'); } - - function testSetProfileMetadataURIWithSig() public { - uint256 nonce = 0; - uint256 deadline = type(uint256).max; - bytes32 digest = _getSetProfileMetadataURITypedDataHash( - newProfileId, - MOCK_URI, - nonce, - deadline - ); - - assertEq(hub.getProfileMetadataURI(newProfileId), ''); - hub.setProfileMetadataURIWithSig( - DataTypes.SetProfileMetadataURIWithSigData({ - delegatedSigner: address(0), - profileId: newProfileId, - metadataURI: MOCK_URI, - sig: _getSigStruct(profileOwnerKey, digest, deadline) - }) - ); - assertEq(hub.getProfileMetadataURI(newProfileId), MOCK_URI); - } - - function testExecutorSetProfileMetadataURIWithSig() public { - vm.prank(profileOwner); - hub.setDelegatedExecutorApproval(otherSigner, true); - - uint256 nonce = 0; - uint256 deadline = type(uint256).max; - bytes32 digest = _getSetProfileMetadataURITypedDataHash( - newProfileId, - MOCK_URI, - nonce, - deadline - ); - - assertEq(hub.getProfileMetadataURI(newProfileId), ''); - hub.setProfileMetadataURIWithSig( - DataTypes.SetProfileMetadataURIWithSigData({ - delegatedSigner: otherSigner, - profileId: newProfileId, - metadataURI: MOCK_URI, - sig: _getSigStruct(otherSignerKey, digest, deadline) - }) - ); - assertEq(hub.getProfileMetadataURI(newProfileId), MOCK_URI); - } } diff --git a/test/foundry/ProfileMetadataURI.t.sol b/test/foundry/ProfileMetadataURI.t.sol new file mode 100644 index 0000000..05b7b8a --- /dev/null +++ b/test/foundry/ProfileMetadataURI.t.sol @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import './base/BaseTest.t.sol'; +import './MetaTxNegatives.t.sol'; +import {Events} from 'contracts/libraries/Events.sol'; + +contract ProfileMetadataURITest is BaseTest { + function _setProfileMetadataURI( + uint256 pk, + uint256 profileId, + string memory metadataURI + ) internal virtual { + vm.prank(vm.addr(pk)); + hub.setProfileMetadataURI(profileId, metadataURI); + } + + // Negatives + function testCannotSetProfileMetadataURINotExecutor() public { + vm.expectRevert(Errors.ExecutorInvalid.selector); + _setProfileMetadataURI({ + pk: alienSignerKey, + profileId: newProfileId, + metadataURI: MOCK_URI + }); + } + + // Positives + function testExecutorSetProfileMetadataURI() public { + assertEq(hub.getProfileMetadataURI(newProfileId), ''); + vm.prank(profileOwner); + hub.setDelegatedExecutorApproval(otherSigner, true); + + _setProfileMetadataURI({ + pk: otherSignerKey, + profileId: newProfileId, + metadataURI: MOCK_URI + }); + assertEq(hub.getProfileMetadataURI(newProfileId), MOCK_URI); + } + + function testSetProfileMetadataURI() public { + assertEq(hub.getProfileMetadataURI(newProfileId), ''); + + _setProfileMetadataURI({ + pk: profileOwnerKey, + profileId: newProfileId, + metadataURI: MOCK_URI + }); + assertEq(hub.getProfileMetadataURI(newProfileId), MOCK_URI); + } + + // Events + function expectProfileMetadataSetEvent() public { + vm.expectEmit(true, true, true, true, address(hub)); + emit Events.ProfileMetadataSet({ + profileId: newProfileId, + metadata: MOCK_URI, + timestamp: block.timestamp + }); + } + + function testSetProfileMetadataURI_EmitsProperEvent() public { + expectProfileMetadataSetEvent(); + testSetProfileMetadataURI(); + } + + function testExecutorSetProfileMetadataURI_EmitsProperEvent() public { + expectProfileMetadataSetEvent(); + testExecutorSetProfileMetadataURI(); + } +} + +contract ProfileMetadataURITest_MetaTx is ProfileMetadataURITest, MetaTxNegatives { + mapping(address => uint256) cachedNonceByAddress; + + function setUp() public override(MetaTxNegatives, TestSetup) { + TestSetup.setUp(); + MetaTxNegatives.setUp(); + + cachedNonceByAddress[alienSigner] = _getSigNonce(alienSigner); + cachedNonceByAddress[otherSigner] = _getSigNonce(otherSigner); + cachedNonceByAddress[profileOwner] = _getSigNonce(profileOwner); + } + + function _setProfileMetadataURI( + uint256 pk, + uint256 profileId, + string memory metadataURI + ) internal virtual override { + address signer = vm.addr(pk); + uint256 nonce = cachedNonceByAddress[signer]; + uint256 deadline = type(uint256).max; + + bytes32 digest = _getSetProfileMetadataURITypedDataHash( + newProfileId, + MOCK_URI, + nonce, + deadline + ); + + hub.setProfileMetadataURIWithSig( + DataTypes.SetProfileMetadataURIWithSigData({ + delegatedSigner: signer == profileOwner ? address(0) : signer, + profileId: newProfileId, + metadataURI: MOCK_URI, + sig: _getSigStruct(pk, digest, deadline) + }) + ); + } + + function _executeMetaTx( + uint256 signerPk, + uint256 nonce, + uint256 deadline + ) internal virtual override { + bytes32 digest = _getSetProfileMetadataURITypedDataHash( + newProfileId, + MOCK_URI, + nonce, + deadline + ); + + hub.setProfileMetadataURIWithSig( + DataTypes.SetProfileMetadataURIWithSigData({ + delegatedSigner: address(0), + profileId: newProfileId, + metadataURI: MOCK_URI, + sig: _getSigStruct(signerPk, digest, deadline) + }) + ); + } + + function _getDefaultMetaTxSignerPk() internal virtual override returns (uint256) { + return profileOwnerKey; + } +} diff --git a/test/foundry/base/TestSetup.t.sol b/test/foundry/base/TestSetup.t.sol index b4b467d..f8ce464 100644 --- a/test/foundry/base/TestSetup.t.sol +++ b/test/foundry/base/TestSetup.t.sol @@ -38,8 +38,10 @@ contract TestSetup is Test, ForkManagement { uint256 constant otherSignerKey = 0x737562; uint256 constant profileOwnerKey = 0x04546b; + uint256 constant alienSignerKey = 0x123456; address immutable profileOwner = vm.addr(profileOwnerKey); address immutable otherSigner = vm.addr(otherSignerKey); + address immutable alienSigner = vm.addr(alienSignerKey); address immutable me = address(this); bytes32 domainSeparator;