File size: 5,687 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Shared pytest fixtures for E2E API tests

This file is intentionally standalone - it does NOT import from the backend
to keep E2E test dependencies minimal.
"""
import os
import secrets
import string
import logging

from dotenv import load_dotenv
load_dotenv()

import pytest
import httpx
import jwt
from datetime import datetime, timezone, timedelta
from typing import AsyncGenerator, Dict

from tests.config import E2ETestConfig

# Simple logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("e2e_tests")


# Register custom markers
def pytest_configure(config):
    config.addinivalue_line("markers", "e2e: End-to-end tests")
    config.addinivalue_line("markers", "slow: Slow tests (streaming, long runs)")
    config.addinivalue_line("markers", "billing: Tests requiring billing/credits")


@pytest.fixture(scope="session")
def test_config() -> E2ETestConfig:
    """Test configuration fixture - reads from environment variables"""
    return E2ETestConfig(
        base_url=os.getenv("TEST_API_URL", "http://localhost:8000/v1"),
        supabase_url=os.getenv("SUPABASE_URL", ""),
        supabase_anon_key=os.getenv("SUPABASE_ANON_KEY", ""),
        supabase_jwt_secret=os.getenv("SUPABASE_JWT_SECRET", ""),
        admin_api_key=os.getenv("KORTIX_ADMIN_API_KEY", ""),
    )


def _generate_random_yopmail() -> str:
    """Generate a random yopmail email address"""
    random_part = ''.join(secrets.choice(string.ascii_lowercase + string.digits) for _ in range(secrets.randbelow(5) + 8))
    return f"{random_part}@yopmail.com"


# Module-level cache for test user info
_cached_test_user: Dict[str, str] | None = None


async def _ensure_test_user_exists(test_config: E2ETestConfig) -> Dict[str, str]:
    """
    Create a test user via Supabase Admin API.

    Account initialization happens automatically when the user first
    hits an API endpoint (handled by the API server).
    """
    global _cached_test_user

    if _cached_test_user:
        logger.debug(f"Using cached test user: {_cached_test_user['email']}")
        return _cached_test_user

    from supabase import create_client, Client

    supabase_url = os.getenv("SUPABASE_URL")
    supabase_key = os.getenv("SUPABASE_SERVICE_ROLE_KEY")

    if not supabase_url or not supabase_key:
        raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY required for test user setup")

    client: Client = create_client(supabase_url, supabase_key)

    TEST_USER_EMAIL = _generate_random_yopmail()
    logger.info(f"Creating test user: {TEST_USER_EMAIL}")

    try:
        user_response = client.auth.admin.create_user({
            'email': TEST_USER_EMAIL,
            'password': test_config.test_user_password,
            'email_confirm': True,
            'user_metadata': {
                'test_user': True,
                'created_by': 'e2e_api_tests',
            }
        })

        user_id = user_response.user.id
        _cached_test_user = {
            "user_id": user_id,
            "email": TEST_USER_EMAIL
        }

        logger.info(f"✅ Created test user: {TEST_USER_EMAIL} (ID: {user_id})")

        # Create profile (some APIs may require this)
        try:
            client.table('profiles').insert({
                'user_id': user_id,
                'email': TEST_USER_EMAIL,
                'full_name': 'E2E Test User',
            }).execute()
        except Exception:
            pass  # Profile might be auto-created by trigger

        # Account initialization happens automatically when user hits API
        # No need to call initialize_user_account here

        return _cached_test_user

    except Exception as e:
        if "already been registered" in str(e):
            # Retry with new email
            TEST_USER_EMAIL = _generate_random_yopmail()
            user_response = client.auth.admin.create_user({
                'email': TEST_USER_EMAIL,
                'password': test_config.test_user_password,
                'email_confirm': True,
                'user_metadata': {'test_user': True}
            })
            user_id = user_response.user.id
            _cached_test_user = {"user_id": user_id, "email": TEST_USER_EMAIL}
            return _cached_test_user

        raise ValueError(f"Could not create test user: {e}")


@pytest.fixture(scope="function")
async def test_user(test_config: E2ETestConfig) -> Dict[str, str]:
    """Get or create a test user"""
    user_info = await _ensure_test_user_exists(test_config)

    print(f"\n{'='*60}")
    print(f"🧪 TEST USER: {user_info['email']}")
    print(f"{'='*60}\n")

    return user_info


@pytest.fixture
async def auth_token(test_user: Dict[str, str], test_config: E2ETestConfig) -> str:
    """Generate JWT token for test user"""
    jwt_secret = test_config.supabase_jwt_secret or os.getenv("SUPABASE_JWT_SECRET")

    if not jwt_secret:
        raise ValueError("SUPABASE_JWT_SECRET not configured")

    payload = {
        'sub': test_user["user_id"],
        'aud': 'authenticated',
        'role': 'authenticated',
        'iat': datetime.now(timezone.utc).timestamp(),
        'exp': (datetime.now(timezone.utc) + timedelta(hours=1)).timestamp(),
    }

    return jwt.encode(payload, jwt_secret, algorithm='HS256')


@pytest.fixture
async def client(test_config: E2ETestConfig, auth_token: str) -> AsyncGenerator[httpx.AsyncClient, None]:
    """Authenticated HTTP client for API requests"""
    async with httpx.AsyncClient(
        base_url=test_config.base_url,
        headers={"Authorization": f"Bearer {auth_token}"},
        timeout=test_config.request_timeout,
        follow_redirects=True
    ) as client:
        yield client