/ test / CommunityOwnerTokenRegistry.t.sol
CommunityOwnerTokenRegistry.t.sol
  1  // SPDX-License-Identifier: UNLICENSED
  2  pragma solidity ^0.8.17;
  3  
  4  import { Test } from "forge-std/Test.sol";
  5  import { DeployContracts } from "../script/DeployContracts.s.sol";
  6  import { DeploymentConfig } from "../script/DeploymentConfig.s.sol";
  7  import { CommunityOwnerTokenRegistry } from "../contracts/CommunityOwnerTokenRegistry.sol";
  8  import { CommunityTokenDeployer } from "../contracts/CommunityTokenDeployer.sol";
  9  
 10  contract CommunityOwnerTokenRegistryTest is Test {
 11      event TokenDeployerAddressChange(address indexed);
 12      event AddEntry(address indexed, address indexed);
 13  
 14      DeploymentConfig internal deploymentConfig;
 15  
 16      CommunityTokenDeployer internal tokenDeployer;
 17  
 18      CommunityOwnerTokenRegistry internal tokenRegistry;
 19  
 20      address internal deployer;
 21  
 22      address internal tokenDeployerAccount = makeAddr("tokenDeployer");
 23  
 24      address internal communityAddress = makeAddr("communityAddress");
 25  
 26      address internal tokenAddress = makeAddr("tokenAddress");
 27  
 28      function setUp() public virtual {
 29          DeployContracts deployment = new DeployContracts();
 30          (tokenDeployer, tokenRegistry,,, deploymentConfig) = deployment.run();
 31          deployer = deploymentConfig.deployer();
 32      }
 33  }
 34  
 35  contract DeploymentTest is CommunityOwnerTokenRegistryTest {
 36      function setUp() public virtual override {
 37          CommunityOwnerTokenRegistryTest.setUp();
 38      }
 39  
 40      function test_Deployment() public {
 41          assertEq(tokenDeployer.owner(), deployer);
 42          assertEq(tokenRegistry.tokenDeployer(), address(tokenDeployer));
 43      }
 44  }
 45  
 46  contract SetCommunityTokenDeployerAddressTest is CommunityOwnerTokenRegistryTest {
 47      function setUp() public virtual override {
 48          CommunityOwnerTokenRegistryTest.setUp();
 49      }
 50  
 51      function test_RevertWhen_SenderIsNotOwner() public {
 52          vm.expectRevert(bytes("Ownable: caller is not the owner"));
 53          tokenRegistry.setCommunityTokenDeployerAddress(makeAddr("someAddress"));
 54      }
 55  
 56      function test_RevertWhen_InvalidTokenDeployerAddress() public {
 57          vm.prank(deployer);
 58          vm.expectRevert(CommunityOwnerTokenRegistry.CommunityOwnerTokenRegistry_InvalidAddress.selector);
 59          tokenRegistry.setCommunityTokenDeployerAddress(address(0));
 60      }
 61  
 62      function test_SetCommunityTokenDeployerAddress() public {
 63          address newAddress = makeAddr("someAddress");
 64          vm.prank(deployer);
 65          vm.expectEmit(true, true, true, false);
 66          emit TokenDeployerAddressChange(newAddress);
 67          tokenRegistry.setCommunityTokenDeployerAddress(newAddress);
 68          assertEq(tokenRegistry.tokenDeployer(), newAddress);
 69      }
 70  }
 71  
 72  contract AddEntryTest is CommunityOwnerTokenRegistryTest {
 73      function setUp() public virtual override {
 74          CommunityOwnerTokenRegistryTest.setUp();
 75          vm.prank(deployer);
 76          tokenRegistry.setCommunityTokenDeployerAddress(tokenDeployerAccount);
 77      }
 78  
 79      function test_RevertWhen_SenderIsNotTokenDeployer() public {
 80          vm.expectRevert(CommunityOwnerTokenRegistry.CommunityOwnerTokenRegistry_NotAuthorized.selector);
 81          tokenRegistry.addEntry(communityAddress, tokenAddress);
 82      }
 83  
 84      function test_RevertWhen_InvalidAddress() public {
 85          vm.startPrank(tokenDeployerAccount);
 86          vm.expectRevert(CommunityOwnerTokenRegistry.CommunityOwnerTokenRegistry_InvalidAddress.selector);
 87          tokenRegistry.addEntry(address(0), tokenAddress);
 88          vm.expectRevert(CommunityOwnerTokenRegistry.CommunityOwnerTokenRegistry_InvalidAddress.selector);
 89          tokenRegistry.addEntry(communityAddress, address(0));
 90      }
 91  
 92      function test_RevertWhen_EntryAlreadyExists() public {
 93          vm.startPrank(tokenDeployerAccount);
 94          tokenRegistry.addEntry(communityAddress, tokenAddress);
 95          vm.expectRevert(CommunityOwnerTokenRegistry.CommunityOwnerTokenRegistry_EntryAlreadyExists.selector);
 96          tokenRegistry.addEntry(communityAddress, tokenAddress);
 97      }
 98  
 99      function test_AddEntry() public {
100          vm.startPrank(tokenDeployerAccount);
101          vm.expectEmit(true, true, true, true);
102          emit AddEntry(communityAddress, tokenAddress);
103          tokenRegistry.addEntry(communityAddress, tokenAddress);
104  
105          assertEq(tokenRegistry.getEntry(communityAddress), tokenAddress);
106      }
107  }
108  
109  contract GetEntryTest is CommunityOwnerTokenRegistryTest {
110      function setUp() public virtual override {
111          CommunityOwnerTokenRegistryTest.setUp();
112      }
113  
114      function test_ReturnZeroAddressIfEntryDoesNotExist() public {
115          assertEq(tokenRegistry.getEntry(makeAddr("someAddress")), address(0));
116      }
117  }