use super::*;
use crate::oauth2::types::{OidcTokenResponse, OidcUserInfo};
use serde_json::json;
#[test]
fn test_oidc_userinfo_deserialization() {
let json_data = json!({
"sub": "123456789",
"email": "test@example.com",
"email_verified": true,
"name": "Test User",
"given_name": "Test",
"family_name": "User",
"picture": "https://example.com/pic.jpg",
"locale": "en"
});
let json_str = serde_json::to_string(&json_data)
.expect("JSON serialization should not fail for valid data");
let user_info: Result<OidcUserInfo, _> = serde_json::from_str(&json_str);
assert!(
user_info.is_ok(),
"Should successfully deserialize valid Google user info"
);
let user_info = user_info.expect("Already verified result is Ok");
assert_eq!(user_info.email, Some("test@example.com".to_string()));
assert_eq!(user_info.name, Some("Test User".to_string()));
}
#[test]
fn test_oidc_token_response_deserialization() {
let json_data = json!({
"access_token": "ya29.access_token_value",
"expires_in": 3599,
"scope": "openid email profile",
"token_type": "Bearer",
"id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE2NzAyOGE4MzI5Y2QwOTU0Y2JmYWMwNGI2MWI3OGZkYThlMzVjOGMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhdWQiOiJjbGllbnRfaWQiLCJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjoxNjA5NDYyODAwLCJpYXQiOjE2MDk0NTkyMDB9.signature"
});
let json_str = serde_json::to_string(&json_data)
.expect("JSON serialization should not fail for valid data");
let token_response: Result<OidcTokenResponse, _> = serde_json::from_str(&json_str);
assert!(
token_response.is_ok(),
"Should successfully deserialize valid OIDC token response"
);
let token_response = token_response.expect("Already verified result is Ok");
assert_eq!(token_response.access_token, "ya29.access_token_value");
assert!(token_response.id_token.is_some(), "Should have id_token");
}
#[test]
fn test_oidc_token_response_missing_id_token() {
let json_data = json!({
"access_token": "ya29.access_token_value",
"expires_in": 3599,
"scope": "openid email profile",
"token_type": "Bearer"
});
let json_str = serde_json::to_string(&json_data)
.expect("JSON serialization should not fail for valid data");
let token_response: Result<OidcTokenResponse, _> = serde_json::from_str(&json_str);
assert!(
token_response.is_ok(),
"Should successfully deserialize token response without id_token"
);
let token_response = token_response.expect("Already verified result is Ok");
assert_eq!(token_response.access_token, "ya29.access_token_value");
assert!(
token_response.id_token.is_none(),
"Should not have id_token"
);
}
#[test]
fn test_oidc_token_response_missing_expires_in() {
let json_data = json!({
"access_token": "ya29.access_token_value",
"scope": "openid email profile",
"token_type": "Bearer",
"id_token": "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20ifQ.signature"
});
let json_str = serde_json::to_string(&json_data)
.expect("JSON serialization should not fail for valid data");
let token_response: Result<OidcTokenResponse, _> = serde_json::from_str(&json_str);
assert!(
token_response.is_ok(),
"Should successfully deserialize token response without expires_in"
);
let token_response = token_response.expect("Already verified result is Ok");
assert_eq!(token_response.access_token, "ya29.access_token_value");
assert!(
token_response.id_token.is_some(),
"id_token should still be present"
);
}
#[test]
fn test_oidc_userinfo_deserialization_missing_required_fields() {
let json_data = json!({
"id": "123456789",
"verified_email": true,
"picture": "https://example.com/pic.jpg"
});
let json_str = serde_json::to_string(&json_data).expect("JSON serialization should not fail");
let user_info: Result<OidcUserInfo, _> = serde_json::from_str(&json_str);
assert!(
user_info.is_err(),
"Should fail to deserialize when required fields are missing"
);
}
#[test]
fn test_oidc_userinfo_deserialization_invalid_json() {
let invalid_json = r#"{"id": "123", "email":}"#;
let user_info: Result<OidcUserInfo, _> = serde_json::from_str(invalid_json);
assert!(
user_info.is_err(),
"Should fail to deserialize malformed JSON"
);
}
#[test]
fn test_oidc_token_response_missing_access_token() {
let json_data = json!({
"expires_in": 3599,
"scope": "openid email profile",
"token_type": "Bearer",
"id_token": "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20ifQ.signature"
});
let json_str = serde_json::to_string(&json_data).expect("JSON serialization should not fail");
let token_response: Result<OidcTokenResponse, _> = serde_json::from_str(&json_str);
assert!(
token_response.is_err(),
"Should fail to deserialize when access_token is missing"
);
}
#[test]
fn test_oidc_token_response_invalid_json() {
let invalid_json = r#"{"access_token": "token", "expires_in":}"#;
let token_response: Result<OidcTokenResponse, _> = serde_json::from_str(invalid_json);
assert!(
token_response.is_err(),
"Should fail to deserialize malformed JSON"
);
}
#[test]
fn test_id_token_validation_logic() {
let missing_id_token: Option<String> = None;
let result = missing_id_token
.ok_or_else(|| OAuth2Error::TokenExchange("ID token not present in response".to_string()));
assert!(
result.is_err(),
"Should return error when id_token is missing"
);
match result {
Err(OAuth2Error::TokenExchange(msg)) => {
assert_eq!(msg, "ID token not present in response");
}
_ => panic!("Expected TokenExchange error with specific message"),
}
let present_id_token: Option<String> = Some(
"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20ifQ.signature"
.to_string(),
);
let result = present_id_token
.ok_or_else(|| OAuth2Error::TokenExchange("ID token not present in response".to_string()));
assert!(result.is_ok(), "Should succeed when id_token is present");
let id_token = result.expect("Already verified result is Ok");
assert_eq!(
id_token,
"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20ifQ.signature"
);
}