Add method to add users to groups

This commit is contained in:
Valentin Tolmer 2021-05-26 15:49:59 +02:00
parent b3c2f5edfc
commit 352a7d90fc
4 changed files with 42 additions and 17 deletions

View File

@ -68,6 +68,12 @@ pub struct CreateGroupRequest {
pub display_name: String, pub display_name: String,
} }
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct AddUserToGroupRequest {
pub user_id: String,
pub group_id: i32,
}
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
pub struct JWTClaims { pub struct JWTClaims {
pub exp: DateTime<Utc>, pub exp: DateTime<Utc>,

View File

@ -11,6 +11,7 @@ pub trait BackendHandler: Clone + Send {
async fn list_groups(&self) -> Result<Vec<Group>>; async fn list_groups(&self) -> Result<Vec<Group>>;
async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>; async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>;
} }
@ -28,5 +29,6 @@ mockall::mock! {
async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>; async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
} }
} }

View File

@ -242,6 +242,7 @@ impl BackendHandler for SqlBackendHandler {
sqlx::query(&query).execute(&self.sql_pool).await?; sqlx::query(&query).execute(&self.sql_pool).await?;
Ok(()) Ok(())
} }
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32> { async fn create_group(&self, request: CreateGroupRequest) -> Result<i32> {
let query = Query::insert() let query = Query::insert()
.into_table(Groups::Table) .into_table(Groups::Table)
@ -255,8 +256,17 @@ impl BackendHandler for SqlBackendHandler {
.and_where(Expr::col(Groups::DisplayName).eq(request.display_name.as_str())) .and_where(Expr::col(Groups::DisplayName).eq(request.display_name.as_str()))
.to_string(DbQueryBuilder {}); .to_string(DbQueryBuilder {});
let row = sqlx::query(&query).fetch_one(&self.sql_pool).await?; let row = sqlx::query(&query).fetch_one(&self.sql_pool).await?;
Ok( Ok(row.get::<i32, _>(&*Groups::GroupId.to_string()))
row.get::<i32, _>(&*Groups::GroupId.to_string())) }
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()> {
let query = Query::insert()
.into_table(Memberships::Table)
.columns(vec![Memberships::UserId, Memberships::GroupId])
.values_panic(vec![request.user_id.into(), request.group_id.into()])
.to_string(DbQueryBuilder {});
sqlx::query(&query).execute(&self.sql_pool).await?;
Ok(())
} }
} }
@ -288,16 +298,22 @@ mod tests {
} }
async fn insert_group(handler: &SqlBackendHandler, name: &str) -> i32 { async fn insert_group(handler: &SqlBackendHandler, name: &str) -> i32 {
handler.create_group(CreateGroupRequest{display_name: name.to_string()}).await.unwrap() handler
.create_group(CreateGroupRequest {
display_name: name.to_string(),
})
.await
.unwrap()
} }
async fn insert_membership(sql_pool: &Pool, group_id: i32, user_id: &str) { async fn insert_membership(handler: &SqlBackendHandler, group_id: i32, user_id: &str) {
let query = Query::insert() handler
.into_table(Memberships::Table) .add_user_to_group(AddUserToGroupRequest {
.columns(vec![Memberships::UserId, Memberships::GroupId]) user_id: user_id.to_string(),
.values_panic(vec![user_id.into(), group_id.into()]) group_id,
.to_string(DbQueryBuilder {}); })
sqlx::query(&query).execute(sql_pool).await.unwrap(); .await
.unwrap();
} }
#[tokio::test] #[tokio::test]
@ -437,10 +453,10 @@ mod tests {
insert_user(&handler, "John", "Pa33w0rd!").await; insert_user(&handler, "John", "Pa33w0rd!").await;
let group_1 = insert_group(&handler, "Best Group").await; let group_1 = insert_group(&handler, "Best Group").await;
let group_2 = insert_group(&handler, "Worst Group").await; let group_2 = insert_group(&handler, "Worst Group").await;
insert_membership(&sql_pool, group_1, "bob").await; insert_membership(&handler, group_1, "bob").await;
insert_membership(&sql_pool, group_1, "patrick").await; insert_membership(&handler, group_1, "patrick").await;
insert_membership(&sql_pool, group_2, "patrick").await; insert_membership(&handler, group_2, "patrick").await;
insert_membership(&sql_pool, group_2, "John").await; insert_membership(&handler, group_2, "John").await;
assert_eq!( assert_eq!(
handler.list_groups().await.unwrap(), handler.list_groups().await.unwrap(),
vec![ vec![
@ -466,9 +482,9 @@ mod tests {
insert_user(&handler, "John", "Pa33w0rd!").await; insert_user(&handler, "John", "Pa33w0rd!").await;
let group_1 = insert_group(&handler, "Group1").await; let group_1 = insert_group(&handler, "Group1").await;
let group_2 = insert_group(&handler, "Group2").await; let group_2 = insert_group(&handler, "Group2").await;
insert_membership(&sql_pool, group_1, "bob").await; insert_membership(&handler, group_1, "bob").await;
insert_membership(&sql_pool, group_1, "patrick").await; insert_membership(&handler, group_1, "patrick").await;
insert_membership(&sql_pool, group_2, "patrick").await; insert_membership(&handler, group_2, "patrick").await;
let mut bob_groups = HashSet::new(); let mut bob_groups = HashSet::new();
bob_groups.insert("Group1".to_string()); bob_groups.insert("Group1".to_string());
let mut patrick_groups = HashSet::new(); let mut patrick_groups = HashSet::new();

View File

@ -29,6 +29,7 @@ mockall::mock! {
async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>; async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>;
async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>; async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>;
async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>; async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> DomainResult<()>;
} }
#[async_trait] #[async_trait]
impl TcpBackendHandler for TestTcpBackendHandler { impl TcpBackendHandler for TestTcpBackendHandler {