feat(handler): add app

This commit is contained in:
2026-01-31 15:44:56 +08:00
parent 6b68a368f1
commit 4dc46659c9
25 changed files with 2516 additions and 14 deletions

View File

@@ -0,0 +1,93 @@
use iam_service::models::{CreateAppRequest, ListAppsQuery, RequestAppStatusChangeRequest, UpdateAppRequest};
use iam_service::services::AppService;
use sqlx::PgPool;
use uuid::Uuid;
#[tokio::test]
async fn app_lifecycle_create_update_disable_approve_delete()
-> Result<(), Box<dyn std::error::Error>> {
let database_url = match std::env::var("DATABASE_URL") {
Ok(v) if !v.trim().is_empty() => v,
_ => return Ok(()),
};
let pool = PgPool::connect(&database_url).await?;
let service = AppService::new(pool.clone());
let actor = Uuid::new_v4();
let app_id = format!("app{}", Uuid::new_v4().to_string().replace('-', ""));
let app_id = &app_id[..std::cmp::min(32, app_id.len())];
let app_id = app_id.to_string();
let created = service
.create_app(
CreateAppRequest {
id: app_id.clone(),
name: "Test App".to_string(),
description: Some("desc".to_string()),
app_type: "product".to_string(),
owner: Some("team-a".to_string()),
},
actor,
)
.await?;
assert_eq!(created.id, app_id);
assert_eq!(created.status, "active");
let updated = service
.update_app(
&app_id,
UpdateAppRequest {
name: Some("Test App 2".to_string()),
description: None,
app_type: None,
owner: Some("team-b".to_string()),
},
actor,
)
.await?;
assert_eq!(updated.name, "Test App 2");
assert_eq!(updated.owner.as_deref(), Some("team-b"));
let req = service
.request_status_change(
&app_id,
RequestAppStatusChangeRequest {
to_status: "disabled".to_string(),
effective_at: None,
reason: Some("test".to_string()),
},
actor,
)
.await?;
assert_eq!(req.status, "pending");
let approved = service
.approve_status_change(req.id, None, actor)
.await?;
assert!(approved.status == "approved" || approved.status == "applied");
let got = service.get_app(&app_id).await?;
assert_eq!(got.status, "disabled");
let list = service
.list_apps(ListAppsQuery {
page: Some(1),
page_size: Some(50),
status: Some("disabled".to_string()),
app_type: None,
sort_by: Some("id".to_string()),
sort_order: Some("asc".to_string()),
created_from: None,
created_to: None,
})
.await?;
assert!(list.iter().any(|a| a.id == app_id));
service.delete_app(&app_id, actor).await?;
let got2 = service.get_app(&app_id).await?;
assert_eq!(got2.status, "deleted");
Ok(())
}