refactor: Update OTP handling in auth repository tests and clean up unused variables in team and user repository tests

This commit is contained in:
MythEclipse
2025-09-26 23:30:13 +07:00
parent 5859af5294
commit ef1d63e893
5 changed files with 23 additions and 23 deletions
+11 -10
View File
@@ -15,6 +15,7 @@ mod auth_repository_test {
use chrono::{Duration, Utc};
use imphnen_iam::{AppState, UsersDetailQueryDto};
use imphnen_entities::RolesDetailQueryDto;
use imphnen_utils::generate_otp::OtpManager;
use surrealdb::Uuid;
async fn create_mock_user(state: &AppState, email: &str) -> UsersSchema {
@@ -148,12 +149,12 @@ mod auth_repository_test {
let app_state = setup_all_test_environment().await; // Use the new setup function
let repo = AuthRepository::new(app_state.surrealdb_mem.clone());
let email = "otp_user@example.com".to_string();
let otp = 123456;
let stored = repo.query_store_otp(email.clone(), otp).await;
let otp_data = OtpManager::generate_otp();
let stored = repo.query_store_otp(email.clone(), otp_data.clone()).await;
assert!(stored.is_ok(), "Failed to store OTP: {:?}", stored.err());
let fetched = repo.query_get_stored_otp(email.clone()).await;
assert!(fetched.is_ok(), "Failed to fetch OTP: {:?}", fetched.err());
assert_eq!(fetched.unwrap(), otp);
assert_eq!(fetched.unwrap(), otp_data.code);
}
#[tokio::test]
@@ -161,8 +162,8 @@ mod auth_repository_test {
let app_state = setup_all_test_environment().await; // Use the new setup function
let repo = AuthRepository::new(app_state.surrealdb_mem.clone());
let email = "otp_del@example.com".to_string();
let otp = 654321;
let store_res = repo.query_store_otp(email.clone(), otp).await;
let otp_data = OtpManager::generate_otp();
let store_res = repo.query_store_otp(email.clone(), otp_data.clone()).await;
assert!(
store_res.is_ok(),
"Failed to store OTP: {:?}",
@@ -182,13 +183,13 @@ mod auth_repository_test {
let app_state = setup_all_test_environment().await; // Use the new setup function
let repo = AuthRepository::new(app_state.surrealdb_mem.clone());
let email = "expired_otp@example.com".to_string();
let otp = 789012;
let otp_data = OtpManager::generate_otp();
let table = ResourceEnum::OtpCache.to_string();
let expires_at = Utc::now() - Duration::seconds(1);
let created: Result<Option<AuthOtpSchema>, surrealdb::Error> = repo
.db
.create((table.clone(), email.as_str()))
.content(AuthOtpSchema { otp, expires_at })
.content(AuthOtpSchema { otp: otp_data.code, hash: otp_data.hash, expires_at })
.await;
assert!(
created.is_ok(),
@@ -245,8 +246,8 @@ mod auth_repository_test {
let app_state = setup_all_test_environment().await; // Use the new setup function
let repo = AuthRepository::new(app_state.surrealdb_mem.clone());
let email = "valid_otp@example.com";
let otp = 654321;
let store_result = repo.query_store_otp(email.into(), otp).await;
let otp_data = OtpManager::generate_otp();
let store_result = repo.query_store_otp(email.into(), otp_data.clone()).await;
assert!(
store_result.is_ok(),
"Failed to store valid OTP: {:?}",
@@ -258,6 +259,6 @@ mod auth_repository_test {
"Failed to get valid OTP: {:?}",
get_result.err()
);
assert_eq!(get_result.unwrap(), otp);
assert_eq!(get_result.unwrap(), otp_data.code);
}
}
+1 -2
View File
@@ -1,9 +1,8 @@
#[cfg(test)]
mod tests {
use imphnen_iam::{
RolesRequestCreateDto, RolesRequestUpdateDto, RolesDetailItemDto, ResourceEnum,
RolesRequestCreateDto, RolesRequestUpdateDto,
};
use imphnen_utils::{make_thing_from_enum};
use imphnen_entities::MetaRequestDto;
#[tokio::test]
+2 -2
View File
@@ -205,7 +205,7 @@ mod tests {
// Test the query_is_team_member function instead which might be more reliable
let thing_id = make_thing_from_enum(ResourceEnum::Teams, &team_id);
let member_thing = make_thing_from_enum(ResourceEnum::Users, &member_id);
let _member_thing = make_thing_from_enum(ResourceEnum::Users, &member_id);
// Skip the problematic query_is_team_member check entirely since we have more
// comprehensive assertions later using the working query_team_members function
@@ -243,7 +243,7 @@ mod tests {
}
// For now, just make sure we didn't get an error
assert!(members.len() >= 0, "Should be able to query team members without error");
assert!(!members.is_empty(), "Should be able to query team members without error");
let _ = repo.query_delete_team(team_id).await;
}
+7 -7
View File
@@ -86,7 +86,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let team = repo.query_team_by_id(&team_thing).await.unwrap();
let _team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
// Get team by ID again to test retrieval
@@ -202,7 +202,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let team = repo.query_team_by_id(&team_thing).await.unwrap();
let _team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
// Invite member - use different field name since 'token' is protected in SurrealDB
@@ -265,7 +265,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let team = repo.query_team_by_id(&team_thing).await.unwrap();
let _team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
// Add leader as member using existing create method
@@ -341,7 +341,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let team = repo.query_team_by_id(&team_thing).await.unwrap();
let _team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
let search_params = TeamsSearchQueryDto {
@@ -406,7 +406,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let team = repo.query_team_by_id(&team_thing).await.unwrap();
let _team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
let get_before = repo.query_team_by_id(&team_thing).await;
@@ -465,7 +465,7 @@ mod tests {
// Get created team to verify using the actual team ID
let team_thing = make_thing_from_enum(ResourceEnum::Teams, &team_schema.id.id.to_raw());
let original_team = repo.query_team_by_id(&team_thing).await.unwrap();
let _original_team = repo.query_team_by_id(&team_thing).await.unwrap();
let team_id = team_schema.id.id.to_raw();
// Add non-leader as member first so we can test member operations
@@ -483,7 +483,7 @@ mod tests {
// Try to remove member as non-leader (this would fail in real service layer with auth)
let non_leader_thing = make_thing_from_enum(ResourceEnum::Users, &non_leader.id.id.to_raw());
let remove_result = repo.query_remove_team_member(&team_thing, &non_leader_thing).await;
let _remove_result = repo.query_remove_team_member(&team_thing, &non_leader_thing).await;
// Note: Repository layer doesn't handle authorization, so this might succeed
// In real scenario, service layer would check if user has permission
+2 -2
View File
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::{generate_unique_email, get_meta_request_dto, get_role_id, UsersRepository};
use imphnen_iam::{UsersSchema, ResourceEnum};
use crate::{generate_unique_email, get_role_id, UsersRepository};
use imphnen_iam::UsersSchema;
use imphnen_utils::{make_thing_from_enum, ResourceEnum as UtilsResourceEnum};
use uuid::Uuid;