from fastapi.testclient import TestClient from app.main import app from app.core.database import Base, engine from app.models.models import BetaApplication import pytest from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.core.config import settings import jwt from datetime import datetime, timedelta # Create test database SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def override_get_db(): try: db = TestingSessionLocal() yield db finally: db.close() app.dependency_overrides[get_db] = override_get_db client = TestClient(app) def create_test_token(): expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) to_encode = {"sub": settings.ADMIN_EMAIL, "exp": expire} return jwt.encode(to_encode, settings.SECRET_KEY, algorithm="HS256") @pytest.fixture(autouse=True) def setup_database(): Base.metadata.create_all(bind=engine) yield Base.metadata.drop_all(bind=engine) def test_health_check(): response = client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "healthy"} def test_admin_login_success(): response = client.post( "/api/admin/login", data={ "username": settings.ADMIN_EMAIL, "password": settings.ADMIN_PASSWORD } ) assert response.status_code == 200 assert "access_token" in response.json() assert response.json()["token_type"] == "bearer" def test_admin_login_failure(): response = client.post( "/api/admin/login", data={ "username": "wrong@email.com", "password": "wrongpassword" } ) assert response.status_code == 401 def test_get_applications_unauthorized(): response = client.get("/api/admin/applications") assert response.status_code == 401 def test_get_applications_authorized(): token = create_test_token() response = client.get( "/api/admin/applications", headers={"Authorization": f"Bearer {token}"} ) assert response.status_code == 200 assert "applications" in response.json() def test_create_beta_application(): response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "use_case": "Testing" } ) assert response.status_code == 200 data = response.json() assert data["email"] == "test@example.com" assert data["company"] == "Test Company" assert data["use_case"] == "Testing" assert data["status"] == "PENDING" def test_update_application_status(): # First create an application create_response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "use_case": "Testing" } ) application_id = create_response.json()["id"] # Update the status token = create_test_token() response = client.patch( f"/api/admin/applications/{application_id}", headers={"Authorization": f"Bearer {token}"}, json={"status": "APPROVED"} ) assert response.status_code == 200 def test_generate_records(): response = client.post( "/api/generator/generate", json={ "record_type": "clinical_note", "count": 1 } ) assert response.status_code == 200 data = response.json() assert len(data) == 1 assert "type" in data[0] assert "content" in data[0] assert "metadata" in data[0] def test_create_application(): response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "useCase": "Testing" } ) assert response.status_code == 200 data = response.json() assert data["email"] == "test@example.com" assert data["company"] == "Test Company" assert data["useCase"] == "Testing" assert data["status"] == "pending" def test_create_duplicate_application(): # Create first application client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "useCase": "Testing" } ) # Try to create duplicate response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Another Company", "useCase": "Another Use Case" } ) assert response.status_code == 400 assert response.json()["detail"] == "Email already registered" def test_verify_application(): # Create application response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "useCase": "Testing" } ) application_id = response.json()["id"] # Try to verify unapproved application response = client.post( "/api/beta/verify", json={"application_id": application_id} ) assert response.status_code == 403 assert response.json()["detail"] == "Application not approved" def test_admin_login(): response = client.post( "/api/admin/login", data={ "username": "admin@synthex.com", "password": "admin123" } ) assert response.status_code == 200 data = response.json() assert "access_token" in data assert data["token_type"] == "bearer" def test_admin_login_invalid_credentials(): response = client.post( "/api/admin/login", data={ "username": "admin@synthex.com", "password": "wrong_password" } ) assert response.status_code == 401 assert response.json()["detail"] == "Incorrect email or password" def test_update_application(): # Create application response = client.post( "/api/beta/apply", json={ "email": "test@example.com", "company": "Test Company", "useCase": "Testing" } ) application_id = response.json()["id"] # Login as admin login_response = client.post( "/api/admin/login", data={ "username": "admin@synthex.com", "password": "admin123" } ) token = login_response.json()["access_token"] # Update application response = client.patch( f"/api/admin/applications/{application_id}", json={"status": "approved"}, headers={"Authorization": f"Bearer {token}"} ) assert response.status_code == 200 assert response.json()["message"] == "Application updated successfully" # Verify application response = client.post( "/api/beta/verify", json={"application_id": application_id} ) assert response.status_code == 200 assert response.json()["message"] == "Access granted"