Add unit tests for TeamsRepository and TeamsService
- Implement tests for team creation, retrieval, updating, and deletion in TeamsRepository. - Add tests for team member management including adding and removing members. - Create tests for team invitations and searching teams. - Ensure proper cleanup of test data after each test case. - Validate unauthorized operations for team management.
This commit is contained in:
+21
-13
@@ -1,28 +1,31 @@
|
||||
use ::surrealdb::Uuid;
|
||||
use ::surrealdb::sql;
|
||||
pub use imphnen_entities::*;
|
||||
pub use imphnen_iam::*;
|
||||
|
||||
pub fn create_test_mentor(
|
||||
email: &str,
|
||||
fullname: &str,
|
||||
is_active: bool,
|
||||
role_id: &str,
|
||||
role_id: &sql::Thing,
|
||||
) -> 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,
|
||||
role_id: &sql::Thing,
|
||||
) -> UsersSchema {
|
||||
UsersSchema {
|
||||
id: make_thing("app_users", &Uuid::new_v4().to_string()),
|
||||
email: email.to_string(),
|
||||
fullname: format!("Randomize {} {}", fullname, rand::random::<u32>()),
|
||||
fullname: format!("{} {}", fullname, rand::random::<u32>()),
|
||||
legal_name: None,
|
||||
password: hash_password("secret").unwrap(),
|
||||
password: hash_password("password123").unwrap(),
|
||||
is_deleted: false,
|
||||
avatar: None,
|
||||
phone_number: "081234567890".to_string(),
|
||||
@@ -44,12 +47,13 @@ pub fn create_test_user(
|
||||
experience: None,
|
||||
education: None,
|
||||
career_status: None,
|
||||
role: make_thing("app_roles", role_id),
|
||||
role: role_id.clone(),
|
||||
created_at: get_iso_date(),
|
||||
updated_at: get_iso_date(),
|
||||
mentor_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod iam;
|
||||
pub mod mock_test;
|
||||
@@ -65,22 +69,26 @@ 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 {
|
||||
pub async fn get_role_id(role_name: &str, state: &crate::AppState) -> sql::Thing {
|
||||
let repo = RolesRepository::new(state);
|
||||
if let Ok(existing) = repo.query_role_by_name("User".into()).await {
|
||||
return existing.id;
|
||||
if let Ok(existing) = repo.query_role_by_name(role_name.into()).await {
|
||||
return make_thing(&ResourceEnum::Roles.to_string(), &existing.id);
|
||||
}
|
||||
let _ = repo
|
||||
.query_create_role(RolesRequestCreateDto {
|
||||
name: "User".into(),
|
||||
name: role_name.into(),
|
||||
permissions: vec![],
|
||||
})
|
||||
.await;
|
||||
repo
|
||||
.query_role_by_name("User".into())
|
||||
let role = repo
|
||||
.query_role_by_name(role_name.into())
|
||||
.await
|
||||
.expect("Role not found after creation")
|
||||
.id
|
||||
.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
|
||||
}
|
||||
|
||||
pub async fn setup() {
|
||||
|
||||
Reference in New Issue
Block a user