-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase_test_with_emulator.py
50 lines (41 loc) · 1.65 KB
/
firebase_test_with_emulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import firebase_admin
from firebase_admin import auth, firestore, credentials
# Connect to Firebase Emulator
cred = credentials.Certificate("./service_account_key_example.json")
firebase_admin.initialize_app(cred, {
"projectId": "demo-project"
})
# Set Firebase Emulator URLs
import os
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["FIREBASE_AUTH_EMULATOR_HOST"] = "localhost:9099"
# Function to authenticate a user using the local Firebase Emulator
def authenticate_user_emulator(uid):
user = auth.get_user(uid)
token = auth.create_custom_token(uid)
return {"uid": user.uid, "token": token}
# Function to fetch user data from Firestore in the Emulator
def get_user_data_emulator(uid):
db = firestore.client()
doc_ref = db.collection("users").document(uid)
doc = doc_ref.get()
return doc.to_dict() if doc.exists else None
# Integration test with the Firebase Emulator
def test_firebase_emulator():
# Create test user
try:
user = auth.create_user(uid="test-user", email="[email protected]", password="password123")
assert user.uid == "test-user"
except firebase_admin._auth_utils.UidAlreadyExistsError:
pass
# Authenticate and get a token
result = authenticate_user_emulator("test-user")
assert "token" in result
# Write user data to Firestore
db = firestore.client()
db.collection("users").document("test-user").set({"name": "John Doe", "email": "[email protected]"})
# Retrieve user data
user_data = get_user_data_emulator("test-user")
assert user_data["name"] == "John Doe"
assert user_data["email"] == "[email protected]"
test_firebase_emulator()