ubisync/tests/api.rs

87 lines
2.8 KiB
Rust
Raw Normal View History

2023-12-07 19:51:19 +01:00
use std::time::Duration;
2024-01-05 20:48:23 +01:00
use tracing::{debug, Level};
use ubisync::{
api::v0::app::AppDescription,
config::Config,
state::types::{Element, ElementContent, ElementId},
Ubisync,
};
2023-12-07 19:51:19 +01:00
#[tokio::test(flavor = "multi_thread")]
async fn two_nodes_element_creation() {
2024-01-05 20:48:23 +01:00
tracing_subscriber::fmt()
.pretty()
.with_max_level(Level::DEBUG)
.init();
2023-12-07 19:51:19 +01:00
// Two nodes need to bind to different ports
let mut c2 = Config::default();
c2.api_config.port = Some(9982);
let ubi1 = Ubisync::new(&Config::default()).await.unwrap();
let ubi2 = Ubisync::new(&c2).await.unwrap();
2024-01-05 20:48:23 +01:00
ubi1.add_peer_from_id(ubi2.get_destination().unwrap().into())
.unwrap();
2023-12-07 19:51:19 +01:00
let http_client = reqwest::Client::new();
2024-01-05 20:48:23 +01:00
let register_response = http_client
.put("http://localhost:9981/v0/app/register")
.json(&AppDescription {
name: "Test".to_string(),
desc_text: "desc".to_string(),
})
.send()
.await
.unwrap();
let jwt1 = register_response
.text()
.await
.expect("Couldn't fetch token from response");
let register_response = http_client
.put("http://localhost:9982/v0/app/register")
.json(&AppDescription {
name: "Test".to_string(),
desc_text: "desc".to_string(),
})
.send()
.await
.unwrap();
let jwt2 = register_response
.text()
.await
.expect("Couldn't fetch token from response");
2023-12-07 19:51:19 +01:00
let test_element_content = ElementContent::Text("Text".to_string());
2024-01-05 20:48:23 +01:00
let put_resp = http_client
.put(&format!("http://localhost:9981/v0/element"))
.json(&test_element_content)
.header("Authorization", &format!("Bearer {}", &jwt1))
.send()
.await
.unwrap();
2023-12-07 19:51:19 +01:00
debug!("{:?}", &put_resp);
let put_resp_text = put_resp.text().await.expect("No put response body");
debug!("{}", put_resp_text);
2024-01-05 20:48:23 +01:00
let id =
serde_json::from_str::<ElementId>(&put_resp_text).expect("Could not deserialize ElementId");
2023-12-07 19:51:19 +01:00
tokio::time::sleep(Duration::from_millis(3000)).await;
2024-01-05 20:48:23 +01:00
let get_resp = http_client
.get(&format!(
"http://localhost:9982/v0/element/{}",
Into::<String>::into(&id)
))
.header("Authorization", &format!("Bearer {}", &jwt2))
.send()
.await
.expect("Get request failed");
let get_resp_text = get_resp.text().await.expect("No get request body");
debug!("{}", get_resp_text);
2024-01-05 20:48:23 +01:00
let received_element =
serde_json::from_str::<Element>(&get_resp_text).expect("Could not deserialize Element");
2023-12-07 19:51:19 +01:00
debug!("Other node received this element: {:?}", received_element);
2024-01-05 20:48:23 +01:00
2023-12-07 19:51:19 +01:00
assert_eq!(&test_element_content, received_element.content());
std::process::exit(0);
}