From 3d9a68e145cc80ab6a74cc6f09b75dcbac06b970 Mon Sep 17 00:00:00 2001 From: Buckram Date: Wed, 21 Dec 2022 17:40:57 +0200 Subject: [PATCH] tests --- checksum | 2 +- contracts/cw-croncat/src/tasks.rs | 4 +-- contracts/cw-croncat/src/tests/manager.rs | 35 +++++++++++++++++++++ packages/cw-croncat-core/src/tests/types.rs | 25 +++++++++++++++ packages/cw-croncat-core/src/types.rs | 3 ++ 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/checksum b/checksum index 7edbe48d..97137749 100644 --- a/checksum +++ b/checksum @@ -1,2 +1,2 @@ -7e1411d78e92ac4ab2dbda9208b62f0b4cfffc926cfc61de10498fe4e40b7afd cw_croncat.wasm +853004395db1861f1f337925a76e31b9f8ed5fe158b807c647240ea71876e875 cw_croncat.wasm de2d1a0c648e41760020dd261f818da085c358240059acf85128f60eb0e05db2 cw_rules.wasm diff --git a/contracts/cw-croncat/src/tasks.rs b/contracts/cw-croncat/src/tasks.rs index 3b1b957d..9d2769ae 100644 --- a/contracts/cw-croncat/src/tasks.rs +++ b/contracts/cw-croncat/src/tasks.rs @@ -254,9 +254,7 @@ impl<'a> CwCroncat<'a> { val: "Interval invalid".to_string(), }); } - if task.actions.is_empty() { - return Err(ContractError::CoreError(CoreError::InvalidAction {})); - } + let (mut amount_for_one_task, gas_amount) = task.is_valid_msg_calculate_usage( deps.api, &env.contract.address, diff --git a/contracts/cw-croncat/src/tests/manager.rs b/contracts/cw-croncat/src/tests/manager.rs index 0a669587..f3d64ba9 100644 --- a/contracts/cw-croncat/src/tests/manager.rs +++ b/contracts/cw-croncat/src/tests/manager.rs @@ -8,6 +8,7 @@ use cosmwasm_std::{ coin, coins, to_binary, Addr, BankMsg, Coin, CosmosMsg, StakingMsg, StdResult, Uint128, WasmMsg, }; use cw20::Cw20Coin; +use cw_croncat_core::error::CoreError; use cw_croncat_core::msg::{ AgentResponse, AgentTaskResponse, ExecuteMsg, GetAgentIdsResponse, QueryMsg, TaskRequest, TaskResponse, TaskWithQueriesResponse, @@ -3026,3 +3027,37 @@ fn test_error_in_reply() { } assert!(without_failure); } + +#[test] +fn empty_actions_not_allowed() { + let (mut app, cw_template_contract, _) = proper_instantiate(); + let contract_addr = cw_template_contract.addr(); + + let empty_actions = ExecuteMsg::CreateTask { + task: TaskRequest { + interval: Interval::Once, + boundary: None, + stop_on_fail: false, + actions: vec![], + queries: None, + transforms: None, + cw20_coins: vec![], + }, + }; + + let total_gas = GAS_BASE_FEE_JUNO + GAS_ACTION_FEE_JUNO; + let attach_per_action = (total_gas + (total_gas * 5 / 100)) / GAS_DENOMINATOR_DEFAULT_JUNO; + let amount_for_three = (attach_per_action) as u128; + + let res: ContractError = app + .execute_contract( + Addr::unchecked(ADMIN), + contract_addr.clone(), + &empty_actions, + &coins(amount_for_three, NATIVE_DENOM), + ) + .unwrap_err() + .downcast() + .unwrap(); + assert_eq!(res, ContractError::CoreError(CoreError::InvalidAction {})); +} diff --git a/packages/cw-croncat-core/src/tests/types.rs b/packages/cw-croncat-core/src/tests/types.rs index 3bfaa88f..84db19db 100644 --- a/packages/cw-croncat-core/src/tests/types.rs +++ b/packages/cw-croncat-core/src/tests/types.rs @@ -316,6 +316,31 @@ fn is_valid_msg_send_should_success() { .is_ok()); } +#[test] +fn is_valid_empty_actions() { + let task = TaskRequest { + interval: Interval::Block(10), + boundary: None, + stop_on_fail: false, + actions: vec![], + queries: None, + transforms: None, + cw20_coins: Default::default(), + }; + assert_eq!( + task.is_valid_msg_calculate_usage( + &mock_dependencies().api, + &Addr::unchecked("alice2"), + &Addr::unchecked("bob"), + &Addr::unchecked("bob"), + 5, + 5, + ) + .unwrap_err(), + CoreError::InvalidAction {} + ); +} + #[test] fn test_add_tokens() { let mut coins: GenericBalance = GenericBalance::default(); diff --git a/packages/cw-croncat-core/src/types.rs b/packages/cw-croncat-core/src/types.rs index 25028c0d..790af8f9 100644 --- a/packages/cw-croncat-core/src/types.rs +++ b/packages/cw-croncat-core/src/types.rs @@ -239,6 +239,9 @@ impl TaskRequest { let mut gas_amount: u64 = base_gas; let mut amount_for_one_task = GenericBalance::default(); + if self.actions.is_empty() { + return Err(CoreError::InvalidAction {}); + } for action in self.actions.iter() { // checked for cases, where task creator intentionaly tries to overflow gas_amount = gas_amount