use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::Result;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthStatus {
pub logged_in: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
#[serde(default)]
pub scopes: Vec<String>,
}
#[async_trait]
pub trait AuthProvider: std::fmt::Debug + Send + Sync {
async fn login(&self, token: Option<&str>) -> Result<()>;
async fn logout(&self) -> Result<()>;
async fn status(&self) -> Result<AuthStatus>;
async fn token(&self) -> Result<String>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_deserialize_auth_status_logged_in() {
let json = r#"{
"loggedIn": true,
"user": "octocat",
"scopes": ["repo", "read:org"]
}"#;
let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
assert!(status.logged_in);
assert_eq!(status.user.as_deref(), Some("octocat"));
assert_eq!(
status.scopes,
vec!["repo".to_string(), "read:org".to_string()]
);
}
#[test]
fn test_should_deserialize_auth_status_not_logged_in() {
let json = r#"{
"loggedIn": false
}"#;
let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
assert!(!status.logged_in);
assert!(status.user.is_none());
assert!(status.scopes.is_empty());
}
#[test]
fn test_should_roundtrip_auth_status_via_serde() {
let json = r#"{
"loggedIn": true,
"user": "alice",
"scopes": ["repo"]
}"#;
let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
let re_serialized = serde_json::to_string(&status).expect("serialize");
let round_tripped: AuthStatus =
serde_json::from_str(&re_serialized).expect("re-deserialize");
assert_eq!(round_tripped.logged_in, status.logged_in);
assert_eq!(round_tripped.user, status.user);
assert_eq!(round_tripped.scopes, status.scopes);
}
#[test]
fn test_should_serialize_auth_status_camel_case() {
let status = AuthStatus {
logged_in: true,
user: Some("bob".into()),
scopes: vec!["gist".into()],
};
let json = serde_json::to_string(&status).expect("serialize");
assert!(json.contains("\"loggedIn\":true"));
assert!(json.contains("\"user\":\"bob\""));
assert!(json.contains("\"scopes\""));
assert!(!json.contains("\"logged_in\""));
}
#[test]
fn test_should_omit_none_user_on_serialize() {
let status = AuthStatus {
logged_in: false,
user: None,
scopes: vec![],
};
let json = serde_json::to_string(&status).expect("serialize");
assert!(!json.contains("null"));
assert!(!json.contains("\"user\":"));
assert!(json.contains("\"loggedIn\":false"));
}
#[test]
fn test_should_default_empty_scopes() {
let json = r#"{"loggedIn":true,"user":"u"}"#;
let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
assert!(status.scopes.is_empty());
}
#[test]
fn test_should_format_debug_for_auth_status() {
let status = AuthStatus {
logged_in: true,
user: Some("test".into()),
scopes: vec!["repo".into()],
};
let debug = format!("{status:?}");
assert!(debug.contains("AuthStatus"));
assert!(debug.contains("test"));
}
#[test]
fn test_should_clone_auth_status() {
let original = AuthStatus {
logged_in: true,
user: Some("alice".into()),
scopes: vec!["repo".into(), "gist".into()],
};
let cloned = original.clone();
assert_eq!(cloned.logged_in, original.logged_in);
assert_eq!(cloned.user, original.user);
assert_eq!(cloned.scopes, original.scopes);
assert!(!std::ptr::eq(
std::ptr::from_ref(&cloned.scopes),
std::ptr::from_ref(&original.scopes)
));
}
}