test_project_health.py
1 import random 2 import pytest 3 from fastapi.testclient import TestClient 4 5 from restai.config import RESTAI_DEFAULT_PASSWORD 6 from restai.main import app 7 8 ADMIN = ("admin", RESTAI_DEFAULT_PASSWORD) 9 10 _suffix = str(random.randint(0, 999999)) 11 test_username = f"health_user_{_suffix}" 12 test_password = "health_test_pass" 13 14 15 @pytest.fixture(scope="module") 16 def client(): 17 with TestClient(app) as c: 18 yield c 19 20 21 def test_setup(client): 22 """Create a restricted user for permission tests.""" 23 resp = client.post( 24 "/users", 25 json={ 26 "username": test_username, 27 "password": test_password, 28 "admin": False, 29 "private": False, 30 "is_restricted": True, 31 }, 32 auth=ADMIN, 33 ) 34 assert resp.status_code in (200, 201) 35 36 37 def test_projects_health(client): 38 """GET /projects/health as admin returns a list of project health scores.""" 39 resp = client.get("/projects/health", auth=ADMIN) 40 assert resp.status_code == 200 41 data = resp.json() 42 assert isinstance(data, list) 43 # Each entry should have the expected fields if projects exist 44 if len(data) > 0: 45 item = data[0] 46 assert "project_id" in item 47 assert "health" in item 48 49 50 def test_projects_health_non_admin(client): 51 """GET /projects/health as restricted user should still return 200 (read endpoint).""" 52 resp = client.get("/projects/health", auth=(test_username, test_password)) 53 assert resp.status_code == 200 54 data = resp.json() 55 assert isinstance(data, list) 56 57 58 def test_cleanup(client): 59 client.delete(f"/users/{test_username}", auth=ADMIN)