use chrono::{Duration, Utc};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{
error::{AppError, Result},
storage::Storage,
types::{ClientId, ConfirmationCode},
};
use super::helpers::{generate_confirmation_code, mask_email};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Request {
client_id: ClientId,
username: 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)))?;
let client = storage
.get_user_pool_client(&req.client_id)
.await
.ok_or(AppError::UserPoolClientNotFound)?;
let user = storage
.get_user_by_username(&client.user_pool_id, &req.username)
.await
.ok_or(AppError::UserNotFound)?;
let code = generate_confirmation_code();
let confirmation = ConfirmationCode {
user_id: user.id,
code: code.clone(),
expires_at: Utc::now() + Duration::hours(24),
};
storage.save_confirmation_code(confirmation).await;
tracing::info!("Resend confirmation code for {}: {}", req.username, code);
Ok(json!({
"CodeDeliveryDetails": {
"Destination": user.email.map(|e| mask_email(&e)).unwrap_or_default(),
"DeliveryMedium": "EMAIL",
"AttributeName": "email"
}
}))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use crate::action::user::sign_up;
use crate::action::user_pool::{create_user_pool, create_user_pool_client};
async fn setup_pool_and_client(storage: &Storage) -> (String, String) {
let pool = create_user_pool::handler(storage, json!({"PoolName": "test"}))
.await
.unwrap();
let pool_id = pool["UserPool"]["Id"].as_str().unwrap().to_string();
let client = create_user_pool_client::handler(
storage,
json!({
"UserPoolId": pool_id,
"ClientName": "test-client"
}),
)
.await
.unwrap();
let client_id = client["UserPoolClient"]["ClientId"]
.as_str()
.unwrap()
.to_string();
(pool_id, client_id)
}
#[tokio::test]
async fn test_resend_confirmation_code_success() {
let storage = Storage::new();
let (_pool_id, client_id) = setup_pool_and_client(&storage).await;
let sign_up_result = sign_up::handler(
&storage,
json!({
"ClientId": client_id,
"Username": "testuser",
"Password": "Password123!",
"UserAttributes": [
{"Name": "email", "Value": "test@example.com"}
]
}),
)
.await
.unwrap();
let user_sub = sign_up_result["UserSub"].as_str().unwrap();
let user_id = uuid::Uuid::parse_str(user_sub).unwrap();
let original_code = storage.get_confirmation_code(&user_id).await.unwrap();
let result = handler(
&storage,
json!({
"ClientId": client_id,
"Username": "testuser"
}),
)
.await;
assert!(result.is_ok());
let body = result.unwrap();
assert!(
body["CodeDeliveryDetails"]["Destination"]
.as_str()
.unwrap()
.contains("***")
);
let new_code = storage.get_confirmation_code(&user_id).await.unwrap();
assert_ne!(original_code.code, new_code.code);
}
#[tokio::test]
async fn test_resend_confirmation_code_user_not_found() {
let storage = Storage::new();
let (_pool_id, client_id) = setup_pool_and_client(&storage).await;
let result = handler(
&storage,
json!({
"ClientId": client_id,
"Username": "nonexistent"
}),
)
.await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_resend_confirmation_code_invalid_client() {
let storage = Storage::new();
let result = handler(
&storage,
json!({
"ClientId": "invalid-client-id",
"Username": "testuser"
}),
)
.await;
assert!(result.is_err());
}
}