Skip to content

Commit

Permalink
add mock keys storage
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMaxyz committed Aug 24, 2024
1 parent 7d9d6f9 commit 65c5295
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn from_semver(err: semver::Error) -> ContractError {

mod execute {
use cosmwasm_std::{
from_json, to_json_binary, CosmosMsg, DepsMut, Env, Event, MessageInfo, Response,
from_json, to_json_binary, Addr, CosmosMsg, DepsMut, Env, Event, MessageInfo, Response,
};
use cw20::Cw20ReceiveMsg;

Expand Down Expand Up @@ -146,7 +146,7 @@ mod execute {
) {
Ok(payload) => {
// check if sender is equal to address in token
let addr_in_jwt = _deps.api.addr_validate(&payload.nonce)?;
let addr_in_jwt: Addr = _deps.api.addr_validate(&payload.nonce)?;
if addr_in_jwt != _info.sender {
return Err(ContractError::Unauthorized {});
}
Expand Down
30 changes: 19 additions & 11 deletions src/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
use crate::{
contract::{self, instantiate},
msg::{self, InstantiateMsg, KeysMsg, QueryClaimResponse, QueryMsg},
state::{AUDIENCE, KID_MAP},
};
use cosmwasm_std::{
from_json,
testing::{mock_dependencies, mock_env, mock_info},
to_json_binary, Deps, DepsMut, MessageInfo, Uint128,
to_json_binary, Deps, DepsMut, MessageInfo, Storage, Uint128,
};
use cw20::Cw20ReceiveMsg;

pub const AUDIENCE: &str =
"965798652522-bn240k47q576vhqon2tuk0feg20bbt0u.apps.googleusercontent.com";
pub const AUD: &str = "965798652522-bn240k47q576vhqon2tuk0feg20bbt0u.apps.googleusercontent.com";
pub const EMAIL: &str = "meisamtaher71@gmail.com";
pub const SESSION_JWT: &str = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImE0OTM5MWJmNTJiNThjMWQ1NjAyNTVjMmYyYTA0ZTU5ZTIyYTdiNjUiLCJ0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RzlwoLCt-sHov6qV6WqzyakHq3qGxUCUCu0JWFlRThmWQ7q2C4pk0P6yBJKN3URtvgq_I-5xOtxt097aZCbVc0S29jCvyerbV9ZFi2hj8dgyFj1hQm-m6TQiHzGEEwUMly80n_iyVidVzQ";
pub const NOUNCE: &str = "xion15a5ws2y636kf2vs7wets44ay78dsr5zq3399eqfv4nknkw7javds6p7ccc";
Expand Down Expand Up @@ -120,7 +120,7 @@ fn test_claim_by_email() {

// claim the tokens by email
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand Down Expand Up @@ -157,7 +157,7 @@ fn test_aggregate_multiple_receives() {
);
// Claim
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand Down Expand Up @@ -201,7 +201,7 @@ fn test_claim_multi_token() {
);
// Claim
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand Down Expand Up @@ -233,7 +233,7 @@ fn test_claim_only_once() {

// claim the tokens by email
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand All @@ -242,7 +242,7 @@ fn test_claim_only_once() {

// panics
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand Down Expand Up @@ -274,7 +274,7 @@ fn test_claim_panicks_with_wrong_sender() {

// should panic
claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
// another sender tries to claim tokens with valid jwt, but jwt is signed for address1
Expand All @@ -299,7 +299,7 @@ fn test_empty_memo() {
);

claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand All @@ -324,7 +324,7 @@ fn test_panic_long_memo() {
);

claim_by_email(
AUDIENCE.to_string(),
AUD.to_string(),
SESSION_JWT.to_string(),
dep.as_mut(),
mock_info(NOUNCE, &[]),
Expand All @@ -346,6 +346,8 @@ fn claim_by_email(_aud: String, _jwt: String, _dep: DepsMut, _info: MessageInfo,
let claim_msg = crate::msg::ExecuteMsg::Claim {
msg: token_claim_msg,
};
// mock keys
mock_keys(_dep.storage);
match crate::contract::execute(_dep, mock_env(), _info.clone(), claim_msg) {
Ok(resp) => {
assert_eq!(
Expand Down Expand Up @@ -389,6 +391,12 @@ fn send_token(
};
}

fn mock_keys(s: &mut dyn Storage) {
KID_MAP.save(s, "d2d444cf8c5e3ae8386d66a13a316976aa369991", &"onV5tzUbqyPfkM6MwUqCtrqun9x20hEUbIUlmAYYuPuMhsaNHJqs1AVzRt2TzaNjmPVddEbU7VMDmeFWUt7vgDi7Xu0leevuIN4VSPbAMGBa0oj9Qopqkn9ePO_7DvIN13ktHgfQqatNBu6uXH6zkUl3VtXnubXrUhx7uyF22dARDc1-pJoj2NnsvgxDRElPMyDkU-siVv3c6cgIEwLEZZPWOcwplPTUB4qeTK6prrPBGQshuE1PWK2ZrYpIvXfzHyEbkGdPnrhcxgCzbKBUFvr8n_sfSurLRoDBLjkURKmgB8T8iRzLyXsCu9D3Hw61LKuex1aeSQLdwOFLuUEBdw;AQAB".to_string()).unwrap();
KID_MAP.save(s, "a49391bf52b58c1d560255c2f2a04e59e22a7b65", &"v7hTj49pNGYjxKbgMx_iDyjeErhfJFepMl306IV_TW5T_CEGE4lWFfBe9w0cwpi5KD6XlC1GO1AsrtzcYF29wJ283GNBZRkbl8iTe-LQYdjQsBtf_1fLIVt6LR7H2U1RPqa3pY16Kq6i6yC2osVg6tD7ApBCGw1WKe8uU3cm28biJzuV4gv6PzcbOdErd-hb4Cv6n2SoMPYlBfT4pWee75poQh8DYoQ1KJwowz3U1MaxOBMP260hmDK-QK0q4LYabCQiBNsz4FWWcaAAFxZFbiqGY5Gdu18uOkpMbdAN5FoZ_6nMDMSTmlf0CHv7gZe_cL38kZvTaynkWwDxqsW_Xw;AQAB".to_string()).unwrap();
AUDIENCE.save(s, &AUD.to_string()).unwrap();
}

fn send_token_custom_memo(
_dep: DepsMut,
_info: MessageInfo,
Expand Down

0 comments on commit 65c5295

Please sign in to comment.