|
| 1 | +//! Common utilities for integration tests. |
| 2 | +//! |
| 3 | +//! These tests target ArcGIS Online (AGOL) and require credentials |
| 4 | +//! set in a `.env` file at the repository root. |
| 5 | +
|
| 6 | +use arcgis::{ArcGISClient, auth::ApiKeyAuth}; |
| 7 | +use std::sync::OnceLock; |
| 8 | + |
| 9 | +/// Load environment variables from .env file. |
| 10 | +/// Only loads once, subsequent calls are no-ops. |
| 11 | +pub fn load_env() { |
| 12 | + static INIT: OnceLock<()> = OnceLock::new(); |
| 13 | + INIT.get_or_init(|| { |
| 14 | + dotenvy::dotenv().ok(); |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +/// Get CLIENT_ID from environment. |
| 19 | +pub fn client_id() -> String { |
| 20 | + load_env(); |
| 21 | + std::env::var("CLIENT_ID") |
| 22 | + .expect("CLIENT_ID not found in environment. Add to .env file") |
| 23 | +} |
| 24 | + |
| 25 | +/// Get CLIENT_SECRET from environment. |
| 26 | +pub fn client_secret() -> String { |
| 27 | + load_env(); |
| 28 | + std::env::var("CLIENT_SECRET") |
| 29 | + .expect("CLIENT_SECRET not found in environment. Add to .env file") |
| 30 | +} |
| 31 | + |
| 32 | +/// Get an optional API key from environment. |
| 33 | +/// Some tests may use API key instead of OAuth. |
| 34 | +pub fn api_key() -> Option<String> { |
| 35 | + load_env(); |
| 36 | + std::env::var("ARCGIS_API_KEY").ok() |
| 37 | +} |
| 38 | + |
| 39 | +/// Create a test client with API key authentication. |
| 40 | +/// |
| 41 | +/// # Panics |
| 42 | +/// |
| 43 | +/// Panics if ARCGIS_API_KEY is not set in environment. |
| 44 | +pub fn create_api_key_client() -> ArcGISClient { |
| 45 | + let key = api_key().expect("ARCGIS_API_KEY not found in environment. Add to .env file"); |
| 46 | + let auth = ApiKeyAuth::new(key); |
| 47 | + ArcGISClient::new(auth) |
| 48 | +} |
| 49 | + |
| 50 | +/// Public ArcGIS Online feature service for testing (read-only). |
| 51 | +/// This is ESRI's World Cities sample service. |
| 52 | +pub const SAMPLE_FEATURE_SERVICE: &str = |
| 53 | + "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Cities/FeatureServer"; |
| 54 | + |
| 55 | +/// Rate limiting helper to be polite to the API. |
| 56 | +/// Sleeps for a short duration between requests. |
| 57 | +pub async fn rate_limit() { |
| 58 | + tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_env_loading() { |
| 67 | + load_env(); |
| 68 | + // Just verify it doesn't panic |
| 69 | + assert!(std::env::var("CLIENT_ID").is_ok() || std::env::var("ARCGIS_API_KEY").is_ok(), |
| 70 | + "Either CLIENT_ID or ARCGIS_API_KEY should be set"); |
| 71 | + } |
| 72 | +} |
0 commit comments