From 835a51ce267238b564abd5b25772069e0772c7cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Thu, 16 Jan 2025 15:51:22 +0800 Subject: [PATCH] test: Test paginated action requests --- tests/e2e.rs | 124 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 108 insertions(+), 16 deletions(-) diff --git a/tests/e2e.rs b/tests/e2e.rs index 734e11f..e91127b 100644 --- a/tests/e2e.rs +++ b/tests/e2e.rs @@ -49,6 +49,15 @@ async fn tear_down(zitadel: &Zitadel) { let id = user.user_id().expect("Couldn't get user id during tear down."); zitadel.delete_user(id).await.expect("Error deleting user"); } + + let mut stream = zitadel + .search_actions(ListActionsRequest::new(vec![V1ActionQuery::new()])) + .expect("Error getting the list of actions to tear down"); + + while let Some(action) = stream.next().await { + let id = action.id().expect("Couldn't get action id during tear down."); + zitadel.delete_action(id.clone()).await.expect("Error deleting action"); + } } #[tokio::test] @@ -109,16 +118,55 @@ async fn test_e2e_create_application() -> Result<()> { let project_id = project.id().expect("Must have created a project with a valid ID"); - let res = zitadel + zitadel .create_application( project_id.to_owned(), ManagementServiceAddApiAppBody::new("TestApp".to_owned()), ) - .await; + .await?; - // TODO: Once we have a list application method, we should check - // that the org was actually created - assert!(res.is_ok()); + let stream = zitadel.list_applications( + project_id.to_owned(), + ListApplicationsRequest::new(vec![V1AppQuery::new() + .with_name_query(V1AppNameQuery::new().with_name("TestApp".to_owned()))]), + )?; + + let apps: Vec = + stream.map(|app| app.name().expect("App name must be set").clone()).collect().await; + assert_eq!(apps, ["TestApp"]); + + tear_down(&zitadel).await; + + Ok(()) +} + +#[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()).await?; + + let project = + zitadel.create_project(V1AddProjectRequest::new("TestProject".to_owned())).await?; + + let project_id = project.id().expect("Must have created a project with a valid ID"); + + let mut application_names = Vec::new(); + + for i in 0..10 { + let name = format!("TestApp{i}"); + let application = ManagementServiceAddApiAppBody::new(name.to_owned()); + application_names.push(name); + assert!(zitadel.create_application(project_id.to_owned(), application).await.is_ok()); + } + + let stream = + zitadel.list_applications(project_id.to_owned(), ListApplicationsRequest::new(vec![]))?; + + let found_application_names: Vec = + stream.map(|app| app.name().expect("No name found for action").clone()).collect().await; + assert_eq!(found_application_names, application_names); tear_down(&zitadel).await; @@ -139,10 +187,49 @@ async fn test_e2e_create_action() -> Result<()> { )) .await; - // TODO: Once we have a list action method, we should check that - // the action was actually created assert!(res.is_ok()); + let stream = zitadel.search_actions(ListActionsRequest::new(vec![V1ActionQuery::new() + .with_action_name_query(V1ActionNameQuery::new().with_name("test-action".to_owned()))]))?; + + let actions: Vec = stream + .map(|action| action.name().expect("No name found for action").clone()) + .collect() + .await; + assert_eq!(actions, vec!["test-action"]); + + tear_down(&zitadel).await; + + Ok(()) +} + +#[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()).await?; + + let mut action_names = Vec::new(); + + for i in 0..10 { + let name = format!("test-action-{i}"); + + let action = + V1CreateActionRequest::new(name.clone(), "console.log('test-action')".to_owned()); + + action_names.push(name); + assert!(zitadel.create_action(action).await.is_ok()); + } + + let stream = zitadel.search_actions(ListActionsRequest::new(vec![]))?; + + let found_action_names: Vec = stream + .map(|action| action.name().expect("No name found for action").clone()) + .collect() + .await; + assert_eq!(found_action_names, action_names); + tear_down(&zitadel).await; Ok(()) @@ -164,7 +251,7 @@ async fn test_e2e_update_action() -> Result<()> { let action_id = res.id().expect("Must have created an action with a valid ID"); - let res = zitadel + zitadel .update_action( action_id.to_owned(), ManagementServiceUpdateActionBody::new( @@ -172,11 +259,15 @@ async fn test_e2e_update_action() -> Result<()> { "console.log('test-action-update')".to_owned(), ), ) - .await; + .await?; - // TODO: Once we have a list action method, we should check that - // the action was actually updated - assert!(res.is_ok()); + let mut stream = zitadel.search_actions(ListActionsRequest::new(vec![V1ActionQuery::new() + .with_action_name_query(V1ActionNameQuery::new().with_name("test-action".to_owned()))]))?; + + let action = stream.next().await.expect("Action must exist"); + + assert_eq!(action.name(), Some(&"test-action".to_owned())); + assert_eq!(action.script(), Some(&"console.log('test-action-update')".to_owned())); tear_down(&zitadel).await; @@ -199,11 +290,12 @@ async fn test_e2e_delete_action() -> Result<()> { let action_id = res.id().expect("Must have created an action with a valid ID"); - let res = zitadel.delete_action(action_id.to_owned()).await; + zitadel.delete_action(action_id.to_owned()).await?; - // TODO: Once we have a list action method, we should check that - // the action was actually deleted - assert!(res.is_ok()); + let stream = zitadel.search_actions(ListActionsRequest::new(vec![V1ActionQuery::new() + .with_action_name_query(V1ActionNameQuery::new().with_name("test-action".to_owned()))]))?; + + assert_eq!(stream.count().await, 0); tear_down(&zitadel).await;