This issue implements comprehensive initialization tests for both Agent Registry and Property Registry contracts to ensure proper state setup and authorization checks.
All required tests have been implemented and verified.
File: contract/contracts/agent_registry/src/tests.rs
#[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
#[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
#[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
File: contract/contracts/property_registry/src/tests.rs
#[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
#[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
#[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
Beyond the core initialization tests, the following tests verify the complete agent registry functionality:
- test_register_agent_success - Validates agent registration with profile hash
- test_verify_agent_success - Tests admin verification of agents
- test_rate_agent_success - Validates agent rating (1-5 stars)
- test_multiple_ratings_average - Verifies average rating calculation
- test_register_and_complete_transaction - Tests transaction lifecycle
- test_register_agent_fails_when_not_initialized - Ensures initialization requirement
- test_register_agent_fails_when_already_registered - Prevents duplicate registration
- test_verify_agent_fails_when_not_admin - Validates admin-only operations
- test_verify_agent_fails_when_already_verified - Prevents double verification
- test_rate_agent_fails_with_invalid_score - Validates rating bounds (1-5)
Beyond the core initialization tests, the following tests verify the complete property registry functionality:
- test_register_property_success - Validates property registration
- test_verify_property_success - Tests admin verification of properties
- test_property_count_increments - Verifies counter functionality
- test_multiple_landlords_can_register_properties - Tests multi-landlord support
- test_register_property_fails_if_not_initialized - Ensures initialization requirement
- test_register_property_fails_if_already_exists - Prevents duplicate registration
- test_verify_property_fails_if_not_admin - Validates admin-only operations
- test_verify_property_fails_if_already_verified - Prevents double verification
- test_registered_at_timestamp - Verifies timestamp recording
- test_verified_at_timestamp - Verifies verification timestamp
cd contract
cargo test --lib agent_registry::testscd contract
cargo test --lib property_registry::testscargo test --lib agent_registry::tests::test_successful_initializationBoth contracts use the same authorization pattern:
admin.require_auth(); // Requires admin to authorize the transaction- 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
- AlreadyInitialized: Error code #1 - Returned when attempting to re-initialize
- NotInitialized: Error code #2 - Returned when operations require initialization
- Mock Authentication:
env.mock_all_auths()for testing authorization - Address Generation:
Address::generate(&env)for unique test addresses - Error Verification:
#[should_panic(expected = "...")]for error testing - Try Methods:
try_initialize()for non-panicking error handling - State Verification: Direct state retrieval and assertion
initialize(env: Env, admin: Address) -> Result<(), AgentError>get_state(env: Env) -> Option<ContractState>
initialize(env: Env, admin: Address) -> Result<(), PropertyError>get_state(env: Env) -> Option<ContractState>
- 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
- 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
- #646: Escrow Lifecycle & Dispute Resolution
- #647: Payment Processing & Late Fee Calculation
- #648: Rent Obligation NFT & Agent Registration