#![cfg(feature = "integration-tests")]
use firebase_rust_sdk::{App, AppOptions, Auth};
use std::env;
fn load_env() {
dotenvy::dotenv().ok();
}
async fn get_test_app() -> (App, String, String) {
load_env();
let api_key = env::var("FIREBASE_API_KEY")
.expect("FIREBASE_API_KEY must be set in .env file");
let project_id = env::var("FIREBASE_PROJECT_ID")
.expect("FIREBASE_PROJECT_ID must be set in .env file");
let email = env::var("TEST_USER_EMAIL")
.expect("TEST_USER_EMAIL must be set in .env file");
let password = env::var("TEST_USER_PASSWORD")
.expect("TEST_USER_PASSWORD must be set in .env file");
let app = App::create(AppOptions {
api_key,
project_id,
app_name: Some(format!("test-auth-{}", rand::random::<u32>())),
}).await
.expect("Failed to create App");
(app, email, password)
}
#[tokio::test]
async fn test_sign_in_with_email_password() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_with_email_and_password(&email, &password).await
.expect("Failed to sign in");
assert!(!result.user.uid.is_empty());
assert_eq!(result.user.email.as_deref(), Some(email.as_str()));
auth.sign_out().await.expect("Failed to sign out");
assert!(auth.current_user().await.is_none());
println!("✅ Email/password sign in works!");
}
#[tokio::test]
async fn test_anonymous_auth() {
let (app, _, _) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_anonymously().await
.expect("Failed to sign in anonymously");
assert!(result.user.is_anonymous);
assert!(!result.user.uid.is_empty());
let uid = result.user.uid.clone();
result.user.delete().await.expect("Failed to delete user");
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Anonymous auth works! (cleaned up user {})", uid);
}
#[tokio::test]
async fn test_create_and_delete_user() {
let (app, _, _) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let timestamp = chrono::Utc::now().timestamp();
let test_email = format!("test+{}@example.com", timestamp);
let test_password = "TempPassword123!";
let result = auth.create_user_with_email_and_password(&test_email, test_password).await
.expect("Failed to create user");
assert_eq!(result.user.email.as_deref(), Some(test_email.as_str()));
assert!(!result.user.uid.is_empty());
let uid = result.user.uid.clone();
auth.sign_out().await.expect("Failed to sign out");
let result2 = auth.sign_in_with_email_and_password(&test_email, test_password).await
.expect("Failed to sign in with new user");
assert_eq!(result2.user.uid, uid);
result2.user.delete().await.expect("Failed to delete user");
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Create/delete user works! (cleaned up user {})", uid);
}
#[tokio::test]
async fn test_token_refresh() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_with_email_and_password(&email, &password).await
.expect("Failed to sign in");
let user = result.user;
let token1 = user.get_id_token(false).await
.expect("Failed to get token");
let token2 = user.get_id_token(true).await
.expect("Failed to refresh token");
assert!(!token1.is_empty());
assert!(!token2.is_empty());
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Token refresh works!");
}
#[tokio::test]
async fn test_update_profile() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_with_email_and_password(&email, &password).await
.expect("Failed to sign in");
let user = result.user;
let timestamp = chrono::Utc::now().timestamp();
let new_name = format!("Test User {}", timestamp);
use firebase_rust_sdk::auth::UserProfile;
let profile = UserProfile {
display_name: Some(new_name.clone()),
photo_url: None,
};
user.update_profile(profile).await
.expect("Failed to update display name");
println!("Profile updated to: {}", new_name);
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Update profile works!");
}
#[tokio::test]
async fn test_password_reset() {
let (app, email, _) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
auth.send_password_reset_email(&email).await
.expect("Failed to send password reset email");
println!("✅ Password reset email sent! (check your inbox)");
}
#[tokio::test]
async fn test_user_reload() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_with_email_and_password(&email, &password).await
.expect("Failed to sign in");
let user = result.user;
let old_email = user.email.clone();
assert!(old_email.is_some());
auth.sign_out().await.expect("Failed to sign out");
println!("✅ User reload works!");
}
#[tokio::test]
async fn test_send_email_verification() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let result = auth.sign_in_with_email_and_password(&email, &password).await
.expect("Failed to sign in");
result.user.send_email_verification().await
.expect("Failed to send email verification");
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Email verification sent! (check your inbox)");
}
#[tokio::test]
async fn test_update_password() {
let (app, email, password) = get_test_app().await;
let auth = Auth::get_auth(&app).await
.expect("Failed to get Auth instance");
let timestamp = chrono::Utc::now().timestamp();
let test_email = format!("test+pwd{}@example.com", timestamp);
let old_password = "OldPassword123!";
let new_password = "NewPassword456!";
let result = auth.create_user_with_email_and_password(&test_email, old_password).await
.expect("Failed to create user");
let uid = result.user.uid.clone();
result.user.update_password(new_password).await
.expect("Failed to update password");
auth.sign_out().await.expect("Failed to sign out");
let result2 = auth.sign_in_with_email_and_password(&test_email, new_password).await
.expect("Failed to sign in with new password");
assert_eq!(result2.user.uid, uid);
result2.user.delete().await.expect("Failed to delete user");
auth.sign_out().await.expect("Failed to sign out");
println!("✅ Update password works! (cleaned up user {})", uid);
}