from __future__ import annotations import importlib import sys import pytest from fastapi.testclient import TestClient @pytest.fixture() def client(tmp_path, monkeypatch): (tmp_path / "model.cif").write_text("data_model\n", encoding="utf-8") (tmp_path / "other.mmcif").write_text("data_other\n", encoding="utf-8") (tmp_path / "secret.txt").write_text("not public\n", encoding="utf-8") monkeypatch.setenv("AF3_SANDBOX_DATA_ROOT", str(tmp_path)) sys.modules.pop("apps.web.af3_sandbox.main", None) module = importlib.import_module("apps.web.af3_sandbox.main") with TestClient(module.app) as test_client: yield test_client def test_health_reports_data_root(client): response = client.get("/api/health") assert response.status_code == 200 body = response.json() assert body["status"] == "ok" assert body["data_root_available"] is True assert body["molstar_version"] == "5.11.0" def test_list_only_structure_files(client): response = client.get("/api/structures") assert response.status_code == 200 assert [item["name"] for item in response.json()["structures"]] == [ "model.cif", "other.mmcif", ] def test_structure_download_and_rejections(client): response = client.get("/api/structures/model.cif") assert response.status_code == 200 assert response.content == b"data_model\n" assert response.headers["content-type"].startswith("chemical/x-mmcif") assert client.get("/api/structures/secret.txt").status_code == 404 assert client.get("/api/structures/%2E%2E%2Fsecret.txt").status_code in {400, 404} def test_missing_data_root_is_reported(tmp_path, monkeypatch): missing = tmp_path / "missing" monkeypatch.setenv("AF3_SANDBOX_DATA_ROOT", str(missing)) sys.modules.pop("apps.web.af3_sandbox.main", None) module = importlib.import_module("apps.web.af3_sandbox.main") with TestClient(module.app) as test_client: health = test_client.get("/api/health") structures = test_client.get("/api/structures") assert health.status_code == 200 assert health.json()["data_root_available"] is False assert structures.status_code == 200 assert structures.json()["structures"] == []