|
| 1 | +#![cfg(test)] |
| 2 | + |
| 3 | +use soroban_sdk::{testutils::Address as _, Address, BytesN, Env, String}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + base::errors::{CrowdfundingError, SecondCrowdfundingError}, |
| 7 | + crowdfunding::{CrowdfundingContract, CrowdfundingContractClient}, |
| 8 | + interfaces::second_crowdfunding::SecondCrowdfundingTrait, |
| 9 | +}; |
| 10 | + |
| 11 | +fn setup(env: &Env) -> (CrowdfundingContractClient<'_>, Address) { |
| 12 | + env.mock_all_auths(); |
| 13 | + let contract_id = env.register_contract(None, CrowdfundingContract); |
| 14 | + let client = CrowdfundingContractClient::new(env, &contract_id); |
| 15 | + |
| 16 | + let admin = Address::generate(env); |
| 17 | + let token = Address::generate(env); |
| 18 | + client.initialize(&admin, &token, &0); |
| 19 | + |
| 20 | + (client, token) |
| 21 | +} |
| 22 | + |
| 23 | +fn string_of_len(env: &Env, len: usize) -> String { |
| 24 | + String::from_str(env, &"a".repeat(len)) |
| 25 | +} |
| 26 | + |
| 27 | +// ── create_campaign ──────────────────────────────────────────────────────────── |
| 28 | + |
| 29 | +#[test] |
| 30 | +fn test_campaign_title_within_limit_succeeds() { |
| 31 | + let env = Env::default(); |
| 32 | + let (client, token) = setup(&env); |
| 33 | + |
| 34 | + let creator = Address::generate(&env); |
| 35 | + let campaign_id = BytesN::from_array(&env, &[1u8; 32]); |
| 36 | + let title = string_of_len(&env, 200); // exactly at the limit |
| 37 | + let deadline = env.ledger().timestamp() + 86_400; |
| 38 | + |
| 39 | + // Via Soroban client: string validation maps to CrowdfundingError::InvalidTitle |
| 40 | + let result = |
| 41 | + client.try_create_campaign(&campaign_id, &title, &creator, &1000, &deadline, &token); |
| 42 | + assert!(result.is_ok(), "title of 200 chars should be accepted"); |
| 43 | + |
| 44 | + // Via SecondCrowdfundingTrait directly: validates string length only |
| 45 | + let trait_result = <CrowdfundingContract as SecondCrowdfundingTrait>::create_campaign_checked( |
| 46 | + env.clone(), |
| 47 | + campaign_id, |
| 48 | + title, |
| 49 | + creator, |
| 50 | + 1000, |
| 51 | + deadline, |
| 52 | + token, |
| 53 | + ); |
| 54 | + assert!( |
| 55 | + trait_result.is_ok(), |
| 56 | + "SecondCrowdfundingTrait: title of 200 chars should be accepted" |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn test_campaign_title_exceeds_limit_returns_error() { |
| 62 | + let env = Env::default(); |
| 63 | + let (client, token) = setup(&env); |
| 64 | + |
| 65 | + let creator = Address::generate(&env); |
| 66 | + let campaign_id = BytesN::from_array(&env, &[2u8; 32]); |
| 67 | + let title = string_of_len(&env, 201); // one over the limit |
| 68 | + let deadline = env.ledger().timestamp() + 86_400; |
| 69 | + |
| 70 | + // Via Soroban client: SecondCrowdfundingError is mapped to CrowdfundingError::InvalidTitle |
| 71 | + let client_result = |
| 72 | + client.try_create_campaign(&campaign_id, &title, &creator, &1000, &deadline, &token); |
| 73 | + assert_eq!( |
| 74 | + client_result, |
| 75 | + Err(Ok(CrowdfundingError::InvalidTitle)), |
| 76 | + "title of 201 chars should return CrowdfundingError::InvalidTitle via client" |
| 77 | + ); |
| 78 | + |
| 79 | + // Via SecondCrowdfundingTrait directly: returns StringTooLong without remapping |
| 80 | + let trait_result = <CrowdfundingContract as SecondCrowdfundingTrait>::create_campaign_checked( |
| 81 | + env.clone(), |
| 82 | + campaign_id, |
| 83 | + title, |
| 84 | + creator, |
| 85 | + 1000, |
| 86 | + deadline, |
| 87 | + token, |
| 88 | + ); |
| 89 | + assert_eq!( |
| 90 | + trait_result, |
| 91 | + Err(SecondCrowdfundingError::StringTooLong), |
| 92 | + "title of 201 chars should return StringTooLong via SecondCrowdfundingTrait" |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn test_campaign_title_much_longer_than_limit_returns_error() { |
| 98 | + let env = Env::default(); |
| 99 | + let (client, token) = setup(&env); |
| 100 | + |
| 101 | + let creator = Address::generate(&env); |
| 102 | + let campaign_id = BytesN::from_array(&env, &[3u8; 32]); |
| 103 | + let title = string_of_len(&env, 500); // well over the limit |
| 104 | + let deadline = env.ledger().timestamp() + 86_400; |
| 105 | + |
| 106 | + // Via Soroban client: SecondCrowdfundingError is mapped to CrowdfundingError::InvalidTitle |
| 107 | + let client_result = |
| 108 | + client.try_create_campaign(&campaign_id, &title, &creator, &1000, &deadline, &token); |
| 109 | + assert_eq!( |
| 110 | + client_result, |
| 111 | + Err(Ok(CrowdfundingError::InvalidTitle)), |
| 112 | + "title of 500 chars should return CrowdfundingError::InvalidTitle via client" |
| 113 | + ); |
| 114 | + |
| 115 | + // Via SecondCrowdfundingTrait directly: returns StringTooLong without remapping |
| 116 | + let trait_result = <CrowdfundingContract as SecondCrowdfundingTrait>::create_campaign_checked( |
| 117 | + env.clone(), |
| 118 | + campaign_id, |
| 119 | + title, |
| 120 | + creator, |
| 121 | + 1000, |
| 122 | + deadline, |
| 123 | + token, |
| 124 | + ); |
| 125 | + assert_eq!( |
| 126 | + trait_result, |
| 127 | + Err(SecondCrowdfundingError::StringTooLong), |
| 128 | + "title of 500 chars should return StringTooLong via SecondCrowdfundingTrait" |
| 129 | + ); |
| 130 | +} |
0 commit comments