Files
rust-user-api/tests/migration_tests.rs
enoch bb9d7a869d
Some checks failed
Deploy to Production / Run Tests (push) Failing after 16m35s
Deploy to Production / Security Scan (push) Has been skipped
Deploy to Production / Build Docker Image (push) Has been skipped
Deploy to Production / Deploy to Staging (push) Has been skipped
Deploy to Production / Deploy to Production (push) Has been skipped
Deploy to Production / Notify Results (push) Successful in 31s
feat: 完成Rust User API完整开发
 新功能:
- SQLite数据库集成和持久化存储
- 数据库迁移系统和版本管理
- API分页功能和高效查询
- 用户搜索和过滤机制
- 完整的RBAC角色权限系统
- 结构化日志记录和系统监控
- API限流和多层安全防护
- Docker容器化和生产部署配置

🔒 安全特性:
- JWT认证和授权
- 限流和防暴力破解
- 安全头和CORS配置
- 输入验证和XSS防护
- 审计日志和安全监控

📊 监控和运维:
- Prometheus指标收集
- 健康检查和系统监控
- 自动化备份和恢复
- 完整的运维文档和脚本
- CI/CD流水线配置

🚀 部署支持:
- 多环境Docker配置
- 生产环境部署指南
- 性能优化和安全加固
- 故障排除和应急响应
- 自动化运维脚本

📚 文档完善:
- API使用文档
- 部署检查清单
- 运维操作手册
- 性能和安全指南
- 故障排除指南
2025-08-07 16:03:32 +08:00

92 lines
3.3 KiB
Rust

//! 迁移系统测试
use rust_user_api::{
storage::{database::DatabaseUserStore, MigrationManager, UserStore},
models::role::UserRole,
};
use tempfile::tempdir;
#[tokio::test]
async fn test_migration_system_integration() {
// 创建临时数据库
let temp_dir = tempdir().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("migration_test.db");
let database_url = format!("sqlite://{}?mode=rwc", db_path.display());
// 创建数据库存储(这会自动运行迁移)
let store = DatabaseUserStore::from_url(&database_url)
.await
.expect("Failed to create database store with migrations");
// 验证迁移系统创建了正确的表结构
// 通过尝试创建用户来验证表结构正确
let user = rust_user_api::models::user::User {
id: uuid::Uuid::new_v4(),
username: "migration_test_user".to_string(),
email: "migration_test@example.com".to_string(),
password_hash: "hashed_password".to_string(),
role: UserRole::User,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
};
let result = store.create_user(user).await;
assert!(result.is_ok(), "Failed to create user with migrated database: {:?}", result.err());
println!("✅ 迁移系统集成测试通过");
}
#[tokio::test]
async fn test_migration_manager_directly() {
use sqlx::SqlitePool;
// 创建内存数据库
let pool = SqlitePool::connect("sqlite::memory:")
.await
.expect("Failed to create test pool");
let migration_manager = MigrationManager::new(pool.clone());
// 运行迁移
let result = migration_manager.run_migrations().await;
assert!(result.is_ok(), "Failed to run migrations: {:?}", result.err());
// 检查当前版本
let version = migration_manager.get_current_version().await.unwrap();
assert!(version.is_some(), "No migration version found");
assert_eq!(version.unwrap(), 1, "Expected migration version 1");
// 检查迁移状态
let status = migration_manager.get_migration_status().await.unwrap();
assert_eq!(status.len(), 1, "Expected 1 executed migration");
assert_eq!(status[0].0, 1, "Expected migration version 1");
assert_eq!(status[0].1, "initial_users_table", "Expected migration name");
println!("✅ 迁移管理器直接测试通过");
}
#[tokio::test]
async fn test_migration_idempotency() {
use sqlx::SqlitePool;
// 创建内存数据库
let pool = SqlitePool::connect("sqlite::memory:")
.await
.expect("Failed to create test pool");
let migration_manager = MigrationManager::new(pool.clone());
// 第一次运行迁移
let result1 = migration_manager.run_migrations().await;
assert!(result1.is_ok(), "First migration run failed: {:?}", result1.err());
// 第二次运行迁移(应该跳过已执行的迁移)
let result2 = migration_manager.run_migrations().await;
assert!(result2.is_ok(), "Second migration run failed: {:?}", result2.err());
// 验证只有一个迁移记录
let status = migration_manager.get_migration_status().await.unwrap();
assert_eq!(status.len(), 1, "Expected only 1 migration record after multiple runs");
println!("✅ 迁移幂等性测试通过");
}