1use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9
10use crate::Result;
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct AuthStatus {
19 pub logged_in: bool,
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub user: Option<String>,
24 #[serde(default)]
26 pub scopes: Vec<String>,
27}
28
29#[async_trait]
41pub trait AuthProvider: std::fmt::Debug + Send + Sync {
42 async fn login(&self, token: Option<&str>) -> Result<()>;
53
54 async fn logout(&self) -> Result<()>;
60
61 async fn status(&self) -> Result<AuthStatus>;
67
68 async fn token(&self) -> Result<String>;
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_should_deserialize_auth_status_logged_in() {
84 let json = r#"{
85 "loggedIn": true,
86 "user": "octocat",
87 "scopes": ["repo", "read:org"]
88 }"#;
89 let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
90
91 assert!(status.logged_in);
92 assert_eq!(status.user.as_deref(), Some("octocat"));
93 assert_eq!(
94 status.scopes,
95 vec!["repo".to_string(), "read:org".to_string()]
96 );
97 }
98
99 #[test]
100 fn test_should_deserialize_auth_status_not_logged_in() {
101 let json = r#"{
102 "loggedIn": false
103 }"#;
104 let status: AuthStatus = serde_json::from_str(json).expect("valid AuthStatus JSON");
105
106 assert!(!status.logged_in);
107 assert!(status.user.is_none());
108 assert!(status.scopes.is_empty());
109 }
110
111 #[test]
112 fn test_should_roundtrip_auth_status_via_serde() {
113 let json = r#"{
114 "loggedIn": true,
115 "user": "alice",
116 "scopes": ["repo"]
117 }"#;
118 let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
119 let re_serialized = serde_json::to_string(&status).expect("serialize");
120 let round_tripped: AuthStatus =
121 serde_json::from_str(&re_serialized).expect("re-deserialize");
122
123 assert_eq!(round_tripped.logged_in, status.logged_in);
124 assert_eq!(round_tripped.user, status.user);
125 assert_eq!(round_tripped.scopes, status.scopes);
126 }
127
128 #[test]
129 fn test_should_serialize_auth_status_camel_case() {
130 let status = AuthStatus {
131 logged_in: true,
132 user: Some("bob".into()),
133 scopes: vec!["gist".into()],
134 };
135 let json = serde_json::to_string(&status).expect("serialize");
136 assert!(json.contains("\"loggedIn\":true"));
137 assert!(json.contains("\"user\":\"bob\""));
138 assert!(json.contains("\"scopes\""));
139 assert!(!json.contains("\"logged_in\""));
140 }
141
142 #[test]
143 fn test_should_omit_none_user_on_serialize() {
144 let status = AuthStatus {
145 logged_in: false,
146 user: None,
147 scopes: vec![],
148 };
149 let json = serde_json::to_string(&status).expect("serialize");
150 assert!(!json.contains("null"));
151 assert!(!json.contains("\"user\":"));
152 assert!(json.contains("\"loggedIn\":false"));
153 }
154
155 #[test]
156 fn test_should_default_empty_scopes() {
157 let json = r#"{"loggedIn":true,"user":"u"}"#;
158 let status: AuthStatus = serde_json::from_str(json).expect("deserialize");
159 assert!(status.scopes.is_empty());
160 }
161
162 #[test]
163 fn test_should_format_debug_for_auth_status() {
164 let status = AuthStatus {
165 logged_in: true,
166 user: Some("test".into()),
167 scopes: vec!["repo".into()],
168 };
169 let debug = format!("{status:?}");
170 assert!(debug.contains("AuthStatus"));
171 assert!(debug.contains("test"));
172 }
173
174 #[test]
175 fn test_should_clone_auth_status() {
176 let original = AuthStatus {
177 logged_in: true,
178 user: Some("alice".into()),
179 scopes: vec!["repo".into(), "gist".into()],
180 };
181 let cloned = original.clone();
182
183 assert_eq!(cloned.logged_in, original.logged_in);
184 assert_eq!(cloned.user, original.user);
185 assert_eq!(cloned.scopes, original.scopes);
186
187 assert!(!std::ptr::eq(
189 std::ptr::from_ref(&cloned.scopes),
190 std::ptr::from_ref(&original.scopes)
191 ));
192 }
193}