Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subkeys 2 #28

Merged
merged 9 commits into from
Aug 12, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn query_allowance<S: Storage, A: Api, Q: Querier>(
mod tests {
use super::*;
use crate::balance::Balance;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::testing::{mock_dependencies, mock_env, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins};
use cw1_whitelist::msg::ConfigResponse;

Expand Down Expand Up @@ -646,4 +646,72 @@ mod tests {
}
);
}

#[test]
fn execute_checks() {
let mut deps = mock_dependencies(20, &coins(1111, "token1"));

let owner = HumanAddr::from("admin0001");
let admins = vec![owner.clone(), HumanAddr::from("admin0002")];

let spender1 = HumanAddr::from("spender0001");
let spender2 = HumanAddr::from("spender0002");
let initial_spenders = vec![spender1.clone()];

let denom1 = "token1";
let amount1 = 1111;
let allow1 = coin(amount1, denom1);
let initial_allowances = vec![allow1];

let expires_never = Expiration::Never {};
let initial_expirations = vec![expires_never.clone()];

let env = mock_env(owner.clone(), &[]);
setup_test_case(
&mut deps,
&env,
&admins,
&initial_spenders,
&initial_allowances,
&initial_expirations,
);

// Create Send message
let msgs = vec![
BankMsg::Send {
from_address: HumanAddr::from(MOCK_CONTRACT_ADDR),
to_address: spender2.clone(),
amount: coins(1000, "token1"),
}.into(),
];

let handle_msg = HandleMsg::Execute { msgs: msgs.clone() };

// spender2 cannot spend funds (no initial allowance)
let env = mock_env(&spender2, &[]);
let res = handle(&mut deps, env, handle_msg.clone());
match res.unwrap_err() {
StdError::NotFound { .. } => {},
e => panic!("unexpected error: {}", e),
}

// But spender1 can (he has enough funds)
let env = mock_env(&spender1, &[]);
let res = handle(&mut deps, env.clone(), handle_msg.clone()).unwrap();
assert_eq!(res.messages, msgs);
assert_eq!(res.log, vec![log("action", "execute"), log("owner", spender1)]);

// And then cannot (not enough funds anymore)
let res = handle(&mut deps, env, handle_msg.clone());
match res.unwrap_err() {
StdError::Underflow { .. } => {},
e => panic!("unexpected error: {}", e),
}

// Owner / admins can do anything (at the contract level)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good test case.

Can you add another where the spender (with allowance) tries to send a message besides BankMsg::Send. Anything else to show it fails.

Just one more test and this is 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

let env = mock_env(&owner.clone(), &[]);
let res = handle(&mut deps, env.clone(), handle_msg.clone()).unwrap();
assert_eq!(res.messages, msgs);
assert_eq!(res.log, vec![log("action", "execute"), log("owner", owner)]);
}
}