Add comprehensive tests for Gacha Credits, Auth, and User services

- Implement tests for GachaCreditsService including creation, retrieval, updating, and deletion of gacha credits.
- Add tests for AuthController covering user login, registration, email verification, and error handling for invalid credentials.
- Enhance AuthService tests to include login, registration, email verification, OTP resend, password reset, and token refresh functionalities.
- Create tests for UsersService to validate user creation, retrieval, updating, deletion, and fetching by email.
- Ensure all tests utilize a consistent setup and teardown process for database interactions.
This commit is contained in:
MythEclipse committed 2025-09-25 22:53:26 +07:00
1 parent 0e48b6755f
commit f3d88e2ac9
8 files changed
+1912 -212

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
#[cfg(test)]
mod tests {
use crate::{generate_unique_email, get_role_id, UsersRepository};
use axum::{http::StatusCode, response::Response};
use imphnen_iam::{
AuthLoginRequestDto, AuthLoginResponsetDto, AuthNewPasswordRequestDto,
AuthRefreshTokenRequestDto, AuthRegisterRequestDto, AuthResendOtpRequestDto,
AuthVerifyEmailRequestDto, MessageResponseDto, ResponseSuccessDto, TokenDto,
};
use imphnen_utils::{make_thing_from_enum, ResourceEnum as UtilsResourceEnum};
use uuid::Uuid;
#[tokio::test]
async fn test_post_login_controller() {
let app_state = crate::get_app_state().await;
let repo = UsersRepository::new(&app_state);
let role_id = get_role_id("user", &app_state).await;
// Test data
let email = generate_unique_email("test_login_controller");
let password = "password123".to_string();
// Create test user first
let user_request = AuthRegisterRequestDto {
email: email.clone(),
password: password.clone(),
fullname: "Test User Controller".to_string(),
phone_number: Some("+1234567890".to_string()),
};
let register_response = imphnen_iam::AuthController::mutation_register(
&app_state,
user_request,
)
.await;
assert_eq!(register_response.status(), StatusCode::CREATED);
// Wait for email verification (simulate OTP verification in test)
let verify_request = AuthVerifyEmailRequestDto {
email: email.clone(),
otp: "123456".to_string(), // Default test OTP
};
let verify_response = imphnen_iam::AuthController::mutation_verify_email(
&app_state,
verify_request,
)
.await;
assert_eq!(verify_response.status(), StatusCode::OK);
// Try to login
let login_request = AuthLoginRequestDto {
email: email.clone(),
password: password.clone(),
};
let response = imphnen_iam::AuthController::mutation_login(
&app_state,
login_request,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
let login_response: ResponseSuccessDto = response.into_body().await.unwrap();
assert!(login_response.data.is_some());
// Clean up
let user = repo.query_user_by_email(email.clone()).await.unwrap();
let _ = repo.query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_post_login_controller_invalid_credentials() {
let app_state = crate::get_app_state().await;
// Test data
let email = generate_unique_email("test_login_invalid");
let password = "wrongpassword".to_string();
// Try to login with non-existent user
let login_request = AuthLoginRequestDto {
email: email.clone(),
password: password.clone(),
};
let response = imphnen_iam::AuthController::mutation_login(
&app_state,
login_request,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
let error_response: MessageResponseDto = response.into_body().await.unwrap();
assert!(error_response.message.contains("Email or password not correct"));
}
}