So i have a rust function which accepts new arguments and want to run a small unit test for the function but facing challenges

#[update(guard = "caller_is_signer")]
async fn account_balance(account_id: AccountId, chain: ChainEnum) -> Balance {
    log_cycle!(
        "Get balance for account: {} on chain: {:?}",
        account_id,
        chain
    );

    // Retrieve the ledger for the account or panic if not found
    let ledger = with_ledger(&account_id, |ledger| ledger.clone()).unwrap_or_else(panic_log);

    // Retrieve the balance for the account on the specified chain
    let balance = ledger.balance(chain).await;

    // Handle errors and panic if balance retrieval fails
    match balance {
        Ok(balance) => balance,
        Err(err) => panic_log(err),
    }
}

What does this mean? Googling around, is this related to 'Internet Computer/crypto', or is it some other attribute ?

there is a guard.rs function and is related to Intenernet computer

use operations::{role::AccessLevel, store::with_verified_user};

pub fn caller_is_canister_or_admin() -> Result<(), String> {
    let caller = ic_cdk::caller();

    with_verified_user(caller.into(), |signer| {
        signer.have_access_level(&AccessLevel::FullAccess)
            || signer.have_access_level(&AccessLevel::Canister)
    })
}

pub fn caller_is_admin() -> Result<(), String> {
    let caller = ic_cdk::caller();

    with_verified_user(caller.into(), |signer| signer.is_admin())
}

pub fn _caller_is_user() -> Result<(), String> {
    let caller = ic_cdk::caller();

    with_verified_user(caller.into(), |signer| signer.is_user())
}

pub fn caller_is_signer() -> Result<(), String> {
    let caller = ic_cdk::caller();

    with_verified_user(caller.into(), |_| true)
}


What does your unit test look like? What is the error message from the compiler? What is guard.rs, do you have a link?

1 Like

guard.rs is checking requests for example a logged in user can call a certain function an example of the test

 fn test_account_balance() {
        // Set up test data
        let account_id = AccountId::new([0u8; 32]);
        let chain = ChainEnum::default();

        // Call the function being tested
        let balance = account_balance(account_id, chain).await;

        // Check the result
        assert_eq!(balance, 0);
    }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.