Skip to content

Commit

Permalink
feat: add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Feb 22, 2024
1 parent 182af7d commit 6f2eb7a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 7 deletions.
5 changes: 0 additions & 5 deletions contracts/airdrop-token-vesting/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ pub struct Whitelist {
}

impl Whitelist {
pub fn has(&self, addr: impl AsRef<str>) -> bool {
let addr = addr.as_ref();
self.members.contains(addr) || self.admin == addr
}

pub fn is_admin(&self, addr: impl AsRef<str>) -> bool {
let addr = addr.as_ref();
self.admin == addr
Expand Down
132 changes: 130 additions & 2 deletions contracts/airdrop-token-vesting/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn invalid_coin_sent_instantiation() -> TestResult {
deps.as_mut(),
mock_env(),
mock_info("addr0000", &[coin(1000, "nibi"), coin(1000, "usd")]),
msg,
msg.clone(),
);
match res {
Err(err) => {
Expand All @@ -78,6 +78,25 @@ fn invalid_coin_sent_instantiation() -> TestResult {
Ok(_) => panic!("Expected error but got success: {res:?}"),
}

// 0 amount coins sent
let res = instantiate(
deps.as_mut(),
mock_env(),
mock_info("addr0000", &[coin(0, "nibi")]),
msg,
);
match res {
Err(err) => {
assert_eq!(
err,
StdError::GenericErr {
msg: "must deposit some token".to_string(),
}
)
}
Ok(_) => panic!("Expected error but got success: {res:?}"),
}

Ok(())
}

Expand Down Expand Up @@ -215,7 +234,7 @@ fn register_cliff_vesting_account_with_native_token() -> TestResult {
let _res = instantiate(
deps.as_mut(),
mock_env(),
mock_info("addr0000", &[coin(1000, "uusd")]),
mock_info("addr0000", &[coin(2000, "uusd")]),
InstantiateMsg {
admin: "addr0000".to_string(),
managers: vec!["admin-sender".to_string()],
Expand Down Expand Up @@ -248,6 +267,16 @@ fn register_cliff_vesting_account_with_native_token() -> TestResult {
}
};

// unauthorized sender
let msg = create_msg(100, 110, 0, Some(1000), 105);
require_error(
&mut deps,
&env,
mock_info("addr0042", &[]),
msg,
StdError::generic_err("Unauthorized").into(),
);

// zero amount vesting token
let msg = create_msg(100, 110, 0, Some(1000), 105);
require_error(
Expand Down Expand Up @@ -334,6 +363,105 @@ fn register_cliff_vesting_account_with_native_token() -> TestResult {
StdError::generic_err("Insufficient funds for all rewards").into(),
);

// valid amount
let (vesting_amount, cliff_amount, cliff_time) = (1000, 250, 105);
let msg =
create_msg(100, 110, vesting_amount, Some(cliff_amount), cliff_time);

let res =
execute(deps.as_mut(), env.clone(), mock_info("addr0000", &[]), msg)?;

assert_eq!(
res.attributes,
vec![
Attribute {
key: "action".to_string(),
value: "register_vesting_account".to_string()
},
Attribute {
key: "master_address".to_string(),
value: "".to_string(),
},
Attribute {
key: "address".to_string(),
value: "addr0001".to_string()
},
Attribute {
key: "vesting_denom".to_string(),
value: "{\"native\":\"uusd\"}".to_string()
},
Attribute {
key: "vesting_amount".to_string(),
value: "1000".to_string()
},
Attribute {
key: "method".to_string(),
value: "reward_users".to_string()
}
]
);

// valid amount - one failed because duplicate
let vesting_amount = 500u128;
let cliff_amount = 250u128;
let cliff_time = 105u64;

let msg = ExecuteMsg::RewardUsers {
master_address: None,
rewards: vec![
RewardUserRequest {
user_address: "addr0002".to_string(),
vesting_amount: Uint128::new(vesting_amount),
cliff_amount: Some(Uint128::new(cliff_amount)),
},
RewardUserRequest {
user_address: "addr0002".to_string(),
vesting_amount: Uint128::new(vesting_amount),
cliff_amount: Some(Uint128::new(cliff_amount)),
},
],
vesting_schedule: VestingSchedule::LinearVestingWithCliff {
start_time: Uint64::new(100),
end_time: Uint64::new(110),
vesting_amount: Uint128::zero(),
cliff_amount: Uint128::zero(),
cliff_time: Uint64::new(cliff_time),
},
};

let res =
execute(deps.as_mut(), env.clone(), mock_info("addr0000", &[]), msg)?;

assert_eq!(
res.attributes,
vec![
Attribute {
key: "action".to_string(),
value: "register_vesting_account".to_string()
},
Attribute {
key: "master_address".to_string(),
value: "".to_string(),
},
Attribute {
key: "address".to_string(),
value: "addr0002".to_string()
},
Attribute {
key: "vesting_denom".to_string(),
value: "{\"native\":\"uusd\"}".to_string()
},
Attribute {
key: "vesting_amount".to_string(),
value: "500".to_string()
},
Attribute {
key: "method".to_string(),
value: "reward_users".to_string()
}
]
);

Ok(())
}

Expand Down

0 comments on commit 6f2eb7a

Please sign in to comment.