From 732f1db5e85a4311aa26fa6e91628769d0ca9413 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 1 Nov 2023 21:53:34 +0100 Subject: [PATCH] add integration tests --- contracts/human_checker/tests/workspaces.rs | 51 ++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/contracts/human_checker/tests/workspaces.rs b/contracts/human_checker/tests/workspaces.rs index a5d8f32..3df8fb5 100644 --- a/contracts/human_checker/tests/workspaces.rs +++ b/contracts/human_checker/tests/workspaces.rs @@ -4,7 +4,7 @@ use near_workspaces::{network::Sandbox, result::ExecutionFinalResult, Account, C use sbt::{SBTs, TokenMetadata}; use serde_json::json; -use human_checker::RegisterHumanPayload; +use human_checker::{RegisterHumanPayload, VotePayload, VOTING_DURATION}; const REGISTER_HUMAN_TOKEN: &str = "register_human_token"; @@ -29,6 +29,23 @@ impl Suite { Ok(res) } + pub async fn is_human_call_lock( + &self, + caller: &Account, + lock_duration: u64, + payload: &VotePayload, + ) -> anyhow::Result { + let res = caller + .call(self.registry.id(), "is_human_call_lock") + .args_json(json!({"ctr": self.human_checker.id(), "function": "vote", "payload": serde_json::to_string(payload).unwrap(), "lock_duration": lock_duration, "with_proof": false})) + .max_gas() + .transact() + .await?; + println!(">>> is_human_call_lock logs {:?}\n", res.logs()); + Ok(res) + } + + pub async fn query_sbts(&self, user: &Account) -> anyhow::Result> { // check the key does not exists in human checker let r = self @@ -171,5 +188,37 @@ async fn is_human_call() -> anyhow::Result<()> { tokens = suite.query_sbts(&john).await?; assert_eq!(tokens, None); + + // + // Test Vote with lock duration + // + + // + // test1: too short lock duration: should fail + let mut payload = VotePayload{prop_id: 10, vote: "wrong_option".to_string()}; + let r = suite.is_human_call_lock(&john, VOTING_DURATION / 2 *3, &payload).await?; + assert!(r.is_failure()); + let failure_str = format!("{:?}",r.failures()); + assert!(failure_str.contains("sufficient amount of time")); + + // + // test2: second call, should not change + let r = suite.is_human_call_lock(&john, VOTING_DURATION / 2*3, &payload).await?; + assert!(r.is_failure()); + let failure_str = format!("{:?}",r.failures()); + assert!(failure_str.contains("sufficient amount of time")); + + // + // test3: longer call should be accepted, but should fail on wrong payload (vote option) + let r = suite.is_human_call_lock(&john, VOTING_DURATION +100, &payload).await?; + assert!(r.is_failure()); + let failure_str = format!("{:?}",r.failures()); + assert!(failure_str.contains("invalid vote: must be either")); + + // should work with correct input + payload.vote = "approve".to_string(); + let r = suite.is_human_call_lock(&john, VOTING_DURATION +100, &payload).await?; + assert!(r.is_success()); + Ok(()) }