reinhardt_auth/
repository.rs1use crate::core::AuthIdentity;
7use crate::internal_user::InternalUser;
8use async_trait::async_trait;
9use uuid::Uuid;
10
11#[async_trait]
32pub trait UserRepository: Send + Sync {
33 async fn get_user_by_id(&self, user_id: &str) -> Result<Option<Box<dyn AuthIdentity>>, String>;
37}
38
39pub struct SimpleUserRepository;
57
58#[async_trait]
59impl UserRepository for SimpleUserRepository {
60 async fn get_user_by_id(&self, user_id: &str) -> Result<Option<Box<dyn AuthIdentity>>, String> {
61 Ok(Some(Box::new(InternalUser {
66 id: Uuid::new_v5(&Uuid::NAMESPACE_URL, user_id.as_bytes()),
67 username: user_id.to_string(),
68 email: String::new(),
69 is_active: true,
70 is_admin: false,
71 is_staff: false,
72 is_superuser: false,
73 })))
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use rstest::rstest;
81
82 #[rstest]
83 #[tokio::test]
84 async fn test_simple_user_repo_returns_user() {
85 let repo = SimpleUserRepository;
87 let user_id = "test_user_42";
88 let expected_uuid = Uuid::new_v5(&Uuid::NAMESPACE_URL, user_id.as_bytes());
89
90 let result = repo.get_user_by_id(user_id).await;
92
93 let user = result
95 .expect("get_user_by_id should not return Err")
96 .expect("get_user_by_id should return Some for any input");
97 assert_eq!(user.id(), expected_uuid.to_string());
98 assert!(user.is_authenticated());
99 }
100
101 #[rstest]
102 #[tokio::test]
103 async fn test_simple_user_repo_deterministic_uuid() {
104 let repo = SimpleUserRepository;
106 let user_id = "deterministic_id_input";
107 let expected_uuid = Uuid::new_v5(&Uuid::NAMESPACE_URL, user_id.as_bytes());
108
109 let first_result = repo.get_user_by_id(user_id).await;
111 let second_result = repo.get_user_by_id(user_id).await;
112
113 let first_user = first_result.unwrap().unwrap();
115 let second_user = second_result.unwrap().unwrap();
116 assert_eq!(first_user.id(), expected_uuid.to_string());
117 assert_eq!(first_user.id(), second_user.id());
118 }
119}