From ae93e91019a9150826aba9fd677b7b625e9f90cb Mon Sep 17 00:00:00 2001 From: Mike Stemle Date: Fri, 23 Feb 2024 12:06:49 -0500 Subject: [PATCH] Adding tests for `actions().remove_selected_repo_from_org_secret()` (#586) --- ...ns_add_selected_repo_to_org_secret_test.rs | 85 +++++++++++++++++++ ...move_selected_repo_from_org_secret_test.rs | 85 +++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 tests/actions_add_selected_repo_to_org_secret_test.rs create mode 100644 tests/actions_remove_selected_repo_from_org_secret_test.rs diff --git a/tests/actions_add_selected_repo_to_org_secret_test.rs b/tests/actions_add_selected_repo_to_org_secret_test.rs new file mode 100644 index 00000000..a2abc5c4 --- /dev/null +++ b/tests/actions_add_selected_repo_to_org_secret_test.rs @@ -0,0 +1,85 @@ +mod mock_error; + +use mock_error::setup_error_handler; +use octocrab::Octocrab; +use wiremock::{ + matchers::{method, path}, + Mock, MockServer, ResponseTemplate, +}; + +async fn setup_put_api(template: ResponseTemplate) -> MockServer { + let org: &str = "org"; + let secret_name: &str = "some_secret"; + let repository_id: u64 = 456; + + let mock_server = MockServer::start().await; + + Mock::given(method("PUT")) + .and(path(format!( + "/orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id}" + ))) + .respond_with(template.clone()) + .mount(&mock_server) + .await; + + setup_error_handler( + &mock_server, + &format!("PUT on /orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id} was not received"), + ) + .await; + mock_server +} + +fn setup_octocrab(uri: &str) -> Octocrab { + Octocrab::builder().base_uri(uri).unwrap().build().unwrap() +} + +const ORG: &str = "org"; +const SECRET_NAME: &str = "some_secret"; +const REPOSITORY_ID: u64 = 456; + +#[tokio::test] +async fn should_204() { + let template = ResponseTemplate::new(204); + let mock_server = setup_put_api(template).await; + let client = setup_octocrab(&mock_server.uri()); + + let actions = client.actions(); + + let result = actions + .add_selected_repo_to_org_secret( + ORG.to_owned(), + SECRET_NAME.to_owned(), + REPOSITORY_ID.into(), + ) + .await; + + assert!( + result.is_ok(), + "expected successful result, got error: {:#?}", + result + ); +} + +#[tokio::test] +async fn should_500() { + let template = ResponseTemplate::new(500); + let mock_server = setup_put_api(template).await; + let client = setup_octocrab(&mock_server.uri()); + + let actions = client.actions(); + + let result = actions + .add_selected_repo_to_org_secret( + ORG.to_owned(), + SECRET_NAME.to_owned(), + REPOSITORY_ID.into(), + ) + .await; + + assert!( + result.is_err(), + "expected error result, got success somehow: {:#?}", + result + ); +} diff --git a/tests/actions_remove_selected_repo_from_org_secret_test.rs b/tests/actions_remove_selected_repo_from_org_secret_test.rs new file mode 100644 index 00000000..f259e77a --- /dev/null +++ b/tests/actions_remove_selected_repo_from_org_secret_test.rs @@ -0,0 +1,85 @@ +mod mock_error; + +use mock_error::setup_error_handler; +use octocrab::Octocrab; +use wiremock::{ + matchers::{method, path}, + Mock, MockServer, ResponseTemplate, +}; + +async fn setup_delete_api(template: ResponseTemplate) -> MockServer { + let org: &str = "org"; + let secret_name: &str = "some_secret"; + let repository_id: u64 = 456; + + let mock_server = MockServer::start().await; + + Mock::given(method("DELETE")) + .and(path(format!( + "/orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id}" + ))) + .respond_with(template.clone()) + .mount(&mock_server) + .await; + + setup_error_handler( + &mock_server, + &format!("DELETE on /orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id} was not received"), + ) + .await; + mock_server +} + +fn setup_octocrab(uri: &str) -> Octocrab { + Octocrab::builder().base_uri(uri).unwrap().build().unwrap() +} + +const ORG: &str = "org"; +const SECRET_NAME: &str = "some_secret"; +const REPOSITORY_ID: u64 = 456; + +#[tokio::test] +async fn should_204() { + let template = ResponseTemplate::new(204); + let mock_server = setup_delete_api(template).await; + let client = setup_octocrab(&mock_server.uri()); + + let actions = client.actions(); + + let result = actions + .remove_selected_repo_from_org_secret( + ORG.to_owned(), + SECRET_NAME.to_owned(), + REPOSITORY_ID.into(), + ) + .await; + + assert!( + result.is_ok(), + "expected successful result, got error: {:#?}", + result + ); +} + +#[tokio::test] +async fn should_500() { + let template = ResponseTemplate::new(500); + let mock_server = setup_delete_api(template).await; + let client = setup_octocrab(&mock_server.uri()); + + let actions = client.actions(); + + let result = actions + .remove_selected_repo_from_org_secret( + ORG.to_owned(), + SECRET_NAME.to_owned(), + REPOSITORY_ID.into(), + ) + .await; + + assert!( + result.is_err(), + "expected error result, got success somehow: {:#?}", + result + ); +}