File size: 4,390 Bytes
3e6248e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
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