use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DeviceCode {
pub device_code: Option<String>,
pub user_code: Option<String>,
pub verification_url: Option<String>,
pub expires_in: Option<u64>,
pub interval: Option<u64>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OAuthToken {
pub access_token: Option<String>,
pub refresh_token: Option<String>,
pub expires_in: Option<u64>,
pub token_type: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn device_codes_arrive_in_snake_case_unlike_the_rest_of_the_api() {
let code: DeviceCode = serde_json::from_str(
r#"{"device_code":"d","user_code":"ABCD-1234","verification_url":"https://ya.ru/device","expires_in":300,"interval":5}"#,
)
.unwrap();
assert_eq!(code.device_code.as_deref(), Some("d"));
assert_eq!(code.user_code.as_deref(), Some("ABCD-1234"));
assert_eq!(code.expires_in, Some(300));
assert_eq!(code.interval, Some(5));
}
#[test]
fn tokens_arrive_in_snake_case_too() {
let token: OAuthToken = serde_json::from_str(
r#"{"access_token":"a","refresh_token":"r","expires_in":31536000,"token_type":"bearer"}"#,
)
.unwrap();
assert_eq!(token.access_token.as_deref(), Some("a"));
assert_eq!(token.refresh_token.as_deref(), Some("r"));
assert_eq!(token.token_type.as_deref(), Some("bearer"));
}
#[test]
fn device_auth_models_survive_an_empty_object() {
serde_json::from_str::<DeviceCode>("{}").unwrap();
serde_json::from_str::<OAuthToken>("{}").unwrap();
}
}