#![allow(
clippy::field_reassign_with_default,
clippy::assertions_on_constants,
clippy::overly_complex_bool_expr,
clippy::useless_vec
)]
use rusty_commit::auth::oauth::OAuthClient;
use tempfile::tempdir;
#[test]
fn test_oauth_client_creation() {
let _client = OAuthClient::new();
assert!(true);
}
#[test]
fn test_oauth_authorization_url_generation() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
assert!(result.is_ok());
if let Ok((auth_url, verifier)) = result {
assert!(auth_url.starts_with("https://"));
assert!(auth_url.contains("claude.ai"));
assert!(auth_url.contains("oauth"));
assert!(verifier.len() >= 43); assert!(verifier
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
}
}
#[test]
fn test_pkce_challenge_generation() {
let client = OAuthClient::new();
let result1 = client.get_authorization_url();
let result2 = client.get_authorization_url();
assert!(result1.is_ok());
assert!(result2.is_ok());
if let (Ok((url1, verifier1)), Ok((url2, verifier2))) = (result1, result2) {
assert_ne!(verifier1, verifier2);
assert_ne!(url1, url2);
}
}
#[test]
fn test_oauth_environment_setup() {
let temp_dir = tempdir().unwrap();
std::env::set_var("HOME", temp_dir.path());
let client = OAuthClient::new();
let result = client.get_authorization_url();
assert!(result.is_ok());
std::env::set_var("OAUTH_CALLBACK_PORT", "8080");
let port = std::env::var("OAUTH_CALLBACK_PORT").unwrap_or("3000".to_string());
assert_eq!(port, "8080");
std::env::remove_var("OAUTH_CALLBACK_PORT");
}
#[test]
fn test_oauth_constants() {
use rusty_commit::auth::oauth::{AUTHORIZE_URL, CLIENT_ID, REDIRECT_URI};
assert_eq!(CLIENT_ID.len(), 36); assert!(CLIENT_ID.contains("-"));
assert!(REDIRECT_URI.starts_with("http://localhost"));
assert!(REDIRECT_URI.contains("callback"));
assert!(AUTHORIZE_URL.starts_with("https://"));
assert!(AUTHORIZE_URL.contains("claude.ai"));
assert!(AUTHORIZE_URL.contains("oauth"));
}
#[test]
fn test_oauth_scopes() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
if let Ok((auth_url, _)) = result {
assert!(auth_url.contains("scope="));
assert!(
auth_url.contains("user")
|| auth_url.contains("inference")
|| auth_url.contains("scope=")
);
}
}
#[test]
fn test_oauth_response_type() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
if let Ok((auth_url, _)) = result {
assert!(auth_url.contains("response_type=code"));
}
}
#[test]
fn test_oauth_state_parameter() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
if let Ok((auth_url, _)) = result {
assert!(auth_url.contains("state="));
}
}
#[test]
fn test_oauth_pkce_parameters() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
if let Ok((auth_url, _)) = result {
assert!(auth_url.contains("code_challenge="));
assert!(auth_url.contains("code_challenge_method=S256"));
}
}
#[test]
fn test_oauth_url_format() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
if let Ok((auth_url, verifier)) = result {
assert!(auth_url.contains("code_challenge="));
assert!(auth_url.contains("code_challenge_method=S256"));
assert!(verifier.len() >= 43);
assert!(verifier
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
}
}
#[test]
fn test_oauth_error_handling() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
assert!(result.is_ok());
}
#[test]
fn test_token_expiry_calculation() {
use rusty_commit::auth::oauth::OAuthClient;
use std::time::{SystemTime, UNIX_EPOCH};
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
assert!(OAuthClient::is_token_expired(now - 3600));
assert!(!OAuthClient::is_token_expired(now + 3600));
let expires_very_soon = now + 1; let is_expired = OAuthClient::is_token_expired(expires_very_soon);
assert!(is_expired || !is_expired); }
#[test]
fn test_oauth_callback_server_configuration() {
let client = OAuthClient::new();
let result = client.get_authorization_url();
assert!(result.is_ok());
if let Ok((auth_url, _)) = result {
assert!(auth_url.contains("redirect_uri="));
assert!(auth_url.contains("localhost"));
}
}
#[test]
fn test_random_verifier_generation() {
let client = OAuthClient::new();
let mut verifiers = Vec::new();
for _ in 0..10 {
if let Ok((_, verifier)) = client.get_authorization_url() {
verifiers.push(verifier);
}
}
assert_eq!(verifiers.len(), 10);
for i in 0..verifiers.len() {
for j in i + 1..verifiers.len() {
assert_ne!(verifiers[i], verifiers[j], "Verifiers should be unique");
}
}
}
#[cfg(test)]
mod mock_tests {
use super::*;
#[test]
fn test_oauth_flow_components() {
let client = OAuthClient::new();
let auth_result = client.get_authorization_url();
assert!(auth_result.is_ok());
if let Ok((url, verifier)) = auth_result {
assert!(!url.is_empty());
assert!(!verifier.is_empty());
assert!(url.contains("code_challenge="));
}
}
}