60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use dotenv::dotenv;
|
|
use tonic::Request;
|
|
use auth_service::auth::auth_service_server::AuthService;
|
|
use auth_service::auth::{LoginRequest, LoginResponse, ValidateTokenRequest, ValidateTokenResponse};
|
|
use auth_service::database_client::DatabaseClient;
|
|
use auth_service::grpc::MyAuthService;
|
|
use auth_service::jwt;
|
|
|
|
#[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
|
|
}
|
|
|
|
#[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");
|
|
}
|
|
} |