From c85e46c64ae3a7e809c78ede522545bc04e4ef0a Mon Sep 17 00:00:00 2001 From: Thomas Avery Date: Mon, 25 Mar 2024 15:40:53 -0500 Subject: [PATCH] Add crate for testing secrets sync --- Cargo.lock | 11 +++++ crates/test-sync/Cargo.toml | 22 +++++++++ crates/test-sync/src/main.rs | 96 ++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 crates/test-sync/Cargo.toml create mode 100644 crates/test-sync/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index dfa714cc6..58c269487 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3293,6 +3293,17 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "test-sync" +version = "0.1.0" +dependencies = [ + "bitwarden", + "chrono", + "color-eyre", + "tokio", + "uuid", +] + [[package]] name = "textwrap" version = "0.16.1" diff --git a/crates/test-sync/Cargo.toml b/crates/test-sync/Cargo.toml new file mode 100644 index 000000000..af227a8cd --- /dev/null +++ b/crates/test-sync/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "test-sync" +version = "0.1.0" +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +homepage.workspace = true +repository.workspace = true +license-file.workspace = true +keywords.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bitwarden = { workspace = true, features = ["secrets"] } +tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"] } +uuid = { version = "^1.7.0", features = ["serde"] } +color-eyre = "0.6" +chrono = { version = "0.4.35", features = [ + "clock", + "std", +], default-features = false } diff --git a/crates/test-sync/src/main.rs b/crates/test-sync/src/main.rs new file mode 100644 index 000000000..6ca3a0f3e --- /dev/null +++ b/crates/test-sync/src/main.rs @@ -0,0 +1,96 @@ +use bitwarden::{ + auth::login::AccessTokenLoginRequest, client::client_settings::ClientSettings, + secrets_manager::secrets::SecretsSyncRequest, Client, +}; +use chrono::{DateTime, FixedOffset, Utc}; +use color_eyre::eyre::Result; +use uuid::Uuid; + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { + let identity_url = "http://localhost:33656".to_string(); + let api_url = " http://localhost:4000".to_string(); + let access_token = "access_token_goes_here".to_string(); + let organization_id: Uuid = "organization_id_goes_here" + .parse() + .expect("Failed to parse organization ID"); + + let settings: ClientSettings = ClientSettings { + identity_url, + api_url, + ..Default::default() + }; + + let mut client = bitwarden::Client::new(Some(settings)); + + let asdf = client + .auth() + .login_access_token(&AccessTokenLoginRequest { + access_token, + state_file: None, + }) + .await?; + println!("{:?}", asdf); + + call_with_specific_date(&mut client, organization_id).await?; + call_with_current_date(&mut client, organization_id).await?; + call_with_no_date(&mut client, organization_id).await?; + + Ok(()) +} + +async fn call_with_specific_date(client: &mut Client, organization_id: Uuid) -> Result<()> { + let last_sync_date = + DateTime::::parse_from_rfc3339("2024-03-25T19:48:06.813330+00:00") + .unwrap() + .with_timezone(&Utc); + + call_sync( + client, + &SecretsSyncRequest { + organization_id: organization_id, + last_synced_date: Some(last_sync_date), + }, + ) + .await?; + + Ok(()) +} + +async fn call_with_current_date(client: &mut Client, organization_id: Uuid) -> Result<()> { + call_sync( + client, + &SecretsSyncRequest { + organization_id: organization_id, + last_synced_date: Some(Utc::now()), + }, + ) + .await?; + + Ok(()) +} + +async fn call_with_no_date(client: &mut Client, organization_id: Uuid) -> Result<()> { + call_sync( + client, + &SecretsSyncRequest { + organization_id: organization_id, + last_synced_date: None, + }, + ) + .await?; + + Ok(()) +} + +async fn call_sync(client: &mut Client, request: &SecretsSyncRequest) -> Result<()> { + let sync_response = client.secrets().sync(request).await?; + + if sync_response.has_changes { + println!("{:?}", sync_response); + } else { + println!("{:?}", sync_response.has_changes); + } + + Ok(()) +}