From b0c721bb289f7fc352e80b1beeefa3f5ddad897d Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 30 Jun 2021 20:43:18 +0200 Subject: [PATCH] Add a method to get a single user's details --- src/domain/handler.rs | 2 ++ src/domain/sql_backend_handler.rs | 42 +++++++++++++++++++++++++++++++ src/infra/tcp_backend_handler.rs | 1 + 3 files changed, 45 insertions(+) diff --git a/src/domain/handler.rs b/src/domain/handler.rs index 76698cb..0c24432 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -13,6 +13,7 @@ pub trait LoginHandler: Clone + Send { pub trait BackendHandler: Clone + Send { async fn list_users(&self, request: ListUsersRequest) -> Result>; async fn list_groups(&self) -> Result>; + async fn get_user_details(&self, request: UserDetailsRequest) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn create_group(&self, request: CreateGroupRequest) -> Result; @@ -30,6 +31,7 @@ mockall::mock! { impl BackendHandler for TestBackendHandler { async fn list_users(&self, request: ListUsersRequest) -> Result>; async fn list_groups(&self) -> Result>; + async fn get_user_details(&self, request: UserDetailsRequest) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>; async fn create_group(&self, request: CreateGroupRequest) -> Result; diff --git a/src/domain/sql_backend_handler.rs b/src/domain/sql_backend_handler.rs index ca824d3..cc65dbb 100644 --- a/src/domain/sql_backend_handler.rs +++ b/src/domain/sql_backend_handler.rs @@ -117,6 +117,24 @@ impl BackendHandler for SqlBackendHandler { Ok(groups) } + async fn get_user_details(&self, request: UserDetailsRequest) -> Result { + let query = Query::select() + .column(Users::UserId) + .column(Users::Email) + .column(Users::DisplayName) + .column(Users::FirstName) + .column(Users::LastName) + .column(Users::Avatar) + .column(Users::CreationDate) + .from(Users::Table) + .and_where(Expr::col(Users::UserId).eq(request.user_id)) + .to_string(DbQueryBuilder {}); + + Ok(sqlx::query_as::<_, User>(&query) + .fetch_one(&self.sql_pool) + .await?) + } + async fn get_user_groups(&self, user: String) -> Result> { if user == self.config.ldap_user_dn { let mut groups = HashSet::new(); @@ -450,6 +468,30 @@ mod tests { ); } + #[tokio::test] + async fn test_get_user_details() { + let sql_pool = get_initialized_db().await; + let config = get_default_config(); + let handler = SqlBackendHandler::new(config, sql_pool); + insert_user(&handler, "bob", "bob00").await; + { + let user = handler + .get_user_details(UserDetailsRequest { + user_id: String::from("bob"), + }) + .await + .unwrap(); + assert_eq!(user.user_id, "bob".to_string()); + } + { + handler + .get_user_details(UserDetailsRequest { + user_id: String::from("John"), + }) + .await + .unwrap_err(); + } + } #[tokio::test] async fn test_get_user_groups() { let sql_pool = get_initialized_db().await; diff --git a/src/infra/tcp_backend_handler.rs b/src/infra/tcp_backend_handler.rs index 99b5035..638cdcb 100644 --- a/src/infra/tcp_backend_handler.rs +++ b/src/infra/tcp_backend_handler.rs @@ -28,6 +28,7 @@ mockall::mock! { impl BackendHandler for TestTcpBackendHandler { 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 create_user(&self, request: CreateUserRequest) -> DomainResult<()>; async fn delete_user(&self, request: DeleteUserRequest) -> DomainResult<()>;