From 0dfcccb74422d9decdcac2168ef8929c669d2998 Mon Sep 17 00:00:00 2001 From: Matheus Zaniolo Date: Fri, 17 Jul 2026 18:09:55 +0200 Subject: [PATCH] feat!: Make zitadel client receive a reqwest_middleware client --- src/v2/mod.rs | 12 ++++--- tests/e2e.rs | 87 +++++++++++++++------------------------------------ 2 files changed, 33 insertions(+), 66 deletions(-) diff --git a/src/v2/mod.rs b/src/v2/mod.rs index 2cd0310..77b1796 100644 --- a/src/v2/mod.rs +++ b/src/v2/mod.rs @@ -62,12 +62,16 @@ impl Zitadel { /// - `service_account_file` should be the Zitadel-generated /// private key file as documented at [zitadel docs](https://zitadel.com/docs/guides/integrate/service-users/private-key-jwt#2-generate-a-private-key-file) /// - `aud` - Custom `aud` claim to use for auth (`url` is used if unset) - pub async fn new(url: Url, service_account_file: PathBuf, aud: Option) -> Result { + pub async fn new( + client: ClientWithMiddleware, + url: Url, + service_account_file: PathBuf, + aud: Option, + ) -> Result { let retry_policy = ExponentialBackoff::builder().build_with_max_retries(DEFAULT_MAX_RETRIES); - let client_builder = - ClientBuilder::new(reqwest::Client::builder().timeout(DEFAULT_TIMEOUT).build()?) - .with(RetryTransientMiddleware::new_with_policy(retry_policy)); + let client_builder = ClientBuilder::from_client(client) + .with(RetryTransientMiddleware::new_with_policy(retry_policy)); #[cfg(feature = "telemetry")] let client_builder = client_builder.with(OtelMiddleware); let client = client_builder.build(); diff --git a/tests/e2e.rs b/tests/e2e.rs index d7ac4a3..d6391ba 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -23,7 +23,7 @@ use famedly_zitadel_rust_client::v2::{ use futures::{StreamExt, TryStreamExt, future}; use josekit::{jws::JwsHeader, jwt::JwtPayload}; use rand::distr::{Alphanumeric, SampleString}; -use reqwest_middleware::ClientWithMiddleware; +use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use test_log::{self, test}; use time::{Duration, OffsetDateTime}; use url::Url; @@ -183,19 +183,17 @@ macro_rules! assert_list { }; } -#[tokio::test] -async fn test_e2e_create_token() -> Result<()> { +async fn mk_zitadel_client() -> Result { let service_account_file = Path::new(USER_SERVICE_PATH); let url = Url::parse("http://localhost:8080")?; - - assert!(Zitadel::new(url, service_account_file.to_path_buf(), None).await.is_ok()); - Ok(()) + let client = ClientBuilder::new(reqwest::Client::new()).build(); + Zitadel::new(client, url, service_account_file.to_path_buf(), None).await } -async fn mk_zitadel_client() -> Result { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - Zitadel::new(url, service_account_file.to_path_buf(), None).await +#[tokio::test] +async fn test_e2e_create_token() -> Result<()> { + assert!(mk_zitadel_client().await.is_ok()); + Ok(()) } #[test(tokio::test)] @@ -472,9 +470,7 @@ async fn test_e2e_role_and_membership_methods() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_create_organization() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let admins = vec![ (AddOrganizationRequestAdmin::new().with_human(AddHumanUserRequest::new( @@ -515,9 +511,7 @@ async fn test_e2e_create_organization() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_create_application() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let project = zitadel.create_project(V1AddProjectRequest::new("TestProject".to_owned()), None).await?; @@ -549,9 +543,7 @@ async fn test_e2e_create_application() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_list_applications() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; // TODO: Figure out why the teardown fails to delete this project let project = @@ -585,9 +577,7 @@ async fn test_e2e_list_applications() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_create_action() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let res = zitadel .create_action( @@ -625,9 +615,7 @@ async fn test_e2e_create_action() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_list_actions() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let mut action_names = Vec::new(); @@ -658,9 +646,7 @@ async fn test_e2e_list_actions() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_update_action() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let res = zitadel .create_action( @@ -707,9 +693,7 @@ async fn test_e2e_update_action() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_delete_action() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let res = zitadel .create_action( @@ -741,9 +725,7 @@ async fn test_e2e_delete_action() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "trace")] async fn test_e2e_set_trigger_actions() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let res = zitadel .create_action( @@ -778,9 +760,7 @@ async fn test_e2e_set_trigger_actions() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_create_human_user() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let resp = create_user(&zitadel, "John", "Doe").await; @@ -844,9 +824,7 @@ async fn test_e2e_create_user_human_and_machine() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_list_human_users() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let mut users = Vec::new(); @@ -885,9 +863,7 @@ async fn test_e2e_list_human_users() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_list_idp_links() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let idp_links = vec![]; @@ -913,9 +889,7 @@ async fn test_e2e_list_idp_links() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_change_user() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let last_name = "Doe"; let first_name = "John"; @@ -956,9 +930,7 @@ async fn test_e2e_change_user() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_activate_deactivate() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let id = create_user(&zitadel, "John", "Doe").await?; @@ -992,9 +964,7 @@ async fn test_e2e_activate_deactivate() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_delete_user() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let deleted_user_name = "John"; let user_last_name = "Doe"; @@ -1038,9 +1008,7 @@ async fn test_e2e_delete_user() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_user_metadata() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let metadata = vec![("key1", "value1"), ("key2", "value2")]; @@ -1090,9 +1058,7 @@ async fn test_e2e_user_metadata() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_user_metadata_bulk() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; let metadata = vec![ SetMetadataEntry::new("key1".to_owned(), "value1".to_owned()), @@ -1327,10 +1293,7 @@ async fn test_e2e_token_verification_positive() -> Result<()> { #[test(tokio::test)] #[test_log(default_log_filter = "debug")] async fn test_e2e_organization_scoped_operations() -> Result<()> { - let service_account_file = Path::new(USER_SERVICE_PATH); - let url = Url::parse("http://localhost:8080")?; - - let zitadel = Zitadel::new(url, service_account_file.to_path_buf(), None).await?; + let zitadel = mk_zitadel_client().await?; // Create two organizations to test scoping let org_name = "test-org-scoped";