diff --git a/runtime/lite/src/modules/kuska_ssb_client/client/client.rs b/runtime/lite/src/modules/kuska_ssb_client/client/client.rs index a6b0d764..d5154a5d 100644 --- a/runtime/lite/src/modules/kuska_ssb_client/client/client.rs +++ b/runtime/lite/src/modules/kuska_ssb_client/client/client.rs @@ -144,7 +144,7 @@ impl Client { Ok(msg) } - pub async fn user(&mut self, live: bool, user_id: &str) -> Result<()> { + pub async fn user(&mut self, live: bool, user_id: &str) -> Result> { let user_id = match user_id { "me" => self.whoami().await?, _ => user_id.to_string(), @@ -153,9 +153,9 @@ impl Client { let args = CreateHistoryStreamIn::new(user_id).live(live); let req_id = self.api.create_history_stream_req_send(&args).await?; - self.print_source_until_eof(req_id, feed_res_parse).await?; + let feed = self.print_source_until_eof(req_id, feed_res_parse).await?; - Ok(()) + Ok(feed) } pub async fn feed(&mut self, live: bool) -> Result> { diff --git a/runtime/lite/src/modules/kuska_ssb_client/client/tests.rs b/runtime/lite/src/modules/kuska_ssb_client/client/tests.rs index 82478187..aabbd573 100644 --- a/runtime/lite/src/modules/kuska_ssb_client/client/tests.rs +++ b/runtime/lite/src/modules/kuska_ssb_client/client/tests.rs @@ -1,48 +1,199 @@ #[cfg(test)] mod tests { use crate::modules::kuska_ssb_client::client::{types, Client, UserConfig}; - - use super::*; - // use crate::{Client, Event}; + use dotenv::dotenv; + use kuska_ssb::keystore::read_patchwork_config; // ssb-server should keep running for testing + /* configure the env variables such as ssb-sercret file path, ip and port where + ssb-server is running in .env file */ // use `cargo test -- --ignored` command for testing + #[async_std::test] #[ignore] async fn test_client() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); + } + + #[async_std::test] + #[ignore] + async fn test_client_with_config() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + // passing default ip and port of ssb-server for testing - let mut client = Client::new(None, "0.0.0.0".to_string(), "8008".to_string()) + Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); + } + + #[async_std::test] + #[ignore] + async fn test_get_secret_key() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let client = Client::new(Some(config.clone()), ssb_ip, ssb_port) .await .unwrap(); - client.user(false, "me").await.unwrap(); + + let secret_key = client.get_secret_key(); + + assert_eq!(secret_key, config.sk); } #[async_std::test] #[ignore] - async fn test_feed() { - let mut client = Client::new(None, "0.0.0.0".to_string(), "8008".to_string()) + async fn test_whoami() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let mut client = Client::new(Some(config.clone()), ssb_ip, ssb_port) .await .unwrap(); - client.feed(false).await.unwrap(); + + let whoami = client.whoami().await.unwrap(); + assert_eq!(whoami, config.id); } #[async_std::test] #[ignore] - async fn test_feed_test() { - use crate::modules::kuska_ssb_client::client::UserConfig; - let user = UserConfig::new("vhuaeBySHfMTeBpTseKP/ksOVtyLGaqZ+Ae4SyQk7wY=", - "MywOEUUCk9rUcWq6OFsfbzZABDc+sItJHJoN+RJrwMK+G5p4HJId8xN4GlOx4o/+Sw5W3IsZqpn4B7hLJCTvBg=", - "@vhuaeBySHfMTeBpTseKP/ksOVtyLGaqZ+Ae4SyQk7wY=.ed25519"); - let mut client = Client::new(None, "0.0.0.0".to_string(), "8015".to_string()).await.unwrap(); - client.feed(true).await.unwrap(); + // returns list of feeds posted by particular user + async fn test_user_method() { + use types::Event; + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let mut client = Client::new(Some(config.clone()), ssb_ip, ssb_port) + .await + .unwrap(); + + let old_event = Event { + id: "1".to_string(), + body: "hello_world_event".to_string(), + }; + + let value = serde_json::to_value(old_event.clone()).unwrap(); + + client.publish(&value.to_string(), None).await.unwrap(); + + // wait for server to publish + async_std::task::sleep(std::time::Duration::from_secs(1)).await; + + let feed = client.user(false, &config.id).await.unwrap(); + + let event = feed.last().unwrap().value.clone(); + let message = event.get("content").unwrap(); + + let feed_type = message.get("type").unwrap(); + let feed_type: String = serde_json::from_value(feed_type.clone()).unwrap(); + + assert_eq!(&feed_type, "post"); + + let feed_text = message.get("text").unwrap(); + let feed_text: String = serde_json::from_value(feed_text.clone()).unwrap(); + + let new_event: Event = serde_json::from_str(&feed_text).unwrap(); + // let event = serde_json::from_value(event).unwrap(); + assert_eq!(old_event, new_event); + } + + #[async_std::test] + #[ignore] + #[should_panic = "Already closed"] + async fn test_close() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let mut client = Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); + + client.close().await.unwrap(); + client.whoami().await.unwrap(); + } + + #[async_std::test] + #[ignore] + async fn test_feed() { + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let mut client = Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); + + client.feed(false).await.unwrap(); } #[async_std::test] #[ignore] async fn test_publish() { - let mut client = Client::new(None, "0.0.0.0".to_string(), "8008".to_string()) - .await - .unwrap(); + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); + + let mut client = Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); let feed = client.feed(false).await.unwrap(); let prev_len = feed.len(); @@ -73,13 +224,24 @@ mod tests { let feed_text: String = serde_json::from_value(feed_text.clone()).unwrap(); let new_event: types::Event = serde_json::from_str(&feed_text).unwrap(); - // let event = serde_json::from_value(event).unwrap(); assert_eq!(old_event, new_event); } #[tokio::test] #[ignore] async fn test_event_subscription() { + use super::*; + + dotenv().ok(); + + let secret = std::env::var("SECRET").unwrap_or_else(|_| { + let home_dir = dirs::home_dir().unwrap(); + std::format!("{}/.ssb/secret", home_dir.to_string_lossy()) + }); + let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string()); + let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string()); + let mut file = async_std::fs::File::open(secret).await.unwrap(); + let config = read_patchwork_config(&mut file).await.unwrap(); //TODO // Must start a local dev polkadot Node // Must start and setup a ssb-server @@ -88,14 +250,9 @@ mod tests { // Tranfer the amount manually after starting this function - //Todo + //Todo // Change user configuration - let user = UserConfig::new("PV5BFUk8N6DN1lEmnaS6ssZ9HvUc5WqLZP0lHN++CME=", - "iwmBTO3wfIqvOa8aodBJSdmcqhY4IByy9THlWNalL7E9XkEVSTw3oM3WUSadpLqyxn0e9Rzlaotk/SUc374IwQ=", - "@PV5BFUk8N6DN1lEmnaS6ssZ9HvUc5WqLZP0lHN++CME=.ed25519"); - let mut client = Client::new(None, "0.0.0.0".to_string(), "8014".to_string()) - .await - .unwrap(); + let mut client = Client::new(Some(config), ssb_ip, ssb_port).await.unwrap(); use subxt::{OnlineClient, PolkadotConfig}; use subxt_signer::sr25519::dev; diff --git a/runtime/lite/src/modules/wasmtime_wasi_module/tests.rs b/runtime/lite/src/modules/wasmtime_wasi_module/tests.rs index 3f8d0680..dfef33d9 100644 --- a/runtime/lite/src/modules/wasmtime_wasi_module/tests.rs +++ b/runtime/lite/src/modules/wasmtime_wasi_module/tests.rs @@ -1,15 +1,12 @@ #[async_std::test] async fn test_hello_world() { let path = std::env::var("WORKFLOW_WASM") - .unwrap_or("../../../../workflow/examples/hello_world.wasm".to_string()); + .unwrap_or("../../workflow/examples/hello_world.wasm".to_string()); let wasm = std::fs::read(&path).unwrap(); - let server = test_util::post("127.0.0.1:8080").await; let input = serde_json::json!({ - "allowed_hosts": [ - server.uri() - ], + "allowed_hosts": [], "data": { "hello" : "world" } @@ -24,7 +21,7 @@ async fn test_hello_world() { async fn test_employee_salary() { let path = std::env::var("WORKFLOW_WASM").unwrap_or( - "../../../../workflow/examples/employee_salary_state_managed.wasm".to_string(), + "../../workflow/examples/employee_salary_state_managed.wasm".to_string(), ); let wasm = std::fs::read(&path).unwrap();