From 143987dad8d06c1347b22ff092bf5498fd7d5d39 Mon Sep 17 00:00:00 2001 From: sagar sapkota Date: Mon, 4 Nov 2024 12:48:44 +0545 Subject: [PATCH] fix: reset limit admin check chore: add test for reset limit --- contracts/asset_manager/src/contract.rs | 2 ++ .../src/tests/asset_manager_test.rs | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/contracts/asset_manager/src/contract.rs b/contracts/asset_manager/src/contract.rs index 0d35306..959f5e8 100644 --- a/contracts/asset_manager/src/contract.rs +++ b/contracts/asset_manager/src/contract.rs @@ -109,6 +109,8 @@ impl AssetManager { } pub fn reset_limit(env: Env, token: Address) { + let admin = read_administrator(&env); + admin.require_auth(); let balance = Self::get_token_balance(&env, token.clone()); let mut data: TokenData = read_token_data(&env, token.clone()); data.current_limit = (balance * data.percentage as u128 / POINTS) as u64; diff --git a/contracts/asset_manager/src/tests/asset_manager_test.rs b/contracts/asset_manager/src/tests/asset_manager_test.rs index 09356c4..b25f231 100644 --- a/contracts/asset_manager/src/tests/asset_manager_test.rs +++ b/contracts/asset_manager/src/tests/asset_manager_test.rs @@ -29,7 +29,7 @@ fn test_set_admin() { let new_admin: Address = Address::generate(&ctx.env); client.set_admin(&new_admin); - + assert_eq!( ctx.env.auths(), std::vec![( @@ -56,7 +56,6 @@ fn test_configure_rate_limit_panic() { let period = &300; let percentage = &10001; client.configure_rate_limit(&ctx.token, period, percentage); - } #[test] @@ -83,7 +82,6 @@ fn test_configure_rate_limit() { ); let token_data = client.get_rate_limit(&ctx.token); assert_eq!(token_data.3, 0); - } #[test] @@ -482,6 +480,31 @@ fn test_extend_ttl() { }); } +#[test] +fn test_reset_limit() { + let ctx = TestContext::default(); + let client = AssetManagerClient::new(&ctx.env, &ctx.registry); + ctx.init_context(&client); + + client.configure_rate_limit(&ctx.token, &300, &300); + client.reset_limit(&ctx.token); + + assert_eq!( + ctx.env.auths(), + std::vec![( + ctx.admin.clone(), + AuthorizedInvocation { + function: AuthorizedFunction::Contract(( + ctx.registry.clone(), + Symbol::new(&ctx.env, "reset_limit"), + (&ctx.token,).into_val(&ctx.env) + )), + sub_invocations: std::vec![] + } + )] + ); +} + #[test] fn test_set_upgrade_authority() { let ctx = TestContext::default();