File size: 3,496 Bytes
133609a |
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 |
import pytest
import httpx
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Base URL for the FastAPI application
BASE_URL = "http://localhost:7864"
# Supabase credentials from .env
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY")
SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
# Test user credentials
TEST_EMAIL = "test_user@example.com"
TEST_PASSWORD = "test_password"
# Ensure Supabase environment variables are set
if not all([SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY]):
pytest.skip("Supabase environment variables not set. Skipping tests.", allow_module_level=True)
@pytest.fixture(scope="module")
def client():
"""Provides a test client for the FastAPI application."""
# In a real scenario, you might run the app in a separate process
# or use TestClient from fastapi.testclient.
# For this automated test, we assume the app is running at BASE_URL.
with httpx.Client(base_url=BASE_URL) as client:
yield client
@pytest.fixture(scope="module")
def auth_headers(client):
"""Registers and logs in a test user, returning auth headers."""
# 1. Register user
signup_data = {"email": TEST_EMAIL, "password": TEST_PASSWORD}
response = client.post("/api/auth/signup", json=signup_data)
if response.status_code == 400 and "User already registered" in response.text:
print(f"User {TEST_EMAIL} already registered. Proceeding with login.")
elif response.status_code != 200:
response.raise_for_status() # Raise for other signup errors
# 2. Login user
login_data = {"email": TEST_EMAIL, "password": TEST_PASSWORD}
response = client.post("/api/auth/login", json=login_data)
response.raise_for_status()
token = response.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
def test_signup_and_login(client):
"""Tests user registration and login."""
# Ensure a unique email for signup test if running independently
unique_email = f"test_user_{os.urandom(4).hex()}@example.com"
signup_data = {"email": unique_email, "password": TEST_PASSWORD}
response = client.post("/api/auth/signup", json=signup_data)
assert response.status_code == 200 or (response.status_code == 400 and "User already registered" in response.text)
login_data = {"email": unique_email, "password": TEST_PASSWORD}
response = client.post("/api/auth/login", json=login_data)
response.raise_for_status()
assert "access_token" in response.json()
def test_generate_api_key(client, auth_headers):
"""Tests API key generation."""
response = client.post("/api/user/generate-api-key", headers=auth_headers)
response.raise_for_status()
assert "api_key" in response.json()
def test_get_api_keys(client, auth_headers):
"""Tests fetching API keys."""
response = client.get("/api/user/api-keys", headers=auth_headers)
response.raise_for_status()
assert isinstance(response.json(), list)
def test_get_proxy_data(client, auth_headers):
"""Tests retrieval of proxy data."""
response = client.get("/api/proxies", headers=auth_headers)
response.raise_for_status()
assert isinstance(response.json(), list)
# Optionally, assert on the structure of the returned proxy data
# For example:
# if response.json():
# assert "ip_address" in response.json()[0]
# assert "port" in response.json()[0]
|