From d2617e08a790a07f5a0532a7aa6515e78c524c86 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Thu, 26 Aug 2021 10:00:49 +0200 Subject: [PATCH] Simplify get_user_groups to take a &str --- src/domain/handler.rs | 4 ++-- src/domain/sql_backend_handler.rs | 14 ++++---------- src/infra/auth_service.rs | 4 ++-- src/infra/graphql/query.rs | 4 ++-- src/infra/tcp_backend_handler.rs | 2 +- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/domain/handler.rs b/src/domain/handler.rs index 0c24432..953949d 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -18,7 +18,7 @@ pub trait BackendHandler: Clone + Send { async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn create_group(&self, request: CreateGroupRequest) -> Result; async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; - async fn get_user_groups(&self, user: String) -> Result>; + async fn get_user_groups(&self, user: &str) -> Result>; } #[cfg(test)] @@ -35,7 +35,7 @@ mockall::mock! { async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn create_group(&self, request: CreateGroupRequest) -> Result; - async fn get_user_groups(&self, user: String) -> Result>; + async fn get_user_groups(&self, user: &str) -> Result>; async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; } #[async_trait] diff --git a/src/domain/sql_backend_handler.rs b/src/domain/sql_backend_handler.rs index cc65dbb..d037503 100644 --- a/src/domain/sql_backend_handler.rs +++ b/src/domain/sql_backend_handler.rs @@ -135,7 +135,7 @@ impl BackendHandler for SqlBackendHandler { .await?) } - async fn get_user_groups(&self, user: String) -> Result> { + async fn get_user_groups(&self, user: &str) -> Result> { if user == self.config.ldap_user_dn { let mut groups = HashSet::new(); groups.insert("lldap_admin".to_string()); @@ -510,19 +510,13 @@ mod tests { let mut patrick_groups = HashSet::new(); patrick_groups.insert("Group1".to_string()); patrick_groups.insert("Group2".to_string()); + assert_eq!(handler.get_user_groups("bob").await.unwrap(), bob_groups); assert_eq!( - handler.get_user_groups("bob".to_string()).await.unwrap(), - bob_groups - ); - assert_eq!( - handler - .get_user_groups("patrick".to_string()) - .await - .unwrap(), + handler.get_user_groups("patrick").await.unwrap(), patrick_groups ); assert_eq!( - handler.get_user_groups("John".to_string()).await.unwrap(), + handler.get_user_groups("John").await.unwrap(), HashSet::new() ); } diff --git a/src/infra/auth_service.rs b/src/infra/auth_service.rs index 83af89e..8f76b14 100644 --- a/src/infra/auth_service.rs +++ b/src/infra/auth_service.rs @@ -89,7 +89,7 @@ where match res_found { Ok(found) => { if found { - backend_handler.get_user_groups(user.to_string()).await + backend_handler.get_user_groups(&user).await } else { Err(DomainError::AuthenticationError( "Invalid refresh token".to_string(), @@ -191,7 +191,7 @@ where // The authentication was successful, we need to fetch the groups to create the JWT // token. data.backend_handler - .get_user_groups(name.to_string()) + .get_user_groups(name) .and_then(|g| async { Ok((g, data.backend_handler.create_refresh_token(name).await?)) }) .await .map(|(groups, (refresh_token, max_age))| { diff --git a/src/infra/graphql/query.rs b/src/infra/graphql/query.rs index a2c66ee..1565904 100644 --- a/src/infra/graphql/query.rs +++ b/src/infra/graphql/query.rs @@ -149,7 +149,7 @@ impl User { async fn groups(&self, context: &Context) -> FieldResult>> { Ok(context .handler - .get_user_groups(self.user.user_id.clone()) + .get_user_groups(&self.user.user_id) .await .map(|set| set.into_iter().map(Into::into).collect())?) } @@ -243,7 +243,7 @@ mod tests { let mut groups = HashSet::::new(); groups.insert("Bobbersons".to_string()); mock.expect_get_user_groups() - .with(eq("bob".to_string())) + .with(eq("bob")) .return_once(|_| Ok(groups)); let context = Context:: { diff --git a/src/infra/tcp_backend_handler.rs b/src/infra/tcp_backend_handler.rs index 638cdcb..d647e25 100644 --- a/src/infra/tcp_backend_handler.rs +++ b/src/infra/tcp_backend_handler.rs @@ -29,7 +29,7 @@ mockall::mock! { async fn list_users(&self, request: ListUsersRequest) -> DomainResult>; async fn list_groups(&self) -> DomainResult>; async fn get_user_details(&self, request: UserDetailsRequest) -> DomainResult; - async fn get_user_groups(&self, user: String) -> DomainResult>; + async fn get_user_groups(&self, user: &str) -> DomainResult>; async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>; async fn delete_user(&self, request: DeleteUserRequest) -> DomainResult<()>; async fn create_group(&self, request: CreateGroupRequest) -> DomainResult;