2025-06-08 09:19:04 -04:00
|
|
|
//! Integration tests for authentication
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2025-06-08 16:49:35 -04:00
|
|
|
use krust::auth::{resolve_auth, AuthConfig};
|
2025-06-08 22:16:04 -04:00
|
|
|
use krust::registry::RegistryAuth;
|
2025-06-08 09:19:04 -04:00
|
|
|
use std::fs;
|
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_auth_integration_with_docker_config() -> Result<()> {
|
|
|
|
|
// Create a temporary directory for Docker config
|
|
|
|
|
let temp_dir = TempDir::new()?;
|
|
|
|
|
let config_dir = temp_dir.path().join(".docker");
|
|
|
|
|
fs::create_dir_all(&config_dir)?;
|
|
|
|
|
|
|
|
|
|
// Create a test Docker config
|
|
|
|
|
let config_content = r#"{
|
|
|
|
|
"auths": {
|
|
|
|
|
"ghcr.io": {
|
|
|
|
|
"auth": "dGVzdDp0ZXN0MTIz"
|
|
|
|
|
},
|
|
|
|
|
"docker.io": {
|
|
|
|
|
"username": "testuser",
|
|
|
|
|
"password": "testpass"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
|
|
fs::write(config_dir.join("config.json"), config_content)?;
|
|
|
|
|
|
|
|
|
|
// Set HOME to temp directory
|
|
|
|
|
std::env::set_var("HOME", temp_dir.path());
|
|
|
|
|
|
2025-10-15 08:51:46 -04:00
|
|
|
// Test GitHub Container Registry auth (should resolve from config)
|
2025-06-08 16:49:35 -04:00
|
|
|
let ghcr_auth = resolve_auth("ghcr.io/user/image:tag")?;
|
2025-10-15 08:51:46 -04:00
|
|
|
match ghcr_auth {
|
|
|
|
|
RegistryAuth::Basic { username, password } => {
|
|
|
|
|
assert_eq!(username, "test");
|
|
|
|
|
assert_eq!(password, "test123");
|
|
|
|
|
}
|
|
|
|
|
_ => panic!("Expected Basic auth for ghcr.io, got: {:?}", ghcr_auth),
|
|
|
|
|
}
|
2025-06-08 09:19:04 -04:00
|
|
|
|
2025-10-15 08:51:46 -04:00
|
|
|
// Test Docker Hub auth (should resolve from config)
|
2025-06-08 16:49:35 -04:00
|
|
|
let docker_auth = resolve_auth("docker.io/library/ubuntu:latest")?;
|
2025-10-15 08:51:46 -04:00
|
|
|
match docker_auth {
|
|
|
|
|
RegistryAuth::Basic { username, password } => {
|
|
|
|
|
assert_eq!(username, "testuser");
|
|
|
|
|
assert_eq!(password, "testpass");
|
|
|
|
|
}
|
|
|
|
|
_ => panic!("Expected Basic auth for docker.io, got: {:?}", docker_auth),
|
|
|
|
|
}
|
2025-06-08 09:19:04 -04:00
|
|
|
|
|
|
|
|
// Test unknown registry returns anonymous
|
2025-06-08 16:49:35 -04:00
|
|
|
let unknown_auth = resolve_auth("unknown.registry.io/image:tag")?;
|
|
|
|
|
assert!(matches!(unknown_auth, RegistryAuth::Anonymous));
|
2025-06-08 09:19:04 -04:00
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
std::env::remove_var("HOME");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_auth_config_creation() {
|
|
|
|
|
// Test anonymous
|
|
|
|
|
let anon = AuthConfig::anonymous();
|
|
|
|
|
assert!(anon.is_anonymous());
|
|
|
|
|
|
|
|
|
|
// Test basic auth
|
|
|
|
|
let basic = AuthConfig::new("user".to_string(), "pass".to_string());
|
|
|
|
|
assert!(!basic.is_anonymous());
|
|
|
|
|
assert_eq!(basic.username, Some("user".to_string()));
|
|
|
|
|
assert_eq!(basic.password, Some("pass".to_string()));
|
|
|
|
|
|
|
|
|
|
// Test auth header generation
|
|
|
|
|
let header = basic.to_authorization_header().unwrap();
|
|
|
|
|
assert!(header.is_some());
|
|
|
|
|
let header_val = header.unwrap();
|
|
|
|
|
assert!(header_val.starts_with("Basic "));
|
|
|
|
|
}
|