use busbar_sf_auth::{Credentials, SalesforceCredentials};
use std::sync::OnceLock;
use tokio::sync::OnceCell;
pub const TEST_ACCOUNT_NAMES: &[&str] = &[
"BusbarIntTest_Alpha Corp",
"BusbarIntTest_Beta Industries",
"BusbarIntTest_Gamma Solutions",
];
pub async fn ensure_test_accounts(client: &busbar_sf_rest::SalesforceRestClient) -> Vec<String> {
let existing: Vec<serde_json::Value> = client
.query_all("SELECT Id, Name FROM Account WHERE Name LIKE 'BusbarIntTest_%' LIMIT 10")
.await
.expect("Query for test accounts should succeed");
if existing.len() >= TEST_ACCOUNT_NAMES.len() {
return existing
.iter()
.filter_map(|r| r.get("Id").and_then(|v| v.as_str()).map(String::from))
.collect();
}
let existing_names: Vec<String> = existing
.iter()
.filter_map(|r| r.get("Name").and_then(|v| v.as_str()).map(String::from))
.collect();
let mut ids: Vec<String> = existing
.iter()
.filter_map(|r| r.get("Id").and_then(|v| v.as_str()).map(String::from))
.collect();
for name in TEST_ACCOUNT_NAMES {
if !existing_names.iter().any(|n| n == name) {
let id = client
.create("Account", &serde_json::json!({"Name": name}))
.await
.expect("Create test account should succeed");
ids.push(id);
}
}
ids
}
#[allow(dead_code)]
pub async fn cleanup_test_accounts(client: &busbar_sf_rest::SalesforceRestClient) {
let accounts: Vec<serde_json::Value> = client
.query_all("SELECT Id FROM Account WHERE Name LIKE 'BusbarIntTest_%' LIMIT 100")
.await
.unwrap_or_default();
for account in accounts {
if let Some(id) = account.get("Id").and_then(|v| v.as_str()) {
let _ = client.delete("Account", id).await;
}
}
}
fn get_auth_url() -> &'static str {
static AUTH_URL: OnceLock<String> = OnceLock::new();
AUTH_URL.get_or_init(|| {
match std::env::var("SF_AUTH_URL") {
Ok(url) if !url.is_empty() => {
if !url.starts_with("force://") {
let preview = if url.len() > 50 {
format!("{}...", &url[..50])
} else {
url.clone()
};
panic!(
"Invalid SF_AUTH_URL format! Expected 'force://...' but got: {preview}"
);
}
url
}
Ok(_) => panic!("SF_AUTH_URL is set but empty!"),
Err(_) => panic!(
"SF_AUTH_URL environment variable is NOT set! \
Set it with: export SF_AUTH_URL=$(sf org display --target-org busbar-test --verbose --json | jq -r '.result.sfdxAuthUrl')"
),
}
})
}
static SHARED_CREDENTIALS: OnceCell<SalesforceCredentials> = OnceCell::const_new();
pub async fn get_credentials() -> SalesforceCredentials {
SHARED_CREDENTIALS
.get_or_init(|| async {
let auth_url = get_auth_url();
match SalesforceCredentials::from_sfdx_auth_url(auth_url).await {
Ok(creds) => creds,
Err(e) => {
panic!(
"Failed to authenticate with SF_AUTH_URL: {e}\n\
Ensure the org exists and the auth URL is fresh."
);
}
}
})
.await
.clone()
}
pub async fn get_revocable_credentials() -> SalesforceCredentials {
let shared = get_credentials().await;
SalesforceCredentials::new(
shared.instance_url(),
"00D_FABRICATED_ACCESS_TOKEN_FOR_REVOCATION_TEST",
shared.api_version(),
)
}