import os os.environ.setdefault("BITRIX_SYNC_ENABLED", "false") os.environ.setdefault("BITRIX_SYNC_SERVICE_TOKEN", "test-sync-token-32-characters") import httpx import pytest from app.main import Settings, create_app class Probe: def __init__(self, fail=False): self.fail = fail self.calls = 0 async def check(self): self.calls += 1 if self.fail: raise OSError("down") async def close(self): pass @pytest.mark.asyncio async def test_disabled_semantics(): settings = Settings( bitrix_sync_enabled=False, bitrix_sync_service_token="test-sync-token-32-characters", ) app = create_app(settings) async with app.router.lifespan_context(app): async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test" ) as client: assert (await client.get("/health/live")).status_code == 200 ready = await client.get("/health/ready") assert ready.status_code == 503 assert ready.json()["reason"] == "sync_disabled" status = await client.get( "/internal/sync/v1/status", headers={"Authorization": "Bearer test-sync-token-32-characters"}, ) assert status.json()["state"] == "disabled" assert status.json()["crm_sync_implemented"] is False @pytest.mark.asyncio async def test_initial_probe_and_auth(): probe = Probe() settings = Settings( bitrix_sync_enabled=True, bitrix_sync_database_url="postgresql://unused/unused", bitrix_sync_service_token="test-sync-token-32-characters", bitrix_sync_db_check_interval_sec=60, ) app = create_app(settings, probe) async with app.router.lifespan_context(app): async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test" ) as client: assert probe.calls == 1 assert (await client.get("/health/ready")).status_code == 200 assert (await client.get("/internal/sync/v1/status")).status_code == 401