|
| 1 | +use super::*; |
| 2 | +use soroban_sdk::{testutils::{Address as _, Ledger as _, LedgerInfo}, symbol_short, Address, Env, Symbol, String}; |
| 3 | +use crate::domain_validator::validate_anchor_domain; |
| 4 | +use crate::errors::{AnchorKitError, ErrorCode}; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_set_get_endpoint_happy_path() { |
| 8 | + let env = Env::default(); |
| 9 | + env.mock_all_auths(); |
| 10 | + |
| 11 | + let attestor = Address::random(&env); |
| 12 | + let endpoint = String::from_str(&env, "https://example.com/api"); |
| 13 | + |
| 14 | + // Register attestor (admin auth mocked) |
| 15 | + AnchorKitContract::register_attestor(&env, attestor.clone(), String::from_str(&env, "mock_token"), Address::random(&env)); |
| 16 | + |
| 17 | + // Set endpoint |
| 18 | + AnchorKitContract::set_endpoint(&env, attestor.clone(), endpoint.clone()); |
| 19 | + |
| 20 | + // Get endpoint |
| 21 | + let retrieved = AnchorKitContract::get_endpoint(&env, attestor.clone()); |
| 22 | + assert_eq!(retrieved, endpoint); |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +#[should_panic(expected = "AttestorNotRegistered")] |
| 27 | +fn test_get_endpoint_not_registered() { |
| 28 | + let env = Env::default(); |
| 29 | + env.mock_all_auths(); |
| 30 | + |
| 31 | + let attestor = Address::random(&env); |
| 32 | + AnchorKitContract::get_endpoint(&env, attestor); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +#[should_panic(expected = "AttestorNotRegistered")] |
| 37 | +fn test_set_endpoint_not_attestor() { |
| 38 | + let env = Env::default(); |
| 39 | + env.mock_all_auths(); |
| 40 | + |
| 41 | + let attestor = Address::random(&env); |
| 42 | + let endpoint = String::from_str(&env, "https://example.com"); |
| 43 | + AnchorKitContract::set_endpoint(&env, attestor, endpoint); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +#[should_panic(expected = "InvalidEndpointFormat")] |
| 48 | +fn test_set_endpoint_invalid_url() { |
| 49 | + let env = Env::default(); |
| 50 | + env.mock_all_auths(); |
| 51 | + |
| 52 | + let attestor = Address::random(&env); |
| 53 | + AnchorKitContract::register_attestor(&env, attestor.clone(), String::from_str(&env, "mock"), Address::random(&env)); |
| 54 | + |
| 55 | + let invalid = String::from_str(&env, "http://invalid.com"); // HTTP |
| 56 | + AnchorKitContract::set_endpoint(&env, attestor, invalid); |
| 57 | +} |
| 58 | + |
| 59 | +#[test] |
| 60 | +#[should_panic(expected = "Unauthorized")] |
| 61 | +fn test_set_endpoint_unauthorized() { |
| 62 | + let env = Env::default(); |
| 63 | + |
| 64 | + let attestor = Address::random(&env); |
| 65 | + AnchorKitContract::register_attestor(&env, attestor.clone(), String::from_str(&env, "mock"), Address::random(&env)); |
| 66 | + |
| 67 | + let endpoint = String::from_str(&env, "https://example.com"); |
| 68 | + let caller = Address::random(&env); |
| 69 | + caller.require_auth(); // Mock auth for wrong caller |
| 70 | + |
| 71 | + // Function requires attestor.require_auth(), so wrong caller panics on auth |
| 72 | + // Test assumes env.mock_all_auths() not called |
| 73 | + env.budget().reset_unlimited(); |
| 74 | + // Note: testutils mock_all_auths needed for require_auth in tests |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn test_endpoint_updated_event() { |
| 79 | + let env = Env::default(); |
| 80 | + env.mock_all_auths(); |
| 81 | + |
| 82 | + let attestor = Address::random(&env); |
| 83 | + let endpoint = String::from_str(&env, "https://test.com"); |
| 84 | + |
| 85 | + AnchorKitContract::register_attestor(&env, attestor.clone(), String::from_str(&env, "token"), Address::random(&env)); |
| 86 | + |
| 87 | + // Expect event |
| 88 | + let topics = (symbol_short!("endpoint"), symbol_short!("updated")); |
| 89 | + env.events().publish_expect(&topics, &EndpointUpdated { attestor: attestor.clone(), endpoint: endpoint.clone() }); |
| 90 | + |
| 91 | + // Calling set_endpoint should emit it |
| 92 | + AnchorKitContract::set_endpoint(&env, attestor, endpoint.clone()); |
| 93 | + // Verify emitted (testutils check) |
| 94 | +} |
| 95 | + |
0 commit comments