2023-07-14 22:02:26 +03:00
|
|
|
import os
|
2024-01-27 03:10:16 +03:00
|
|
|
import socket
|
2023-07-14 22:02:26 +03:00
|
|
|
|
|
|
|
import pytest
|
2023-11-13 15:53:25 +03:00
|
|
|
from dotenv import load_dotenv
|
2023-07-14 22:02:26 +03:00
|
|
|
from fastapi.testclient import TestClient
|
2023-11-13 15:53:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def load_env():
|
2024-01-27 03:10:16 +03:00
|
|
|
load_dotenv(".env_test", verbose=True, override=True)
|
|
|
|
|
|
|
|
# Testing socket connection
|
|
|
|
host, port = "localhost", 54321
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
try:
|
|
|
|
s.connect((host, port))
|
|
|
|
print(f"Connection to {host} on port {port} succeeded.")
|
|
|
|
except socket.error as e:
|
|
|
|
print(f"Connection to {host} on port {port} failed: {e}")
|
|
|
|
|
|
|
|
print("Loaded SUPABASE_URL:", os.getenv("SUPABASE_URL"))
|
2023-11-13 15:53:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def verify_env_variables():
|
|
|
|
required_vars = [
|
|
|
|
"SUPABASE_URL",
|
|
|
|
"SUPABASE_SERVICE_KEY",
|
|
|
|
"OPENAI_API_KEY",
|
|
|
|
"JWT_SECRET_KEY",
|
|
|
|
"CELERY_BROKER_URL",
|
|
|
|
]
|
|
|
|
missing_vars = [var for var in required_vars if not os.getenv(var)]
|
|
|
|
|
|
|
|
if missing_vars:
|
|
|
|
missing_vars_str = ", ".join(missing_vars)
|
|
|
|
pytest.fail(f"Required environment variables are missing: {missing_vars_str}")
|
2023-07-14 22:02:26 +03:00
|
|
|
|
|
|
|
|
2024-01-27 03:10:16 +03:00
|
|
|
@pytest.fixture(scope="session")
|
2023-07-14 22:02:26 +03:00
|
|
|
def client():
|
2023-11-13 15:53:25 +03:00
|
|
|
from main import app
|
|
|
|
|
2024-01-27 03:10:16 +03:00
|
|
|
print("CLIENT_SUPABASE_URL:", os.getenv("SUPABASE_URL")) # For debugging
|
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
return TestClient(app)
|
|
|
|
|
|
|
|
|
2024-01-27 03:10:16 +03:00
|
|
|
@pytest.fixture(scope="session")
|
2023-07-14 22:02:26 +03:00
|
|
|
def api_key():
|
|
|
|
API_KEY = os.getenv("CI_TEST_API_KEY")
|
|
|
|
if not API_KEY:
|
|
|
|
raise ValueError(
|
|
|
|
"CI_TEST_API_KEY environment variable not set. Cannot run tests."
|
|
|
|
)
|
|
|
|
return API_KEY
|