use proptest::prelude::*;
use secure_identity::session::{InMemorySessionManager, SessionManager};
use security_core::{identity::AuthenticatedIdentity, types::ActorId};
use std::collections::HashMap;
use time::OffsetDateTime;
use tokio::runtime::Runtime;
use uuid::Uuid;
fn make_identity(subject: &str) -> AuthenticatedIdentity {
let _ = subject; AuthenticatedIdentity {
actor_id: ActorId::from(Uuid::new_v4()),
tenant_id: None,
roles: vec!["user".to_string()],
attributes: HashMap::new(),
authenticated_at: OffsetDateTime::now_utc(),
}
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(64))]
#[test]
fn prop_session_ids_unique(
subject1 in "[a-z]{3,10}",
subject2 in "[a-z]{3,10}",
) {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let mgr = InMemorySessionManager::new();
let id1 = mgr.create_session(&make_identity(&subject1), 300).await.unwrap().id;
let id2 = mgr.create_session(&make_identity(&subject2), 300).await.unwrap().id;
prop_assert_ne!(id1, id2);
Ok(())
})?;
}
#[test]
fn prop_short_ttl_session_created_successfully(
subject in "[a-z]{3,10}",
) {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let mgr = InMemorySessionManager::new();
let session = mgr.create_session(&make_identity(&subject), 1).await;
prop_assert!(session.is_ok(), "session creation should succeed");
Ok(())
})?;
}
#[test]
fn prop_session_valid_before_expiry(subject in "[a-z]{3,10}") {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let mgr = InMemorySessionManager::new();
let session = mgr.create_session(&make_identity(&subject), 300).await.unwrap();
let result = mgr.validate_session(&session.id).await;
prop_assert!(result.is_ok(), "session should be valid before expiry");
Ok(())
})?;
}
}