Skip to content

Commit

Permalink
Merge pull request #373 from CosmWasm/341-returned-items-validity
Browse files Browse the repository at this point in the history
Responses validation in multi-test
  • Loading branch information
ethanfrey authored Aug 3, 2021
2 parents 0d07c27 + 4fbb283 commit 9c17aa9
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 25 deletions.
169 changes: 162 additions & 7 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ where
mod test {
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{
attr, coin, coins, AllBalanceResponse, BankMsg, BankQuery, Event, Reply, SubMsg, WasmMsg,
attr, coin, coins, to_binary, AllBalanceResponse, Attribute, BankMsg, BankQuery, Event,
Reply, SubMsg, WasmMsg,
};

use crate::test_helpers::contracts::{echo, hackatom, payout, reflect};
Expand Down Expand Up @@ -925,10 +926,6 @@ mod test {
}

mod replay_data_overwrite {
use cosmwasm_std::to_binary;

use crate::test_helpers::EmptyMsg;

use super::*;

fn make_echo_submsg(
Expand All @@ -939,7 +936,12 @@ mod test {
let data = data.into().map(|s| s.to_owned());
SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract.into(),
msg: to_binary(&echo::Message { data, sub_msg }).unwrap(),
msg: to_binary(&echo::Message {
data,
sub_msg,
..echo::Message::default()
})
.unwrap(),
funds: vec![],
}))
}
Expand All @@ -961,7 +963,7 @@ mod test {
contract,
&echo::Message {
data: Some("Data".to_owned()),
sub_msg: vec![],
..echo::Message::default()
},
&[],
)
Expand All @@ -988,6 +990,7 @@ mod test {
&echo::Message {
data: Some("First".to_owned()),
sub_msg: vec![make_echo_submsg(contract, "Second", vec![])],
..echo::Message::default()
},
&[],
)
Expand All @@ -1014,6 +1017,7 @@ mod test {
&echo::Message {
data: Some("First".to_owned()),
sub_msg: vec![make_echo_submsg(contract, None, vec![])],
..echo::Message::default()
},
&[],
)
Expand Down Expand Up @@ -1045,6 +1049,7 @@ mod test {
make_echo_submsg(contract.clone(), "Second", vec![]),
make_echo_submsg(contract, None, vec![]),
],
..echo::Message::default()
},
&[],
)
Expand Down Expand Up @@ -1083,6 +1088,7 @@ mod test {
)],
)],
)],
..echo::Message::default()
},
&[],
)
Expand All @@ -1091,4 +1097,153 @@ mod test {
assert_eq!(response.data, Some("Second".as_bytes().into()));
}
}

mod response_validation {
use super::*;

#[test]
fn empty_attribute_key() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");

let contract_id = app.store_code(echo::contract());
let contract = app
.instantiate_contract(contract_id, owner.clone(), &EmptyMsg {}, &[], "Echo", None)
.unwrap();

let err = app
.execute_contract(
owner,
contract,
&echo::Message {
data: None,
attributes: vec![
Attribute::new(" ", "value"),
Attribute::new("proper", "proper_val"),
],
..echo::Message::default()
},
&[],
)
.unwrap_err();

assert_eq!(err, "Empty attribute key. Value: value");
}

#[test]
fn empty_attribute_value() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");

let contract_id = app.store_code(echo::contract());
let contract = app
.instantiate_contract(contract_id, owner.clone(), &EmptyMsg {}, &[], "Echo", None)
.unwrap();

let err = app
.execute_contract(
owner,
contract,
&echo::Message {
data: None,
attributes: vec![
Attribute::new("key", " "),
Attribute::new("proper", "proper_val"),
],
..echo::Message::default()
},
&[],
)
.unwrap_err();

assert_eq!(err, "Empty attribute value. Key: key");
}

#[test]
fn empty_event_attribute_key() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");

let contract_id = app.store_code(echo::contract());
let contract = app
.instantiate_contract(contract_id, owner.clone(), &EmptyMsg {}, &[], "Echo", None)
.unwrap();

let err = app
.execute_contract(
owner,
contract,
&echo::Message {
data: None,
events: vec![Event::new("event")
.add_attribute(" ", "value")
.add_attribute("proper", "proper_val")],
..echo::Message::default()
},
&[],
)
.unwrap_err();

assert_eq!(err, "Empty attribute key. Value: value");
}

#[test]
fn empty_event_attribute_value() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");

let contract_id = app.store_code(echo::contract());
let contract = app
.instantiate_contract(contract_id, owner.clone(), &EmptyMsg {}, &[], "Echo", None)
.unwrap();

let err = app
.execute_contract(
owner,
contract,
&echo::Message {
data: None,
events: vec![Event::new("event")
.add_attribute("key", " ")
.add_attribute("proper", "proper_val")],
..echo::Message::default()
},
&[],
)
.unwrap_err();

assert_eq!(err, "Empty attribute value. Key: key");
}

#[test]
fn too_short_event_type() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");

let contract_id = app.store_code(echo::contract());
let contract = app
.instantiate_contract(contract_id, owner.clone(), &EmptyMsg {}, &[], "Echo", None)
.unwrap();

let err = app
.execute_contract(
owner,
contract,
&echo::Message {
data: None,
events: vec![Event::new(" e "), Event::new("event")],
..echo::Message::default()
},
&[],
)
.unwrap_err();

assert_eq!(err, "Event type too short: e");
}
}
}
17 changes: 13 additions & 4 deletions packages/multi-test/src/test_helpers/contracts/echo.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Very simple echoing contract which just returns incomming string if any, but performming subcall of
//! given message to test response
//! given message to test response.
//!
//! Additionally it bypass all events and attributes send to it
use cosmwasm_std::{
to_binary, Binary, ContractResult, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response,
StdError, SubMsg, SubMsgExecutionResponse,
to_binary, Attribute, Binary, ContractResult, Deps, DepsMut, Empty, Env, Event, MessageInfo,
Reply, Response, StdError, SubMsg, SubMsgExecutionResponse,
};
use serde::{Deserialize, Serialize};

Expand All @@ -13,6 +15,8 @@ use crate::{test_helpers::EmptyMsg, Contract, ContractWrapper};
pub struct Message {
pub data: Option<String>,
pub sub_msg: Vec<SubMsg>,
pub attributes: Vec<Attribute>,
pub events: Vec<Event>,
}

#[allow(clippy::unnecessary_wraps)]
Expand All @@ -33,10 +37,15 @@ fn execute(
msg: Message,
) -> Result<Response, StdError> {
let mut resp = Response::new();

if let Some(data) = msg.data {
resp = resp.set_data(data.into_bytes());
}
Ok(resp.add_submessages(msg.sub_msg))

Ok(resp
.add_submessages(msg.sub_msg)
.add_attributes(msg.attributes)
.add_events(msg.events))
}

fn query(_deps: Deps, _env: Env, msg: EmptyMsg) -> Result<Binary, StdError> {
Expand Down
Loading

0 comments on commit 9c17aa9

Please sign in to comment.