gbserver/gb-auth/tests/auth_service_tests.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2024-12-22 20:56:52 -03:00
#[cfg(test)]
mod tests {
2024-12-24 21:13:47 -03:00
use gb_auth::services::auth_service::AuthService;
use gb_auth::models::LoginRequest;
use gb_core::models::User;
2024-12-22 20:56:52 -03:00
use sqlx::PgPool;
2024-12-24 21:13:47 -03:00
use std::sync::Arc;
2024-12-22 20:56:52 -03:00
use rstest::*;
2024-12-24 21:13:47 -03:00
2024-12-22 20:56:52 -03:00
#[fixture]
async fn auth_service() -> AuthService {
2024-12-24 21:13:47 -03:00
let db_pool = PgPool::connect("postgresql://postgres:postgres@localhost:5432/test_db")
.await
.expect("Failed to create database connection");
2024-12-22 20:56:52 -03:00
AuthService::new(
2024-12-24 21:13:47 -03:00
Arc::new(db_pool),
2024-12-22 20:56:52 -03:00
"test_secret".to_string(),
2024-12-24 21:13:47 -03:00
3600
2024-12-22 20:56:52 -03:00
)
}
#[rstest]
2024-12-24 21:13:47 -03:00
#[tokio::test]
async fn test_login_success() -> Result<(), Box<dyn std::error::Error>> {
let auth_service = auth_service().await;
2024-12-22 20:56:52 -03:00
let request = LoginRequest {
email: "test@example.com".to_string(),
password: "password123".to_string(),
};
let result = auth_service.login(request).await;
assert!(result.is_ok());
2024-12-24 21:13:47 -03:00
Ok(())
2024-12-22 20:56:52 -03:00
}
#[rstest]
#[tokio::test]
2024-12-24 21:13:47 -03:00
async fn test_login_invalid_credentials() -> Result<(), Box<dyn std::error::Error>> {
let auth_service = auth_service().await;
2024-12-22 20:56:52 -03:00
let request = LoginRequest {
2024-12-24 21:13:47 -03:00
email: "wrong@example.com".to_string(),
password: "wrongpassword".to_string(),
2024-12-22 20:56:52 -03:00
};
let result = auth_service.login(request).await;
assert!(result.is_err());
2024-12-24 21:13:47 -03:00
Ok(())
2024-12-22 20:56:52 -03:00
}
2024-12-24 21:13:47 -03:00
}