2025-05-22 00:31:36 +07:00
|
|
|
use ::surrealdb::Uuid;
|
2025-09-22 14:59:38 +07:00
|
|
|
use ::surrealdb::sql;
|
2025-04-06 22:13:46 +07:00
|
|
|
pub use imphnen_entities::*;
|
2025-05-17 02:08:59 +07:00
|
|
|
pub use imphnen_iam::*;
|
2025-09-22 14:59:38 +07:00
|
|
|
|
2025-07-21 21:29:04 +07:00
|
|
|
pub fn create_test_mentor(
|
|
|
|
|
email: &str,
|
|
|
|
|
fullname: &str,
|
|
|
|
|
is_active: bool,
|
2025-09-22 14:59:38 +07:00
|
|
|
role_id: &sql::Thing,
|
2025-07-21 21:29:04 +07:00
|
|
|
) -> UsersSchema {
|
|
|
|
|
let mut user = create_test_user(email, fullname, is_active, role_id);
|
|
|
|
|
user.mentor_id = Some(user.id.clone());
|
|
|
|
|
user
|
|
|
|
|
}
|
2025-09-22 14:59:38 +07:00
|
|
|
|
2025-07-21 21:29:04 +07:00
|
|
|
pub fn create_test_user(
|
|
|
|
|
email: &str,
|
|
|
|
|
fullname: &str,
|
|
|
|
|
is_active: bool,
|
2025-09-22 14:59:38 +07:00
|
|
|
role_id: &sql::Thing,
|
2025-07-21 21:29:04 +07:00
|
|
|
) -> UsersSchema {
|
|
|
|
|
UsersSchema {
|
|
|
|
|
id: make_thing("app_users", &Uuid::new_v4().to_string()),
|
|
|
|
|
email: email.to_string(),
|
2025-09-22 14:59:38 +07:00
|
|
|
fullname: format!("{} {}", fullname, rand::random::<u32>()),
|
2025-08-12 17:31:12 +07:00
|
|
|
legal_name: None,
|
2025-09-22 14:59:38 +07:00
|
|
|
password: hash_password("password123").unwrap(),
|
2025-07-21 21:29:04 +07:00
|
|
|
is_deleted: false,
|
|
|
|
|
avatar: None,
|
|
|
|
|
phone_number: "081234567890".to_string(),
|
2025-08-12 17:31:12 +07:00
|
|
|
phone_for_verification: None,
|
2025-07-21 21:29:04 +07:00
|
|
|
is_active,
|
|
|
|
|
gender: None,
|
|
|
|
|
birthdate: None,
|
2025-08-12 17:31:12 +07:00
|
|
|
domicile: None,
|
|
|
|
|
bio: None,
|
|
|
|
|
last_education: None,
|
|
|
|
|
linkedin_url: None,
|
|
|
|
|
github_url: None,
|
|
|
|
|
cv_url: None,
|
|
|
|
|
portfolio_url: None,
|
2025-08-13 20:40:02 +07:00
|
|
|
website_url: None,
|
|
|
|
|
twitter_url: None,
|
|
|
|
|
location: None,
|
|
|
|
|
skills: None,
|
|
|
|
|
experience: None,
|
|
|
|
|
education: None,
|
2025-08-14 13:16:39 +07:00
|
|
|
career_status: None,
|
2025-09-22 14:59:38 +07:00
|
|
|
role: role_id.clone(),
|
2025-07-21 21:29:04 +07:00
|
|
|
created_at: get_iso_date(),
|
|
|
|
|
updated_at: get_iso_date(),
|
|
|
|
|
mentor_id: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-22 14:59:38 +07:00
|
|
|
|
2025-04-06 22:13:46 +07:00
|
|
|
#[cfg(test)]
|
2025-05-17 02:08:59 +07:00
|
|
|
pub mod iam;
|
2025-07-21 21:29:04 +07:00
|
|
|
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};
|
2025-05-22 00:31:36 +07:00
|
|
|
|
|
|
|
|
pub fn generate_unique_email(prefix: &str) -> String {
|
|
|
|
|
format!("{}_{}@example.com", prefix, Uuid::new_v4())
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 14:59:38 +07:00
|
|
|
pub async fn get_role_id(role_name: &str, state: &crate::AppState) -> sql::Thing {
|
2025-05-22 00:31:36 +07:00
|
|
|
let repo = RolesRepository::new(state);
|
2025-09-22 14:59:38 +07:00
|
|
|
if let Ok(existing) = repo.query_role_by_name(role_name.into()).await {
|
|
|
|
|
return make_thing(&ResourceEnum::Roles.to_string(), &existing.id);
|
2025-05-22 00:31:36 +07:00
|
|
|
}
|
|
|
|
|
let _ = repo
|
|
|
|
|
.query_create_role(RolesRequestCreateDto {
|
2025-09-22 14:59:38 +07:00
|
|
|
name: role_name.into(),
|
2025-05-22 00:31:36 +07:00
|
|
|
permissions: vec![],
|
|
|
|
|
})
|
|
|
|
|
.await;
|
2025-09-22 14:59:38 +07:00
|
|
|
let role = repo
|
|
|
|
|
.query_role_by_name(role_name.into())
|
2025-05-22 00:31:36 +07:00
|
|
|
.await
|
2025-09-22 14:59:38 +07:00
|
|
|
.expect("Role not found after creation");
|
|
|
|
|
make_thing(&ResourceEnum::Roles.to_string(), &role.id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_app_state() -> crate::AppState {
|
|
|
|
|
create_mock_app_state().await
|
2025-05-22 00:31:36 +07:00
|
|
|
}
|
2025-07-21 21:29:04 +07:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|