ewere / tests /test_auth.py
andevs's picture
Create test_auth.py
a3ad7a2 verified
Raw
History Blame Contribute Delete
2.34 kB
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, Mock
def test_verify_token_valid(client: TestClient):
"""
Test token verification with valid token
"""
headers = {"Authorization": "Bearer valid_token"}
with patch('firebase_admin.auth.verify_id_token') as mock_verify:
mock_verify.return_value = {"uid": "test_user"}
response = client.get(
"/api/auth/verify",
headers=headers
)
assert response.status_code == 200
assert response.json()["user_id"] == "test_user"
def test_verify_token_invalid(client: TestClient):
"""
Test token verification with invalid token
"""
headers = {"Authorization": "Bearer invalid_token"}
with patch('firebase_admin.auth.verify_id_token') as mock_verify:
mock_verify.side_effect = Exception("Invalid token")
response = client.get(
"/api/auth/verify",
headers=headers
)
assert response.status_code == 401
assert "Invalid authentication token" in response.json()["detail"]
def test_verify_token_missing(client: TestClient):
"""
Test token verification with missing token
"""
response = client.get("/api/auth/verify")
assert response.status_code == 403 # FastAPI returns 403 for missing auth
def test_refresh_token(client: TestClient):
"""
Test token refresh
"""
response = client.post(
"/api/auth/refresh",
json={"refresh_token": "test_refresh_token"}
)
# Should return new token
assert response.status_code in [200, 401]
def test_user_registration(client: TestClient):
"""
Test user registration
"""
response = client.post(
"/api/auth/register",
json={
"email": "newuser@example.com",
"password": "Test123!",
"name": "New User"
}
)
assert response.status_code in [200, 201, 400] # May fail if user exists
def test_password_reset_request(client: TestClient):
"""
Test password reset request
"""
response = client.post(
"/api/auth/reset-password",
json={"email": "user@example.com"}
)
assert response.status_code in [200, 404] # 404 if user not found