test_project_sync.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 team_name = f"sync_team_{_suffix}" 12 llm_name = f"sync_llm_{_suffix}" 13 proj_name = f"sync_proj_{_suffix}" 14 team_id = None 15 project_id = None 16 17 18 @pytest.fixture(scope="module") 19 def client(): 20 with TestClient(app) as c: 21 yield c 22 23 24 def test_setup(client): 25 """Create team, LLM, and a block project for sync endpoint tests.""" 26 global team_id, project_id 27 # Create LLM 28 client.post( 29 "/llms", 30 json={ 31 "name": llm_name, 32 "class_name": "OpenAI", 33 "options": {"model": "gpt-test", "api_key": "sk-fake"}, 34 "privacy": "public", 35 }, 36 auth=ADMIN, 37 ) 38 39 # Create team with LLM 40 resp = client.post( 41 "/teams", 42 json={"name": team_name, "users": [], "admins": [], "llms": [llm_name]}, 43 auth=ADMIN, 44 ) 45 assert resp.status_code in (200, 201) 46 team_id = resp.json()["id"] 47 48 # Create a block project 49 resp = client.post( 50 "/projects", 51 json={"name": proj_name, "type": "block", "team_id": team_id}, 52 auth=ADMIN, 53 ) 54 assert resp.status_code == 201 55 project_id = resp.json()["project"] 56 57 58 def test_sync_status(client): 59 """GET /projects/{id}/sync/status returns sync status info.""" 60 resp = client.get( 61 f"/projects/{project_id}/sync/status", 62 auth=ADMIN, 63 ) 64 assert resp.status_code == 200 65 data = resp.json() 66 assert "enabled" in data 67 assert "sources" in data 68 assert data["sources"] == 0 69 70 71 def test_sync_trigger_no_sources(client): 72 """POST /projects/{id}/sync/trigger with no sync sources should return 400.""" 73 resp = client.post( 74 f"/projects/{project_id}/sync/trigger", 75 auth=ADMIN, 76 ) 77 # Block projects return 400 ("Sync only available for RAG projects") 78 # or if it were RAG with no sources, also 400 79 assert resp.status_code == 400 80 81 82 def test_sync_status_persists(client): 83 """Verify sync status reflects project options.""" 84 # Initially no sync sources 85 resp = client.get( 86 f"/projects/{project_id}/sync/status", 87 auth=ADMIN, 88 ) 89 assert resp.status_code == 200 90 data = resp.json() 91 assert data["enabled"] is False 92 assert data["sources"] == 0 93 94 95 def test_cleanup(client): 96 client.delete(f"/projects/{project_id}", auth=ADMIN) 97 client.delete(f"/llms/{llm_name}", auth=ADMIN) 98 client.delete(f"/teams/{team_id}", auth=ADMIN)