Spaces:
Sleeping
Sleeping
File size: 7,204 Bytes
3f61e65 22d5408 3f61e65 22d5408 3f61e65 22d5408 3f61e65 |
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
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" |