use mcp_gmailcal::auth::TokenManager;
use mcp_gmailcal::config::Config;
use mcp_gmailcal::token_cache::{TokenCacheConfig, TokenCache};
use std::env;
use std::path::PathBuf;
use reqwest::Client;
use tempfile::tempdir;
mod helper;
fn setup_test_env(use_cache: bool) {
env::remove_var("GMAIL_CLIENT_ID");
env::remove_var("GMAIL_CLIENT_SECRET");
env::remove_var("GMAIL_REFRESH_TOKEN");
env::remove_var("GMAIL_ACCESS_TOKEN");
env::remove_var("TOKEN_CACHE_ENABLED");
env::remove_var("TOKEN_CACHE_FILE");
env::remove_var("TOKEN_CACHE_ENCRYPTION_KEY");
if use_cache {
env::set_var("TOKEN_CACHE_ENABLED", "true");
} else {
env::set_var("TOKEN_CACHE_ENABLED", "false");
}
}
fn create_test_config_with_token() -> Config {
Config {
client_id: "test_client_id".to_string(),
client_secret: "test_client_secret".to_string(),
refresh_token: "test_refresh_token".to_string(),
access_token: Some("test_access_token".to_string()),
token_refresh_threshold: 300, token_expiry_buffer: 60, }
}
#[tokio::test]
async fn test_token_manager_with_cache_disabled() {
setup_test_env(false);
let config = create_test_config_with_token();
let mut token_manager = TokenManager::new(&config);
let client = Client::new();
let result = token_manager.get_token(&client).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "test_access_token");
}
#[tokio::test]
async fn test_token_manager_with_cache_enabled() {
let temp_dir = tempdir().unwrap();
let cache_path = temp_dir.path().join("token_cache_test.json");
setup_test_env(true);
env::set_var("TOKEN_CACHE_FILE", cache_path.to_str().unwrap());
env::set_var("TOKEN_CACHE_ENCRYPTION_KEY", "test_encryption_key_1234567890abcdef");
let config = create_test_config_with_token();
let mut token_manager = TokenManager::new(&config);
let client = Client::new();
let result = token_manager.get_token(&client).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "test_access_token");
temp_dir.close().unwrap();
}
#[tokio::test]
async fn test_token_manager_with_invalid_cache_path() {
setup_test_env(true);
if cfg!(unix) {
env::set_var("TOKEN_CACHE_FILE", "/proc/invalid_path/token_cache.json");
} else {
env::set_var("TOKEN_CACHE_FILE", "Z:\\invalid_path\\token_cache.json");
}
env::set_var("TOKEN_CACHE_ENCRYPTION_KEY", "test_encryption_key_1234567890abcdef");
let config = create_test_config_with_token();
let mut token_manager = TokenManager::new(&config);
let client = Client::new();
let result = token_manager.get_token(&client).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "test_access_token");
}
#[tokio::test]
async fn test_token_manager_with_encryption_key_env_var() {
let temp_dir = tempdir().unwrap();
let cache_path = temp_dir.path().join("token_cache_test.json");
setup_test_env(true);
env::set_var("TOKEN_CACHE_FILE", cache_path.to_str().unwrap());
let test_key = "very_secure_encryption_key_12345";
env::set_var("TOKEN_CACHE_ENCRYPTION_KEY", test_key);
let config = create_test_config_with_token();
let mut token_manager = TokenManager::new(&config);
let client = Client::new();
let result = token_manager.get_token(&client).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "test_access_token");
let mut new_token_manager = TokenManager::new(&Config {
access_token: None, ..config });
let _ = new_token_manager.get_token(&client).await;
temp_dir.close().unwrap();
}