use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserProfile {
#[serde(alias = "id")]
pub user_id: String,
pub username: String,
pub display_name: String,
pub role: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupabaseConfig {
pub url: String,
#[serde(alias = "anonKey")]
pub anon_key: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthSession {
pub user: UserProfile,
pub supabase: SupabaseConfig,
pub key_name: String,
#[serde(default)]
pub api_key: String,
pub backend: String,
pub authenticated_at: DateTime<Utc>,
}
impl AuthSession {
pub fn is_expired(&self) -> bool {
false
}
}
#[derive(Debug, Serialize)]
pub struct AuthRequest {
#[serde(rename = "apiKey")]
pub api_key: String,
}
#[derive(Debug, Deserialize)]
pub struct AuthResponse {
pub user: UserProfile,
pub supabase: SupabaseConfig,
#[serde(rename = "keyName")]
pub key_name: String,
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_session() -> AuthSession {
AuthSession {
user: UserProfile {
user_id: "user123".to_string(),
username: "testuser".to_string(),
display_name: "Test User".to_string(),
role: "user".to_string(),
},
supabase: SupabaseConfig {
url: "https://test.supabase.co".to_string(),
anon_key: "test-anon-key".to_string(),
},
key_name: "test_key".to_string(),
api_key: "bw_dev_12345678901234567890123456789012".to_string(),
backend: "https://backend.test".to_string(),
authenticated_at: Utc::now(),
}
}
#[test]
fn test_user_profile_creation() {
let profile = UserProfile {
user_id: "123".to_string(),
username: "user".to_string(),
display_name: "User Name".to_string(),
role: "admin".to_string(),
};
assert_eq!(profile.user_id, "123");
assert_eq!(profile.username, "user");
assert_eq!(profile.display_name, "User Name");
assert_eq!(profile.role, "admin");
}
#[test]
fn test_supabase_config() {
let config = SupabaseConfig {
url: "https://example.supabase.co".to_string(),
anon_key: "anon-key".to_string(),
};
assert_eq!(config.url, "https://example.supabase.co");
assert_eq!(config.anon_key, "anon-key");
}
#[test]
fn test_auth_session_never_expires() {
let session = create_test_session();
assert!(!session.is_expired());
let mut old_session = create_test_session();
old_session.authenticated_at = Utc::now() - chrono::Duration::days(365);
assert!(!old_session.is_expired());
}
#[test]
fn test_auth_request_serialization() {
let request = AuthRequest {
api_key: "test-api-key".to_string(),
};
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("apiKey"));
assert!(json.contains("test-api-key"));
}
#[test]
fn test_auth_response_deserialization() {
let json = r#"{
"user": {
"user_id": "123",
"username": "testuser",
"display_name": "Test User",
"role": "user"
},
"providers": {
"openai_api_key": "key1"
},
"supabase": {
"url": "https://test.supabase.co",
"anon_key": "anon"
},
"keyName": "test_key"
}"#;
let response: AuthResponse = serde_json::from_str(json).unwrap();
assert_eq!(response.user.user_id, "123");
assert_eq!(response.key_name, "test_key");
}
#[test]
fn test_session_serialization_roundtrip() {
let session = create_test_session();
let json = serde_json::to_string(&session).unwrap();
let deserialized: AuthSession = serde_json::from_str(&json).unwrap();
assert_eq!(session.user.user_id, deserialized.user.user_id);
assert_eq!(session.key_name, deserialized.key_name);
}
}