use chrono::Utc;
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{
error::{AppError, Result},
storage::Storage,
types::UserPoolId,
};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Request {
user_pool_id: UserPoolId,
username: String,
user_attribute_names: Vec<String>,
}
pub async fn handler(storage: &Storage, body: Value) -> Result<Value> {
let req: Request = serde_json::from_value(body)
.map_err(|e| AppError::InvalidParameter(format!("Invalid request: {}", e)))?;
storage
.get_user_pool(&req.user_pool_id)
.await
.ok_or(AppError::UserPoolNotFound)?;
let mut user = storage
.get_user_by_username(&req.user_pool_id, &req.username)
.await
.ok_or(AppError::UserNotFound)?;
user.attributes
.retain(|attr| !req.user_attribute_names.contains(&attr.name));
for attr_name in &req.user_attribute_names {
match attr_name.as_str() {
"email" => user.email = None,
"phone_number" => user.phone_number = None,
_ => {}
}
}
user.last_modified_date = Utc::now();
storage.update_user(user).await;
Ok(json!({}))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::action::user::admin_create_user;
use crate::action::user::admin_get_user;
use crate::action::user_pool::create_user_pool;
use serde_json::json;
#[tokio::test]
async fn test_admin_delete_user_attributes_success() {
let storage = Storage::new();
let pool = create_user_pool::handler(&storage, json!({"PoolName": "test"}))
.await
.unwrap();
let pool_id = pool["UserPool"]["Id"].as_str().unwrap();
admin_create_user::handler(
&storage,
json!({
"UserPoolId": pool_id,
"Username": "testuser",
"UserAttributes": [
{"Name": "email", "Value": "test@example.com"},
{"Name": "custom:role", "Value": "admin"}
]
}),
)
.await
.unwrap();
let result = handler(
&storage,
json!({
"UserPoolId": pool_id,
"Username": "testuser",
"UserAttributeNames": ["custom:role"]
}),
)
.await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), json!({}));
let user = admin_get_user::handler(
&storage,
json!({
"UserPoolId": pool_id,
"Username": "testuser"
}),
)
.await
.unwrap();
let attrs = user["UserAttributes"].as_array().unwrap();
assert!(attrs.iter().all(|a| a["Name"] != "custom:role"));
assert!(attrs.iter().any(|a| a["Name"] == "email"));
}
#[tokio::test]
async fn test_admin_delete_user_attributes_user_not_found() {
let storage = Storage::new();
let pool = create_user_pool::handler(&storage, json!({"PoolName": "test"}))
.await
.unwrap();
let pool_id = pool["UserPool"]["Id"].as_str().unwrap();
let result = handler(
&storage,
json!({
"UserPoolId": pool_id,
"Username": "nonexistent",
"UserAttributeNames": ["email"]
}),
)
.await;
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), AppError::UserNotFound));
}
#[tokio::test]
async fn test_admin_delete_user_attributes_pool_not_found() {
let storage = Storage::new();
let result = handler(
&storage,
json!({
"UserPoolId": "local_nonexistent",
"Username": "testuser",
"UserAttributeNames": ["email"]
}),
)
.await;
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), AppError::UserPoolNotFound));
}
}