use crate::passkey::types::PublicKeyCredentialUserEntity;
use crate::passkey::types::StoredOptions;
use crate::storage::CacheData;
use crate::test_utils::init_test_environment;
#[test]
fn test_stored_options_cache_data_conversion() {
let options = StoredOptions {
challenge: "test_challenge".to_string(),
user: PublicKeyCredentialUserEntity {
user_handle: "test_user_handle".to_string(),
name: "test_user".to_string(),
display_name: "Test User".to_string(),
},
timestamp: 1622505600, ttl: 300, };
let cache_data: CacheData = options.clone().into();
assert!(!cache_data.value.is_empty());
assert!(cache_data.value.contains("test_challenge"));
assert!(cache_data.value.contains("test_user_handle"));
let converted_options: Result<StoredOptions, _> = cache_data.try_into();
assert!(converted_options.is_ok());
let converted = converted_options.unwrap();
assert_eq!(converted.challenge, options.challenge);
assert_eq!(converted.user.user_handle, options.user.user_handle);
assert_eq!(converted.timestamp, options.timestamp);
assert_eq!(converted.ttl, options.ttl);
}
#[test]
fn test_stored_options_cache_data_conversion_edge_cases() {
let options = StoredOptions {
challenge: "".to_string(), user: PublicKeyCredentialUserEntity {
user_handle: "user@example.com".to_string(), name: "".to_string(), display_name: "User with Special Chars: !@#$%^&*()".to_string(), },
timestamp: 0, ttl: 1, };
let cache_data: CacheData = options.clone().into();
let converted_options: Result<StoredOptions, _> = cache_data.try_into();
assert!(converted_options.is_ok());
let converted = converted_options.unwrap();
assert_eq!(converted.challenge, options.challenge);
assert_eq!(converted.user.user_handle, options.user.user_handle);
assert_eq!(converted.user.name, options.user.name);
assert_eq!(converted.user.display_name, options.user.display_name);
assert_eq!(converted.timestamp, options.timestamp);
assert_eq!(converted.ttl, options.ttl);
}
#[tokio::test]
async fn test_cache_operations() {
init_test_environment().await;
let category = "test_category";
let key = "test_key";
let options = StoredOptions {
challenge: "cache_test_challenge".to_string(),
user: PublicKeyCredentialUserEntity {
user_handle: "cache_user_handle".to_string(),
name: "cache_user".to_string(),
display_name: "Cache Test User".to_string(),
},
timestamp: 1622505600,
ttl: 300,
};
use crate::storage::{CacheKey, CachePrefix, get_data, remove_data, store_cache_keyed};
let cache_prefix =
CachePrefix::new(category.to_string()).expect("Failed to create cache prefix");
let cache_key = CacheKey::new(key.to_string()).expect("Failed to create cache key");
let store_result = store_cache_keyed::<_, crate::passkey::PasskeyError>(
cache_prefix,
cache_key,
options.clone(),
300,
)
.await;
assert!(store_result.is_ok());
let cache_prefix = CachePrefix::new(category.to_string()).unwrap();
let cache_key = CacheKey::new(key.to_string()).unwrap();
let retrieved: Result<Option<StoredOptions>, _> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix.clone(), cache_key.clone()).await;
assert!(retrieved.is_ok());
let retrieved_options = retrieved.unwrap();
assert!(retrieved_options.is_some());
let retrieved_data = retrieved_options.unwrap();
assert_eq!(retrieved_data.challenge, options.challenge);
assert_eq!(retrieved_data.user.user_handle, options.user.user_handle);
assert_eq!(retrieved_data.timestamp, options.timestamp);
let remove_result = remove_data::<crate::passkey::PasskeyError>(cache_prefix, cache_key).await;
assert!(remove_result.is_ok());
let cache_prefix2 = CachePrefix::new(category.to_string()).unwrap();
let cache_key2 = CacheKey::new(key.to_string()).unwrap();
let after_remove: Result<Option<StoredOptions>, _> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix2, cache_key2).await;
assert!(after_remove.is_ok());
assert!(after_remove.unwrap().is_none());
}
#[tokio::test]
async fn test_cache_operations_different_keys() {
init_test_environment().await;
let category = "isolation_test";
let key1 = "key1";
let key2 = "key2";
let data1 = StoredOptions {
challenge: "challenge1".to_string(),
user: PublicKeyCredentialUserEntity {
user_handle: "user1".to_string(),
name: "user1".to_string(),
display_name: "User 1".to_string(),
},
timestamp: 1111,
ttl: 100,
};
let data2 = StoredOptions {
challenge: "challenge2".to_string(),
user: PublicKeyCredentialUserEntity {
user_handle: "user2".to_string(),
name: "user2".to_string(),
display_name: "User 2".to_string(),
},
timestamp: 2222,
ttl: 200,
};
use crate::storage::{CacheKey, CachePrefix, get_data, store_cache_keyed};
let cache_prefix1 =
CachePrefix::new(category.to_string()).expect("Failed to create cache prefix");
let cache_key1 = CacheKey::new(key1.to_string()).expect("Failed to create cache key");
let _ = store_cache_keyed::<_, crate::passkey::PasskeyError>(
cache_prefix1,
cache_key1,
data1.clone(),
100,
)
.await;
let cache_prefix2 =
CachePrefix::new(category.to_string()).expect("Failed to create cache prefix");
let cache_key2 = CacheKey::new(key2.to_string()).expect("Failed to create cache key");
let _ = store_cache_keyed::<_, crate::passkey::PasskeyError>(
cache_prefix2,
cache_key2,
data2.clone(),
200,
)
.await;
let (cache_prefix1, cache_key1) = (
CachePrefix::new(category.to_string()).unwrap(),
CacheKey::new(key1.to_string()).unwrap(),
);
let cache_prefix2 = CachePrefix::new(category.to_string()).unwrap();
let cache_key2 = CacheKey::new(key2.to_string()).unwrap();
let retrieved1: Option<StoredOptions> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix1, cache_key1)
.await
.unwrap();
let retrieved2: Option<StoredOptions> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix2, cache_key2)
.await
.unwrap();
assert!(retrieved1.is_some());
assert!(retrieved2.is_some());
let data_from_key1 = retrieved1.unwrap();
let data_from_key2 = retrieved2.unwrap();
assert_eq!(data_from_key1.challenge, "challenge1");
assert_eq!(data_from_key2.challenge, "challenge2");
assert_eq!(data_from_key1.timestamp, 1111);
assert_eq!(data_from_key2.timestamp, 2222);
}
#[tokio::test]
async fn test_comprehensive_cache_operations() {
init_test_environment().await;
let category = "comprehensive_test";
let key = "comprehensive_key";
let options = StoredOptions {
challenge: "comprehensive_challenge".to_string(),
user: PublicKeyCredentialUserEntity {
user_handle: "comprehensive_user_handle".to_string(),
name: "comprehensive_user".to_string(),
display_name: "Comprehensive Test User".to_string(),
},
timestamp: 1622505600,
ttl: 300,
};
use crate::storage::{CacheKey, CachePrefix, get_data, remove_data, store_cache_keyed};
let cache_prefix =
CachePrefix::new(category.to_string()).expect("Failed to create cache prefix");
let cache_key = CacheKey::new(key.to_string()).expect("Failed to create cache key");
let store_result = store_cache_keyed::<_, crate::passkey::PasskeyError>(
cache_prefix,
cache_key,
options.clone(),
300,
)
.await;
assert!(store_result.is_ok());
let cache_prefix = CachePrefix::new(category.to_string()).unwrap();
let cache_key = CacheKey::new(key.to_string()).unwrap();
let retrieved: Result<Option<StoredOptions>, _> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix.clone(), cache_key.clone()).await;
assert!(retrieved.is_ok());
let retrieved_options = retrieved.unwrap();
assert!(retrieved_options.is_some());
let retrieved_data = retrieved_options.unwrap();
assert_eq!(retrieved_data.challenge, options.challenge);
assert_eq!(retrieved_data.user.user_handle, options.user.user_handle);
assert_eq!(retrieved_data.timestamp, options.timestamp);
let remove_result = remove_data::<crate::passkey::PasskeyError>(cache_prefix, cache_key).await;
assert!(remove_result.is_ok());
let cache_prefix2 = CachePrefix::new(category.to_string()).unwrap();
let cache_key2 = CacheKey::new(key.to_string()).unwrap();
let after_remove: Result<Option<StoredOptions>, _> =
get_data::<_, crate::passkey::PasskeyError>(cache_prefix2, cache_key2).await;
assert!(after_remove.is_ok());
assert!(after_remove.unwrap().is_none());
}