From d09db0198135d7aa1abcdf574f375ab5a00f445d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 28 Jul 2020 20:55:22 +0200 Subject: [PATCH] Clean up testcode, leave stubs for subkeys --- contracts/cw1-subkeys/src/balance.rs | 3 +- contracts/cw1-subkeys/src/contract.rs | 148 +++++--------------------- 2 files changed, 28 insertions(+), 123 deletions(-) diff --git a/contracts/cw1-subkeys/src/balance.rs b/contracts/cw1-subkeys/src/balance.rs index 67235d07e..172a16194 100644 --- a/contracts/cw1-subkeys/src/balance.rs +++ b/contracts/cw1-subkeys/src/balance.rs @@ -129,8 +129,7 @@ impl ops::Sub for Balance { #[cfg(test)] mod test { use super::*; - use cosmwasm_std::{from_slice, to_vec}; - use std::convert::TryInto; + use cosmwasm_std::coin; #[test] fn balance_has_works() { diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 4b3232c0c..405b7ff17 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -146,132 +146,38 @@ mod tests { const CANONICAL_LENGTH: usize = 20; - #[test] - fn init_and_modify_config() { - let mut deps = mock_dependencies(CANONICAL_LENGTH, &[]); - - let alice = HumanAddr::from("alice"); - let bob = HumanAddr::from("bob"); - let carl = HumanAddr::from("carl"); - - let anyone = HumanAddr::from("anyone"); - - // init the contract - let init_msg = InitMsg { - admins: vec![alice.clone(), bob.clone(), carl.clone()], - mutable: true, - }; - let env = mock_env(&deps.api, &anyone, &[]); - init(&mut deps, env, init_msg).unwrap(); - - // ensure expected config - let expected = ConfigResponse { - admins: vec![alice.clone(), bob.clone(), carl.clone()], - mutable: true, - }; - assert_eq!(query_config(&deps).unwrap(), expected); - - // anyone cannot modify the contract - let msg = HandleMsg::UpdateAdmins { - admins: vec![anyone.clone()], - }; - let env = mock_env(&deps.api, &anyone, &[]); - let res = handle(&mut deps, env, msg); - match res.unwrap_err() { - StdError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + // you probably want some `setup_test_case` function that inits a contract with 2 admins + // and 2 subkeys with some allowances. these keys can be constants strings here, + // used like `HumanAddr::from(admin1)` and then all tests can just run against that - // but alice can kick out carl - let msg = HandleMsg::UpdateAdmins { - admins: vec![alice.clone(), bob.clone()], - }; - let env = mock_env(&deps.api, &alice, &[]); - handle(&mut deps, env, msg).unwrap(); - - // ensure expected config - let expected = ConfigResponse { - admins: vec![alice.clone(), bob.clone()], - mutable: true, - }; - assert_eq!(query_config(&deps).unwrap(), expected); - - // carl cannot freeze it - let env = mock_env(&deps.api, &carl, &[]); - let res = handle(&mut deps, env, HandleMsg::Freeze {}); - match res.unwrap_err() { - StdError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } - - // but bob can - let env = mock_env(&deps.api, &bob, &[]); - handle(&mut deps, env, HandleMsg::Freeze {}).unwrap(); - let expected = ConfigResponse { - admins: vec![alice.clone(), bob.clone()], - mutable: false, - }; - assert_eq!(query_config(&deps).unwrap(), expected); - - // and now alice cannot change it again - let msg = HandleMsg::UpdateAdmins { - admins: vec![alice.clone()], - }; - let env = mock_env(&deps.api, &alice, &[]); - let res = handle(&mut deps, env, msg); - match res.unwrap_err() { - StdError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + #[test] + fn query_allowances() { + // TODO + // check the allowances work for accounts with balances and accounts with none } #[test] - fn execute_messages_has_proper_permissions() { - let mut deps = mock_dependencies(CANONICAL_LENGTH, &[]); - - let alice = HumanAddr::from("alice"); - let bob = HumanAddr::from("bob"); - let carl = HumanAddr::from("carl"); - - // init the contract - let init_msg = InitMsg { - admins: vec![alice.clone(), carl.clone()], - mutable: false, - }; - let env = mock_env(&deps.api, &bob, &[]); - init(&mut deps, env, init_msg).unwrap(); - - let freeze: HandleMsg = HandleMsg::Freeze {}; - let msgs = vec![ - BankMsg::Send { - from_address: HumanAddr::from(MOCK_CONTRACT_ADDR), - to_address: bob.clone(), - amount: coins(10000, "DAI"), - } - .into(), - WasmMsg::Execute { - contract_addr: HumanAddr::from("some contract"), - msg: to_binary(&freeze).unwrap(), - send: vec![], - } - .into(), - ]; - - // make some nice message - let handle_msg = HandleMsg::Execute { msgs: msgs.clone() }; + fn update_admins_and_query() { + // TODO + // insure imported logic is wired up properly + } - // bob cannot execute them - let env = mock_env(&deps.api, &bob, &[]); - let res = handle(&mut deps, env, handle_msg.clone()); - match res.unwrap_err() { - StdError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + #[test] + fn increase_allowances() { + // TODO + // add to existing account (expires = None) => don't change Expiration (previous should be different than Never) + // add to existing account (expires = Some) + // add to new account (expires = None) => default Expiration::Never + // add to new account (expires = Some) + } - // but carl can - let env = mock_env(&deps.api, &carl, &[]); - let res = handle(&mut deps, env, handle_msg.clone()).unwrap(); - assert_eq!(res.messages, msgs); - assert_eq!(res.log, vec![log("action", "execute")]); + #[test] + fn decrease_allowances() { + // TODO + // subtract to existing account (has none of that denom) + // subtract to existing account (brings denom to 0, other denoms left) + // subtract to existing account (brings denom to > 0) + // subtract to existing account (brings denom to 0, no other denoms left => should delete Allowance) + // subtract from empty account (should error) } }