Skip to content

Writing Custom Tests

This guide helps you write additional tests for the Zaphenath contract using the Foundry testing framework.

🧰 Setup

Tests are located in the test/ directory and follow this structure:

contract ZaphenathTest is Test {
    Zaphenath public zaph;

    function setUp() public {
        zaph = new Zaphenath();
    }
}

Use vm.prank() to simulate calls from different addresses:

vm.prank(rachel);
zaph.createKey(...);

βœ… Common Tools

Tool Description
vm.prank() Simulate msg.sender
vm.warp() Simulate time passing
vm.expectRevert() Expect failure with specific reason
console.log() Debug inside tests

πŸ§ͺ Test Ideas

  • πŸ“€ A Writer tries to delete a key
  • πŸ” A Reader tries to update a key (should fail)
  • ⏱ A custodian pings just before timeout
  • 🚫 Unauthorized user tries to assign custodian

🧩 Example: Prevent Non-Owner Ping Without Permission

function testCustodianCannotPingIfNotAllowed() public {
    vm.prank(owner);
    zaph.setCustodian(keyId, owner, jacob, Role.Writer, false);

    vm.prank(jacob);
    vm.expectRevert("Not authorized to ping");
    zaph.ping(keyId, owner);
}

πŸ§ͺ Don’t forget to run your tests with:

forge test -vv

➑️ Visit the Support FAQ