Files
imphnen-backend-service/tests/src/lib.rs
T
MythEclipse 1a2e0c58b6 Add comprehensive tests for mentor repository and authentication
- Implemented tests for creating, retrieving, updating, and deleting mentors in `mentor_repository_test.rs`.
- Added tests for user authentication, including successful login, invalid email formats, and inactive users in `auth_login_tests.rs`.
- Created a mock test environment setup in `mock_test.rs` to facilitate database operations during tests.
- Updated module structure to include new test files for mentors and authentication.
- Ensured cleanup of the database after tests to maintain isolation and prevent side effects.
2025-07-21 21:29:04 +07:00

78 lines
1.9 KiB
Rust

use ::surrealdb::Uuid;
pub use imphnen_entities::*;
pub use imphnen_iam::*;
pub fn create_test_mentor(
email: &str,
fullname: &str,
is_active: bool,
role_id: &str,
) -> UsersSchema {
let mut user = create_test_user(email, fullname, is_active, role_id);
user.mentor_id = Some(user.id.clone());
user
}
pub fn create_test_user(
email: &str,
fullname: &str,
is_active: bool,
role_id: &str,
) -> UsersSchema {
UsersSchema {
id: make_thing("app_users", &Uuid::new_v4().to_string()),
email: email.to_string(),
fullname: format!("Randomize {} {}", fullname, rand::random::<u32>()),
password: hash_password("secret").unwrap(),
is_deleted: false,
avatar: None,
phone_number: "081234567890".to_string(),
is_active,
gender: None,
birthdate: None,
role: make_thing("app_roles", role_id),
created_at: get_iso_date(),
updated_at: get_iso_date(),
mentor_id: None,
}
}
#[cfg(test)]
pub mod iam;
pub mod mock_test;
pub use mock_test::{
cleanup_db, create_mock_app_state, seed_permissions_and_roles_for_test,
seed_users_for_test, setup_all_test_environment,
};
pub use imphnen_utils::{get_iso_date, hash_password, make_thing, Env};
pub fn generate_unique_email(prefix: &str) -> String {
format!("{}_{}@example.com", prefix, Uuid::new_v4())
}
pub async fn get_role_id(state: &crate::AppState) -> String {
let repo = RolesRepository::new(state);
if let Ok(existing) = repo.query_role_by_name("User".into()).await {
return existing.id;
}
let _ = repo
.query_create_role(RolesRequestCreateDto {
name: "User".into(),
permissions: vec![],
})
.await;
repo
.query_role_by_name("User".into())
.await
.expect("Role not found after creation")
.id
}
pub async fn setup() {
cleanup_db().await;
let app_state = create_mock_app_state().await;
seed_permissions_and_roles_for_test(&app_state.surrealdb_ws)
.await
.unwrap();
seed_users_for_test(&app_state.surrealdb_ws).await.unwrap();
}