use serde::Deserialize;
use serde_json::{Value, json};
use super::create_user_pool_client::build_client_response;
use crate::{
error::{AppError, Result},
storage::Storage,
types::{ClientId, UserPoolId},
};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Request {
user_pool_id: UserPoolId,
client_id: ClientId,
}
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 client = storage
.get_user_pool_client(&req.client_id)
.await
.ok_or(AppError::UserPoolClientNotFound)?;
if client.user_pool_id != req.user_pool_id {
return Err(AppError::UserPoolClientNotFound);
}
Ok(json!({
"UserPoolClient": build_client_response(&client)
}))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::action::user_pool::{create_user_pool, create_user_pool_client};
use serde_json::json;
#[tokio::test]
async fn test_describe_user_pool_client_success() {
let storage = Storage::new();
let pool = create_user_pool::handler(&storage, json!({"PoolName": "test-pool"}))
.await
.unwrap();
let pool_id = pool["UserPool"]["Id"].as_str().unwrap();
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();
let result = handler(
&storage,
json!({
"UserPoolId": pool_id,
"ClientId": client_id
}),
)
.await;
assert!(result.is_ok());
let body = result.unwrap();
assert_eq!(body["UserPoolClient"]["ClientId"], client_id);
assert_eq!(body["UserPoolClient"]["ClientName"], "test-client");
assert_eq!(body["UserPoolClient"]["UserPoolId"], pool_id);
}
#[tokio::test]
async fn test_describe_user_pool_client_pool_not_found() {
let storage = Storage::new();
let result = handler(
&storage,
json!({
"UserPoolId": "local_nonexistent123",
"ClientId": "client12345678901234567"
}),
)
.await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_describe_user_pool_client_client_not_found() {
let storage = Storage::new();
let pool = create_user_pool::handler(&storage, json!({"PoolName": "test-pool"}))
.await
.unwrap();
let pool_id = pool["UserPool"]["Id"].as_str().unwrap();
let result = handler(
&storage,
json!({
"UserPoolId": pool_id,
"ClientId": "nonexistent123456789012345"
}),
)
.await;
assert!(result.is_err());
}
}