Skip to content

Latest commit

 

History

History
223 lines (161 loc) · 7.01 KB

File metadata and controls

223 lines (161 loc) · 7.01 KB

Issue #645: Contract Logic Testing - Agent Registry & Property Registry Initialization

Overview

This issue implements comprehensive initialization tests for both Agent Registry and Property Registry contracts to ensure proper state setup and authorization checks.

Implementation Status: ✓ COMPLETE

All required tests have been implemented and verified.

Test Cases Implemented

Agent Registry Initialization Tests

File: contract/contracts/agent_registry/src/tests.rs

1. Successful Initialization

#[test]
fn test_successful_initialization()
  • Purpose: Verify contract initializes with correct admin address
  • Verification:
    • Contract initializes successfully
    • Admin address is correctly stored
    • Initial state is properly set
    • Contract is ready for operations

2. Initialize Fails Without Admin Auth

#[test]
#[should_panic]
fn test_initialize_fails_without_admin_auth()
  • Purpose: Ensure initialization requires proper authorization
  • Verification:
    • Initialization fails without admin authentication
    • Error is returned to caller
    • Contract state remains uninitialized

3. Double Initialization Fails

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
fn test_double_initialization_fails()
  • Purpose: Prevent re-initialization of already initialized contract
  • Verification:
    • First initialization succeeds
    • Second initialization attempt fails with AlreadyInitialized error
    • Contract state remains unchanged

Property Registry Initialization Tests

File: contract/contracts/property_registry/src/tests.rs

1. Successful Initialization

#[test]
fn test_successful_initialization()
  • Purpose: Verify contract initializes with correct admin address
  • Verification:
    • Contract initializes successfully
    • Admin address is correctly stored
    • Initial state is properly set
    • Contract is ready for operations

2. Initialize Fails Without Admin Auth

#[test]
#[should_panic]
fn test_initialize_fails_without_admin_auth()
  • Purpose: Ensure initialization requires proper authorization
  • Verification:
    • Initialization fails without admin authentication
    • Error is returned to caller
    • Contract state remains uninitialized

3. Double Initialization Fails

#[test]
#[should_panic(expected = "Error(Contract, #1)")]
fn test_double_initialization_fails()
  • Purpose: Prevent re-initialization of already initialized contract
  • Verification:
    • First initialization succeeds
    • Second initialization attempt fails with AlreadyInitialized error
    • Contract state remains unchanged

Additional Tests Implemented

Agent Registry Extended Tests

Beyond the core initialization tests, the following tests verify the complete agent registry functionality:

  1. test_register_agent_success - Validates agent registration with profile hash
  2. test_verify_agent_success - Tests admin verification of agents
  3. test_rate_agent_success - Validates agent rating (1-5 stars)
  4. test_multiple_ratings_average - Verifies average rating calculation
  5. test_register_and_complete_transaction - Tests transaction lifecycle
  6. test_register_agent_fails_when_not_initialized - Ensures initialization requirement
  7. test_register_agent_fails_when_already_registered - Prevents duplicate registration
  8. test_verify_agent_fails_when_not_admin - Validates admin-only operations
  9. test_verify_agent_fails_when_already_verified - Prevents double verification
  10. test_rate_agent_fails_with_invalid_score - Validates rating bounds (1-5)

Property Registry Extended Tests

Beyond the core initialization tests, the following tests verify the complete property registry functionality:

  1. test_register_property_success - Validates property registration
  2. test_verify_property_success - Tests admin verification of properties
  3. test_property_count_increments - Verifies counter functionality
  4. test_multiple_landlords_can_register_properties - Tests multi-landlord support
  5. test_register_property_fails_if_not_initialized - Ensures initialization requirement
  6. test_register_property_fails_if_already_exists - Prevents duplicate registration
  7. test_verify_property_fails_if_not_admin - Validates admin-only operations
  8. test_verify_property_fails_if_already_verified - Prevents double verification
  9. test_registered_at_timestamp - Verifies timestamp recording
  10. test_verified_at_timestamp - Verifies verification timestamp

Test Execution

Run All Agent Registry Tests

cd contract
cargo test --lib agent_registry::tests

Run All Property Registry Tests

cd contract
cargo test --lib property_registry::tests

Run Specific Test

cargo test --lib agent_registry::tests::test_successful_initialization

Key Implementation Details

Authorization Pattern

Both contracts use the same authorization pattern:

admin.require_auth();  // Requires admin to authorize the transaction

State Management

  • Persistent Storage: Uses env.storage().persistent() for initialization flag
  • Instance Storage: Uses env.storage().instance() for contract state
  • TTL Extension: Extends TTL to 500,000 ledger entries for durability

Error Handling

  • AlreadyInitialized: Error code #1 - Returned when attempting to re-initialize
  • NotInitialized: Error code #2 - Returned when operations require initialization

Testing Best Practices Used

  1. Mock Authentication: env.mock_all_auths() for testing authorization
  2. Address Generation: Address::generate(&env) for unique test addresses
  3. Error Verification: #[should_panic(expected = "...")] for error testing
  4. Try Methods: try_initialize() for non-panicking error handling
  5. State Verification: Direct state retrieval and assertion

Related Functions

Agent Registry

  • initialize(env: Env, admin: Address) -> Result<(), AgentError>
  • get_state(env: Env) -> Option<ContractState>

Property Registry

  • initialize(env: Env, admin: Address) -> Result<(), PropertyError>
  • get_state(env: Env) -> Option<ContractState>

Verification Checklist

  • Agent Registry initialization tests implemented
  • Property Registry initialization tests implemented
  • Authorization checks verified
  • Double initialization prevention tested
  • State persistence verified
  • Error handling validated
  • All tests passing
  • Documentation complete

Notes

  • Both contracts follow identical initialization patterns for consistency
  • Tests use Soroban SDK testing utilities for proper environment setup
  • Authorization is enforced at the contract level using require_auth()
  • State is stored in both persistent and instance storage for different purposes
  • TTL is extended to ensure data persistence across ledger entries

Related Issues

  • #646: Escrow Lifecycle & Dispute Resolution
  • #647: Payment Processing & Late Fee Calculation
  • #648: Rent Obligation NFT & Agent Registration