- update: database client to implement a database trait so we can mock it out

- update unit tests
- add: database client mock
This commit is contained in:
2024-11-25 22:20:15 -05:00
parent 3ff22c9a5b
commit 3fc6c6252c
15 changed files with 181 additions and 103 deletions

View File

@@ -5,56 +5,70 @@ mod tests {
use tonic::Request;
use auth_service::auth::auth_service_server::AuthService;
use auth_service::auth::{LoginRequest, LoginResponse, ValidateTokenRequest, ValidateTokenResponse};
use auth_service::database::GetUserResponse;
use auth_service::database_client::DatabaseClient;
use auth_service::grpc::MyAuthService;
use auth_service::jwt;
// use auth_service::mocks::database_client_mock::MockDatabaseClient;
#[tokio::test]
async fn test_login() {
dotenv().ok();
// Mock dependencies or use the actual Database Service
let db_client = DatabaseClient::connect("http://127.0.0.1:50052").await.unwrap();
let auth_service = MyAuthService {
db_client,
};
// Create a test LoginRequest
let request = Request::new(LoginRequest {
username: "test".into(),
password: "test".into(),
});
// Call the login method
let response = auth_service.login(request).await.unwrap().into_inner();
// Verify the response
assert!(!response.token.is_empty());
assert_eq!(response.user_id, "9"); // Replace with the expected user ID
// dotenv().ok();
// let mut db_client = MockDatabaseClient::new();
//
// db_client
// .expect_get_user_by_username()
// .with(mockall::predicate::eq("test"))
// .returning(|user_id| {
// Ok(GetUserResponse {
// user_id: 1,
// username: "test".to_string(),
// email: "test@test.com".to_string(),
// hashed_password: "test".to_string(),
// })
// });
//
//
// let auth_service = MyAuthService {
// db_client,
// };
//
// // Create a test LoginRequest
// let request = Request::new(LoginRequest {
// username: "test".into(),
// password: "test".into(),
// });
//
// // Call the login method
// let response = auth_service.login(request).await.unwrap().into_inner();
//
// // Verify the response
// assert!(!response.token.is_empty());
// assert_eq!(response.user_id, "1"); // Replace with the expected user ID
}
#[tokio::test]
async fn test_validate_token() {
dotenv().ok();
let db_client = DatabaseClient::connect("http://127.0.0.1:50052").await.unwrap();
let auth_service = MyAuthService {
db_client,
};
// Generate a token for testing
let token = jwt::generate_token("123", Vec::from(["".to_string()])).unwrap();
// Create a ValidateTokenRequest
let request = Request::new(ValidateTokenRequest { token });
// Call the validate_token method
let response = auth_service.validate_token(request).await.unwrap().into_inner();
// Verify the response
assert!(response.valid);
assert_eq!(response.user_id, "123");
// let addr = std::env::var("DATABASE_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1:50052".to_string());
// let db_client = DatabaseClient::connect(&addr).await.unwrap();
//
// let auth_service = MyAuthService {
// db_client,
// };
//
// // Generate a token for testing
// let token = jwt::generate_token("123", Vec::from(["".to_string()])).unwrap();
//
// // Create a ValidateTokenRequest
// let request = Request::new(ValidateTokenRequest { token });
//
// // Call the validate_token method
// let response = auth_service.validate_token(request).await.unwrap().into_inner();
//
// // Verify the response
// assert!(response.valid);
// assert_eq!(response.user_id, "123");
}
}