use mcp_gmailcal::config::{Config, get_token_expiry_seconds};
use mcp_gmailcal::errors::ConfigError;
use std::env;
mod helper;
#[macro_use]
mod test_macros;
fn clear_env_vars() {
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_EXPIRY_SECONDS");
env::remove_var("TOKEN_REFRESH_THRESHOLD_SECONDS");
env::remove_var("TOKEN_EXPIRY_BUFFER_SECONDS");
env::remove_var("DOTENV_PATH");
}
fn create_config(
client_id: &str,
client_secret: &str,
refresh_token: &str,
access_token: Option<&str>
) -> Config {
Config {
client_id: client_id.to_string(),
client_secret: client_secret.to_string(),
refresh_token: refresh_token.to_string(),
access_token: access_token.map(|s| s.to_string()),
token_refresh_threshold: 300, token_expiry_buffer: 60, }
}
#[test]
fn test_config_from_env_success() {
let config = create_config(
"test_client_id",
"test_client_secret",
"test_refresh_token",
Some("test_access_token")
);
assert_eq!(config.client_id, "test_client_id");
assert_eq!(config.client_secret, "test_client_secret");
assert_eq!(config.refresh_token, "test_refresh_token");
assert_eq!(config.access_token, Some("test_access_token".to_string()));
assert_eq!(config.token_refresh_threshold, 300);
assert_eq!(config.token_expiry_buffer, 60);
{
let original_client_id = env::var("GMAIL_CLIENT_ID").ok();
let original_client_secret = env::var("GMAIL_CLIENT_SECRET").ok();
let original_refresh_token = env::var("GMAIL_REFRESH_TOKEN").ok();
let original_access_token = env::var("GMAIL_ACCESS_TOKEN").ok();
env::set_var("GMAIL_CLIENT_ID", "env_client_id");
env::set_var("GMAIL_CLIENT_SECRET", "env_client_secret");
env::set_var("GMAIL_REFRESH_TOKEN", "env_refresh_token");
env::set_var("GMAIL_ACCESS_TOKEN", "env_access_token");
let _ = Config::from_env();
clear_env_vars();
}
}
#[test]
fn test_missing_client_id() {
let missing_client_id_error = ConfigError::MissingEnvVar("GMAIL_CLIENT_ID".to_string());
let missing_client_secret_error = ConfigError::MissingEnvVar("GMAIL_CLIENT_SECRET".to_string());
let missing_refresh_token_error = ConfigError::MissingEnvVar("GMAIL_REFRESH_TOKEN".to_string());
assert!(format!("{}", missing_client_id_error).contains("GMAIL_CLIENT_ID"));
assert!(format!("{}", missing_client_secret_error).contains("GMAIL_CLIENT_SECRET"));
assert!(format!("{}", missing_refresh_token_error).contains("GMAIL_REFRESH_TOKEN"));
assert!(matches!(missing_client_id_error, ConfigError::MissingEnvVar(_)));
assert!(matches!(missing_client_secret_error, ConfigError::MissingEnvVar(_)));
assert!(matches!(missing_refresh_token_error, ConfigError::MissingEnvVar(_)));
}
#[test]
fn test_missing_client_secret() {
}
#[test]
fn test_missing_refresh_token() {
}
#[test]
fn test_minimum_config() {
let config = create_config(
"min_client_id",
"min_client_secret",
"min_refresh_token",
None
);
assert_eq!(config.client_id, "min_client_id");
assert_eq!(config.client_secret, "min_client_secret");
assert_eq!(config.refresh_token, "min_refresh_token");
assert_eq!(config.access_token, None);
assert_eq!(config.token_refresh_threshold, 300);
assert_eq!(config.token_expiry_buffer, 60);
}
#[test]
fn test_full_config() {
let mut config = create_config(
"full_client_id",
"full_client_secret",
"full_refresh_token",
Some("full_access_token")
);
config.token_refresh_threshold = 500;
config.token_expiry_buffer = 120;
assert_eq!(config.client_id, "full_client_id");
assert_eq!(config.client_secret, "full_client_secret");
assert_eq!(config.refresh_token, "full_refresh_token");
assert_eq!(config.access_token, Some("full_access_token".to_string()));
assert_eq!(config.token_refresh_threshold, 500);
assert_eq!(config.token_expiry_buffer, 120);
}
#[test]
fn test_config_with_access_token() {
let config = create_config(
"access_client_id",
"access_client_secret",
"access_refresh_token",
Some("test_access_token")
);
assert!(config.access_token.is_some());
assert_eq!(config.access_token.unwrap(), "test_access_token");
}
#[test]
fn test_token_expiry_seconds() {
let original_value = env::var("TOKEN_EXPIRY_SECONDS").ok();
env::remove_var("TOKEN_EXPIRY_SECONDS");
let default_value = get_token_expiry_seconds();
assert_eq!(default_value, 3540, "Default TOKEN_EXPIRY_SECONDS should be 3540");
env::set_var("TOKEN_EXPIRY_SECONDS", "7200");
let custom_value = get_token_expiry_seconds();
assert_eq!(custom_value, 7200, "TOKEN_EXPIRY_SECONDS should reflect environment");
env::set_var("TOKEN_EXPIRY_SECONDS", "not_a_number");
let invalid_value = get_token_expiry_seconds();
assert_eq!(invalid_value, 3540, "Invalid TOKEN_EXPIRY_SECONDS should return default");
match original_value {
Some(val) => env::set_var("TOKEN_EXPIRY_SECONDS", val),
None => env::remove_var("TOKEN_EXPIRY_SECONDS"),
}
}
#[test]
fn test_config_from_dotenv_file() {
let original_dotenv_path = env::var("DOTENV_PATH").ok();
env::set_var("DOTENV_PATH", "/tmp/nonexistent_dotenv_file");
let _ = Config::from_env();
match original_dotenv_path {
Some(val) => env::set_var("DOTENV_PATH", val),
None => env::remove_var("DOTENV_PATH"),
}
}
#[test]
fn test_api_url_constants() {
use mcp_gmailcal::config::{GMAIL_API_BASE_URL, OAUTH_TOKEN_URL};
assert_eq!(GMAIL_API_BASE_URL, "https://gmail.googleapis.com/gmail/v1");
assert_eq!(OAUTH_TOKEN_URL, "https://oauth2.googleapis.com/token");
}