From 32230557414ad68acefde487724a645b08ac8f5e Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure Date: Thu, 21 Sep 2023 15:18:51 +0100 Subject: [PATCH] fix: added tests --- src/example/NFTPortal.sol | 23 ++++++------ test/example/NFTPortal.t.sol | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 test/example/NFTPortal.t.sol diff --git a/src/example/NFTPortal.sol b/src/example/NFTPortal.sol index 454459fe..7e651b7b 100644 --- a/src/example/NFTPortal.sol +++ b/src/example/NFTPortal.sol @@ -9,29 +9,28 @@ import { Attestation, AttestationPayload } from "../types/Structs.sol"; /** * @title NFT Portal * @author Consensys - * @notice This contract aims to provide ERC 721 features - * @dev This Portal implements ERC 721 - balanceOf and ownerOf functions + * @notice This contract aims to provide ERC 721 compatibility + * @dev This Portal implements parts of ERC 721 - balanceOf and ownerOf functions */ contract NFTPortal is AbstractPortal, ERC721 { mapping(bytes owner => uint256 numberOfAttestations) private numberOfAttestationsPerOwner; - constructor(address[] memory modules, address router) AbstractPortal(modules, router) ERC721("1", "2") {} + constructor( + address[] memory modules, + address router + ) AbstractPortal(modules, router) ERC721("NFTPortal", "NFTPortal") {} - /** @notice Count all NFTs assigned to an owner - * @dev NFTs assigned to the zero address are considered invalid, and this - * function throws for queries about the zero address. + /** @notice Count all attestations assigned to an owner * @param owner An address for whom to query the balance - * @return The number of NFTs owned by `owner`, possibly zero + * @return The number of attestations owned by `owner`, possibly zero */ function balanceOf(address owner) public view virtual override returns (uint256) { return numberOfAttestationsPerOwner[abi.encode(owner)]; } - /** @notice Find the owner of an NFT - * @dev NFTs assigned to zero address are considered invalid, and queries - * about them do throw. - * @param tokenId The identifier for an NFT - * @return The address of the owner of the NFT + /** @notice Find the owner of an attestation + * @param tokenId The identifier for an attestation + * @return The address of the owner of the attestation */ function ownerOf(uint256 tokenId) public view virtual override returns (address) { bytes32 attestationId = bytes32(tokenId); diff --git a/test/example/NFTPortal.t.sol b/test/example/NFTPortal.t.sol new file mode 100644 index 00000000..160dc27b --- /dev/null +++ b/test/example/NFTPortal.t.sol @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.21; + +import { Test } from "forge-std/Test.sol"; +import { NFTPortal } from "../../src/example/NFTPortal.sol"; +import { Router } from "../../src/Router.sol"; +import { AbstractPortal } from "../../src/interface/AbstractPortal.sol"; +import { AttestationPayload } from "../../src/types/Structs.sol"; +import { AttestationRegistryMock } from "../mocks/AttestationRegistryMock.sol"; +import { ModuleRegistryMock } from "../mocks/ModuleRegistryMock.sol"; +import { IERC721 } from "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol"; +import { IERC165 } from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol"; + +contract NFTPortalTest is Test { + address public attester = makeAddr("attester"); + NFTPortal public nftPortal; + address[] public modules = new address[](0); + ModuleRegistryMock public moduleRegistryMock = new ModuleRegistryMock(); + AttestationRegistryMock public attestationRegistryMock = new AttestationRegistryMock(); + Router public router = new Router(); + + event Initialized(uint8 version); + event AttestationRegistered(); + event BulkAttestationsRegistered(); + + function setUp() public { + router.initialize(); + router.updateModuleRegistry(address(moduleRegistryMock)); + router.updateAttestationRegistry(address(attestationRegistryMock)); + + nftPortal = new NFTPortal(modules, address(router)); + } + + function test_balanceOf_ownerOf() public { + // Create attestation payload + AttestationPayload memory attestationPayload = AttestationPayload( + bytes32(uint256(1)), + uint64(block.timestamp + 1 days), + abi.encode(address(1)), // Convert address(1) to bytes and use as subject + new bytes(1) + ); + // Create validation payload + bytes[] memory validationPayload = new bytes[](0); + vm.expectEmit(true, true, true, true); + emit AttestationRegistered(); + nftPortal.attest(attestationPayload, validationPayload); + vm.expectEmit(true, true, true, true); + emit AttestationRegistered(); + nftPortal.attest(attestationPayload, validationPayload); + + uint256 balance = nftPortal.balanceOf(address(1)); + assertEq(balance, 2); + + address ownerOfFirstAttestation = nftPortal.ownerOf(1); + address ownerOfSecondAttestation = nftPortal.ownerOf(2); + assertEq(ownerOfFirstAttestation, address(1)); + assertEq(ownerOfSecondAttestation, address(1)); + } + + function testSupportsInterface() public { + bool isIERC165Supported = nftPortal.supportsInterface(type(IERC165).interfaceId); + assertEq(isIERC165Supported, true); + bool isIERC721Supported = nftPortal.supportsInterface(type(IERC721).interfaceId); + assertEq(isIERC721Supported, true); + bool isEASAbstractPortalSupported = nftPortal.supportsInterface(type(AbstractPortal).interfaceId); + assertEq(isEASAbstractPortalSupported, true); + } +}