apigateway / tests /e2e /test_authenticated_flows_e2e.py
jebin2's picture
feat: Add comprehensive E2E testing framework with authenticated flows
3e6248e
"""
E2E Tests for Authenticated Flows
These tests use REAL authentication:
1. Create a user directly in the database
2. Generate a valid JWT token
3. Make API calls with that token
4. Verify actual business logic responses
"""
import pytest
class TestAuthenticatedUserInfoE2E:
"""Test authenticated endpoints with real user."""
def test_get_credit_balance(self, authenticated_client):
"""Get credit balance with valid token - verifies auth works."""
client, user_data = authenticated_client
response = client.get("/credits/balance")
assert response.status_code == 200, f"Expected 200, got {response.status_code}: {response.text}"
data = response.json()
assert data["credits"] == user_data["credits"]
assert data["user_id"] == user_data["user_id"]
class TestAuthenticatedCreditsE2E:
"""Test credits endpoints with real authenticated user."""
def test_get_credit_balance(self, authenticated_client):
"""Get credit balance for authenticated user."""
client, user_data = authenticated_client
response = client.get("/credits/balance")
assert response.status_code == 200
data = response.json()
assert data["credits"] == user_data["credits"]
assert data["user_id"] == user_data["user_id"]
def test_get_credit_history(self, authenticated_client):
"""Get credit history for authenticated user."""
client, user_data = authenticated_client
response = client.get("/credits/history")
assert response.status_code == 200
data = response.json()
assert "history" in data
assert data["current_balance"] == user_data["credits"]
class TestAuthenticatedGeminiE2E:
"""Test Gemini endpoints with real authenticated user."""
def test_get_models(self, authenticated_client):
"""Get available models."""
client, user_data = authenticated_client
response = client.get("/gemini/models")
assert response.status_code == 200
data = response.json()
assert "models" in data
assert "video_generation" in data["models"]
# text_generation might not be exposed in this endpoint yet
# assert "text_generation" in data["models"]
def test_get_jobs_empty(self, authenticated_client):
"""Get jobs list (empty for new user)."""
client, user_data = authenticated_client
response = client.get("/gemini/jobs")
assert response.status_code == 200
data = response.json()
assert "jobs" in data
# New user has no jobs
assert len(data["jobs"]) == 0
def test_generate_text_request(self, authenticated_client):
"""Submit text generation request (will fail without real Gemini API, but tests the flow)."""
client, user_data = authenticated_client
response = client.post("/gemini/generate-text", json={
"prompt": "Hello, write a short greeting"
})
# Should either succeed with job_id or fail due to missing API key (both are valid)
# 402 = insufficient credits, 500 = API error, 200 = success
assert response.status_code in [200, 201, 402, 500, 503]
if response.status_code in [200, 201]:
data = response.json()
assert "job_id" in data
class TestAuthenticatedPaymentsE2E:
"""Test payments endpoints with real authenticated user."""
def test_get_payment_history_empty(self, authenticated_client):
"""Get payment history (empty for new user)."""
client, user_data = authenticated_client
response = client.get("/payments/history")
assert response.status_code == 200
data = response.json()
assert "transactions" in data
assert len(data["transactions"]) == 0
class TestAuthenticatedLogoutE2E:
"""Test logout with real authenticated user."""
def test_logout(self, authenticated_client):
"""Logout invalidates token."""
client, user_data = authenticated_client
response = client.post("/auth/logout")
assert response.status_code == 200
data = response.json()
assert data["success"] is True