Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Jun 4, 2024
1 parent e773776 commit c81a980
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/GrantRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ contract GrantRegistrar is Ownable2Step {
uint256[8] calldata proof
) external payable {
uint256 lastClaimedGrantId = nullifierHashes[nullifierHash];
uint256 maxGrantId = (((currentGrantId) / nGrantsValidity) + 1) * nGrantsValidity - 1;
uint256 maxGrantId = currentGrantId + nGrantsValidity;

if (lastClaimedGrantId != 0 && lastClaimedGrantId <= maxGrantId) revert InvalidNullifier();

Expand Down
111 changes: 110 additions & 1 deletion src/test/GrantRegistrar.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.19;
import { PRBTest } from '@prb/test/PRBTest.sol';
import { WorldIDIdentityManagerRouterMock } from 'src/test/mock/WorldIDIdentityManagerRouterMock.sol';
import { TestERC20 } from './mock/TestERC20.sol';
import { GrantRegistrar } from '../GrantRegistrar.sol';
import { GrantRegistrar, IWorldIDGroups } from '../GrantRegistrar.sol';
import { WLDGrant } from '../WLDGrant.sol';
import { IGrant } from '../IGrant.sol';

Expand Down Expand Up @@ -72,6 +72,39 @@ contract GrantRegistrarTest is PRBTest {
registrar.verify(user, worldIDRoot, nullifierHash, proof);

assertTrue(registrar.canClaimGrant(user, 10));
assertTrue(registrar.canClaimGrant(user, 11));
assertTrue(registrar.canClaimGrant(user, 12));
assertTrue(registrar.canClaimGrant(user, 13));
assertFalse(registrar.canClaimGrant(user, 14));
}

function testCannotReverifyBeforeExpiry(
uint256 worldIDRoot,
uint256 nullifierHash,
uint256 grantsPassed
) public {
vm.assume(worldIDRoot != 0 && nullifierHash != 0 && grantsPassed != 0 && grantsPassed <= 3);

vm.warp(claimTime);

assertFalse(registrar.canClaimGrant(user, 10));

vm.prank(caller);
registrar.verify(user, worldIDRoot, nullifierHash, proof);

assertTrue(registrar.canClaimGrant(user, 10));

vm.prank(manager);
registrar.setCurrentGrant(10 + grantsPassed);

assertTrue(registrar.canClaimGrant(user, 10 + grantsPassed));

vm.expectRevert(GrantRegistrar.InvalidNullifier.selector);
vm.prank(caller);
registrar.verify(user, worldIDRoot, nullifierHash, proof);

assertTrue(registrar.canClaimGrant(user, 10 + grantsPassed));
assertFalse(registrar.canClaimGrant(user, 10 + 4));
}

/// @notice Tests that nullifier hash for the same action cannot be consumed twice
Expand All @@ -90,4 +123,80 @@ contract GrantRegistrarTest is PRBTest {
vm.prank(caller);
registrar.verify(user, worldIDRoot, nullifierHash, proof);
}

function testCannotUpdateWorldIdRouterIfNotManager(address notManager) public {
vm.assume(notManager != manager && notManager != address(0));
assertEq(address(registrar.worldIdRouter()), address(worldIDIdentityManagerRouterMock));

vm.expectRevert();
vm.prank(notManager);
registrar.setWorldIdRouter(IWorldIDGroups(address(0x1)));

assertEq(address(registrar.worldIdRouter()), address(worldIDIdentityManagerRouterMock));
}

function testCannotUpdateGroupIdIfNotManager(address notManager) public {
vm.assume(notManager != manager && notManager != address(0));
assertEq(registrar.groupId(), 1);

vm.expectRevert();
vm.prank(notManager);
registrar.setGroupId(2);

assertEq(registrar.groupId(), 1);
}

function testCannotUpdateCurrentGrantIfNotManager(address notManager) public {
vm.assume(notManager != manager && notManager != address(0));
assertEq(registrar.currentGrantId(), 10);

vm.expectRevert();
vm.prank(notManager);
registrar.setCurrentGrant(11);

assertEq(registrar.currentGrantId(), 10);
}

function testCannotReduceCurrentGrant(uint256 newGrant) public {
vm.assume(newGrant < 10);
assertEq(registrar.currentGrantId(), 10);

vm.expectRevert(GrantRegistrar.InvalidConfiguration.selector);
vm.prank(manager);
registrar.setCurrentGrant(newGrant);

assertEq(registrar.currentGrantId(), 10);
}

function testCannotUpdateGrantValidityIfNotManager(address notManager) public {
vm.assume(notManager != manager && notManager != address(0));
assertEq(registrar.nGrantsValidity(), 3);

vm.expectRevert();
vm.prank(notManager);
registrar.setGrantValidity(10);

assertEq(registrar.nGrantsValidity(), 3);
}

function testCannotReduceGrantValidity(uint256 newGrantValidity) public {
vm.assume(newGrantValidity < 3);
assertEq(registrar.nGrantsValidity(), 3);

vm.expectRevert(GrantRegistrar.InvalidConfiguration.selector);
vm.prank(manager);
registrar.setGrantValidity(newGrantValidity);

assertEq(registrar.nGrantsValidity(), 3);
}

function testCannotRenounceOwnership(address caller) public {
assertEq(registrar.owner(), manager);

vm.expectRevert();
vm.prank(caller);
registrar.renounceOwnership();

assertEq(registrar.owner(), manager);
}
}

0 comments on commit c81a980

Please sign in to comment.