diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fc1a641..c1b1018 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -69,3 +69,5 @@ jobs: uses: actions-rs/cargo@v1 with: command: test + args: TEST_CONFIG=test_config_example.json + diff --git a/interact/Cargo.toml b/interact/Cargo.toml index b347e63..a5627a8 100644 --- a/interact/Cargo.toml +++ b/interact/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pdao-near-interact" version = "0.0.1" -authors = ["PDAO Team "] +authors = ["happyhappy ", "PDAO Team "] edition = '2021' include = ["src/**/*", "Cargo.toml"] license = "MIT" @@ -13,7 +13,7 @@ serde = { version = "1.0", features = ["derive"] } tokio-postgres = "0.7.6" tokio = { version = "1.0", features = ["full"] } futures = "0.3" -reqwest = "0.11" +reqwest = { version = "0.11", features = ["json"] } serde_json = "1.0" anyhow = "1.0" thiserror = "1.0" diff --git a/interact/test_config_example.json b/interact/test_config_example.json index 99076a6..56f3f48 100644 --- a/interact/test_config_example.json +++ b/interact/test_config_example.json @@ -1,6 +1,7 @@ { - "full_node_url": "https://example.com", + "network_id": "testnet", + "test_node_url": "https://rpc.testnet.near.org", "account_public": "example.testnet.public", "account_private": "example.testnet.private", "wasm_binary_path": "example/path" -} \ No newline at end of file +} diff --git a/interact/tests/suite1.rs b/interact/tests/suite1.rs index 5ba3dcd..3c906ee 100644 --- a/interact/tests/suite1.rs +++ b/interact/tests/suite1.rs @@ -1,8 +1,12 @@ +use reqwest::Client; use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use tokio::time::{sleep, Duration}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { - full_node_url: String, + network_id: String, + test_node_url: String, account_public: String, account_private: String, wasm_binary_path: String, @@ -23,28 +27,94 @@ impl Config { } #[tokio::test] -#[ignore] async fn check_connection() { - let _config = Config::read_from_env(); + let config = Config::read_from_env(); // check whether the full node is responding by a simple request - unimplemented!(); + let client = Client::new(); + let payload = json!({ + "jsonrpc": "2.0", + "id": "dontcare", + "method": "status", + "params": [] + }); + + let res = client + .post(&config.test_node_url) + .json(&payload) + .send() + .await + .unwrap(); + + let json = res.json::().await.unwrap(); + assert_eq!(json["result"]["chain_id"], config.network_id); } #[tokio::test] -#[ignore] async fn check_block_number() { let _config = Config::read_from_env(); // check the latest block number recognized by the full node **twice** with some delay, // so that we can assure that the full node is properly updating its blocks - unimplemented!(); + let client = Client::new(); + let payload = json!({ + "jsonrpc": "2.0", + "id": "dontcare", + "method": "block", + "params": { + "finality": "final" + } + }); + + let first_res = client + .post(&_config.test_node_url) + .json(&payload) + .send() + .await + .unwrap(); + + let first_json = first_res.json::().await.unwrap(); + + sleep(Duration::from_secs(2)).await; + + let second_res = client + .post(&_config.test_node_url) + .json(&payload) + .send() + .await + .unwrap(); + + let second_json = second_res.json::().await.unwrap(); + + assert!( + second_json["result"]["header"]["height"].as_u64() + > first_json["result"]["header"]["height"].as_u64() + ); } #[tokio::test] -#[ignore] async fn check_account() { let _config = Config::read_from_env(); // by requesting the full node, check whether the account given by the config has enough native token to pay gas fee - unimplemented!(); + let client = Client::new(); + let payload = json!({ + "jsonrpc": "2.0", + "id": "dontcare", + "method": "query", + "params": { + "request_type": "view_account", + "finality": "final", + "account_id": "nearkat.testnet" + } + }); + + let res = client + .post(&_config.test_node_url) + .json(&payload) + .send() + .await + .unwrap(); + + let json = res.json::().await.unwrap(); + assert_eq!(json["error"], Value::Null); } #[tokio::test]