use std::collections::BTreeMap;
use crate::{SessionId, TenantId, UserId};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HumanPrincipal {
pub user_id: UserId,
pub tenant_id: TenantId,
pub session_id: Option<SessionId>,
pub attributes: BTreeMap<String, serde_json::Value>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn human_principal_constructs_with_minimal_fields() {
let h = HumanPrincipal {
user_id: UserId::from_bytes([3u8; 16]),
tenant_id: TenantId::from_bytes([4u8; 16]),
session_id: None,
attributes: BTreeMap::new(),
};
assert!(h.session_id.is_none());
assert!(h.attributes.is_empty());
}
#[cfg(feature = "serde")]
#[test]
fn human_principal_serde_round_trip() {
let h = HumanPrincipal {
user_id: UserId::from_bytes([5u8; 16]),
tenant_id: TenantId::from_bytes([6u8; 16]),
session_id: Some(SessionId::from_bytes([7u8; 16])),
attributes: BTreeMap::new(),
};
let json = serde_json::to_string(&h).unwrap();
let back: HumanPrincipal = serde_json::from_str(&json).unwrap();
assert_eq!(h, back);
}
}