use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
#[serde(rename = "ztAPI")]
pub zt_api: String,
pub id: String,
pub cert: String,
pub key: String,
pub ca: String,
}
impl Config {
pub fn new(zt_api: String, id: String, cert: String, key: String, ca: String) -> Self {
Self {
zt_api,
id,
cert,
key,
ca,
}
}
pub fn cert_path(&self) -> PathBuf {
PathBuf::from(&self.cert)
}
pub fn key_path(&self) -> PathBuf {
PathBuf::from(&self.key)
}
pub fn ca_path(&self) -> PathBuf {
PathBuf::from(&self.ca)
}
}
#[derive(Debug, Clone)]
pub struct IdentityConfig {
pub id: String,
pub cert: String,
pub key: String,
pub ca: String,
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_config_new() {
let config = Config::new(
"https://controller.example.com".to_string(),
"test-id".to_string(),
"/path/to/cert.pem".to_string(),
"/path/to/key.pem".to_string(),
"/path/to/ca.pem".to_string(),
);
assert_eq!(config.zt_api, "https://controller.example.com");
assert_eq!(config.id, "test-id");
assert_eq!(config.cert, "/path/to/cert.pem");
assert_eq!(config.key, "/path/to/key.pem");
assert_eq!(config.ca, "/path/to/ca.pem");
}
#[test]
fn test_config_path_methods() {
let config = Config::new(
"https://controller.example.com".to_string(),
"test-id".to_string(),
"cert.pem".to_string(),
"key.pem".to_string(),
"ca.pem".to_string(),
);
assert_eq!(config.cert_path(), PathBuf::from("cert.pem"));
assert_eq!(config.key_path(), PathBuf::from("key.pem"));
assert_eq!(config.ca_path(), PathBuf::from("ca.pem"));
}
#[test]
fn test_config_serialization() {
let config = Config::new(
"https://controller.example.com".to_string(),
"test-id".to_string(),
"cert.pem".to_string(),
"key.pem".to_string(),
"ca.pem".to_string(),
);
let json = serde_json::to_string(&config).unwrap();
let deserialized: Config = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.zt_api, config.zt_api);
assert_eq!(deserialized.id, config.id);
assert_eq!(deserialized.cert, config.cert);
assert_eq!(deserialized.key, config.key);
assert_eq!(deserialized.ca, config.ca);
}
#[test]
fn test_config_with_zt_api_field_name() {
let json = r#"{
"ztAPI": "https://controller.example.com",
"id": "test-id",
"cert": "cert.pem",
"key": "key.pem",
"ca": "ca.pem"
}"#;
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(config.zt_api, "https://controller.example.com");
}
#[test]
fn test_legacy_identity_config() {
let legacy_config = IdentityConfig {
id: "legacy-id".to_string(),
cert: "legacy-cert.pem".to_string(),
key: "legacy-key.pem".to_string(),
ca: "legacy-ca.pem".to_string(),
};
assert_eq!(legacy_config.id, "legacy-id");
assert_eq!(legacy_config.cert, "legacy-cert.pem");
assert_eq!(legacy_config.key, "legacy-key.pem");
assert_eq!(legacy_config.ca, "legacy-ca.pem");
}
#[test]
fn test_config_clone() {
let original = Config::new(
"https://controller.example.com".to_string(),
"test-id".to_string(),
"cert.pem".to_string(),
"key.pem".to_string(),
"ca.pem".to_string(),
);
let cloned = original.clone();
assert_eq!(cloned.zt_api, original.zt_api);
assert_eq!(cloned.id, original.id);
assert_eq!(cloned.cert, original.cert);
assert_eq!(cloned.key, original.key);
assert_eq!(cloned.ca, original.ca);
}
}