Implementation of the delete_user_request.

Tests are barebones and are dependant of the list_users_request
(delete_user_request_test will fail if list_users_request fails)
This commit is contained in:
Marcelin Dupraz 2021-05-30 21:11:13 +02:00 committed by nitnelave
parent 48c4f1adbd
commit 2a6d23203c
4 changed files with 71 additions and 0 deletions

View File

@ -57,6 +57,11 @@ pub struct CreateUserRequest {
pub password: String,
}
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)]
pub struct DeleteUserRequest {
pub user_id: String,
}
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Group {
pub display_name: String,

View File

@ -10,6 +10,7 @@ pub trait BackendHandler: Clone + Send {
async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>;
async fn list_groups(&self) -> Result<Vec<Group>>;
async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>;
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>>;
@ -27,6 +28,7 @@ mockall::mock! {
async fn list_users(&self, request: ListUsersRequest) -> Result<Vec<User>>;
async fn list_groups(&self) -> Result<Vec<Group>>;
async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> Result<()>;
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;

View File

@ -243,6 +243,15 @@ impl BackendHandler for SqlBackendHandler {
Ok(())
}
async fn delete_user(&self, request: DeleteUserRequest) -> Result<()> {
let delete_query = Query::delete()
.from_table(Users::Table)
.and_where(Expr::col(Users::UserId).eq(request.user_id))
.to_string(DbQueryBuilder {});
sqlx::query(&delete_query).execute(&self.sql_pool).await?;
Ok(())
}
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32> {
let query = Query::insert()
.into_table(Groups::Table)
@ -506,4 +515,58 @@ mod tests {
HashSet::new()
);
}
#[tokio::test]
async fn test_delete_user() {
let sql_pool = get_initialized_db().await;
let config = Configuration::default();
let handler = SqlBackendHandler::new(config, sql_pool.clone());
insert_user(&handler, "val", "s3np4i").await;
insert_user(&handler, "Hector", "Be$t").await;
insert_user(&handler, "Jennz", "boupBoup").await;
// Remove a user
let _request_result = handler
.delete_user(DeleteUserRequest {
user_id: "Jennz".to_owned(),
})
.await
.unwrap();
let users = handler
.list_users(ListUsersRequest { filters: None })
.await
.unwrap()
.into_iter()
.map(|u| u.user_id)
.collect::<Vec<_>>();
assert_eq!(users, vec!["Hector", "val"]);
// Insert new user and remove two
insert_user(&handler, "NewBoi", "Joni").await;
let _request_result = handler
.delete_user(DeleteUserRequest {
user_id: "Hector".to_owned(),
})
.await
.unwrap();
let _request_result = handler
.delete_user(DeleteUserRequest {
user_id: "NewBoi".to_owned(),
})
.await
.unwrap();
let users = handler
.list_users(ListUsersRequest { filters: None })
.await
.unwrap()
.into_iter()
.map(|u| u.user_id)
.collect::<Vec<_>>();
assert_eq!(users, vec!["val"]);
}
}

View File

@ -28,6 +28,7 @@ mockall::mock! {
async fn list_groups(&self) -> DomainResult<Vec<Group>>;
async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>;
async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>;
async fn delete_user(&self, request: DeleteUserRequest) -> DomainResult<()>;
async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>;
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> DomainResult<()>;
}