Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Mar 28, 2024
1 parent 579215a commit 577d013
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
8 changes: 7 additions & 1 deletion contracts/secret_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub fn instantiate(
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
if !MY_KEYS.is_empty(deps.storage) {
return Err(ContractError::AlreadyInitialized);
}

create_keys(deps, env, msg.end_time)?;

Ok(Response::default())
Expand Down Expand Up @@ -57,7 +61,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
}

fn query_keys(deps: Deps, env: Env) -> Result<KeysResponse, ContractError> {
let my_keys = MY_KEYS.load(deps.storage)?;
let my_keys = MY_KEYS
.load(deps.storage)
.map_err(|_| ContractError::NotInitialized)?;
let private = (env.block.time > my_keys.end_time).then(|| my_keys.private_key);

Ok(KeysResponse {
Expand Down
6 changes: 6 additions & 0 deletions contracts/secret_contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ pub enum ContractError {

#[error("Invalid End Time")]
InvalidEndTime,

#[error("Already initialized")]
AlreadyInitialized,

#[error("Not initialized yet")]
NotInitialized,
}
54 changes: 53 additions & 1 deletion contracts/secret_contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cosmwasm_std::{testing::*, Timestamp};
use secp256k1::{PublicKey, Secp256k1};

use crate::contract::{instantiate, query};
use crate::error::ContractError;
use crate::msg::{InstantiateMsg, KeysResponse, QueryMsg};

fn setup_contract(
Expand Down Expand Up @@ -43,6 +44,43 @@ fn proper_initialization() {
assert!(PublicKey::from_slice(&value.public).is_ok());
}

#[test]
fn user_cant_reinitialize() {
let time = Timestamp::from_seconds(500);
let mut mock_env = mock_env();

mock_env.block.time = time.minus_seconds(5);

let mut deps = setup_contract(time, mock_env.clone());

let info = mock_info(
"creator",
&[Coin {
denom: "earth".to_string(),
amount: Uint128::new(1000),
}],
);

let res = query(deps.as_ref(), mock_env.clone(), QueryMsg::GetKeys {}).unwrap();
let init: KeysResponse = from_binary(&res).unwrap();

assert_eq!(
instantiate(
deps.as_mut(),
mock_env.clone(),
info,
InstantiateMsg {
end_time: time.plus_seconds(10),
},
),
Err(ContractError::AlreadyInitialized)
);
let res = query(deps.as_ref(), mock_env, QueryMsg::GetKeys {}).unwrap();
let upd: KeysResponse = from_binary(&res).unwrap();

assert_eq!(init, upd);
}

#[test]
fn invalid_time_is_failure() {
let time = Timestamp::from_seconds(500);
Expand All @@ -60,7 +98,10 @@ fn invalid_time_is_failure() {

let init_msg = InstantiateMsg { end_time: time };

instantiate(deps.as_mut(), mock_env, info, init_msg).expect_err("Should fail");
assert_eq!(
instantiate(deps.as_mut(), mock_env, info, init_msg),
Err(ContractError::InvalidEndTime)
);
}

#[test]
Expand Down Expand Up @@ -92,3 +133,14 @@ fn secret_revealed_after_time_pass() {
PublicKey::from_slice(&value.public).unwrap()
);
}

#[test]
fn cannot_fetch_uninitialized_contract() {
let deps = mock_dependencies();
let mock_env = mock_env();

assert_eq!(
query(deps.as_ref(), mock_env, QueryMsg::GetKeys {}),
Err(ContractError::NotInitialized)
);
}

0 comments on commit 577d013

Please sign in to comment.