"""Cover cohort metadata, uploads and request errors using a temporary root and the app client. Fails if the API accepts unsafe files, bypasses the upload limit or loses stored metadata. """ from io import BytesIO import pytest from webapi import create_app COHORTS = "/governance/tea/reference-cohorts" @pytest.fixture def cohort_client(tmp_path, monkeypatch): """Wire the app to a temporary root so escaped writes and metadata errors are observable.""" root = tmp_path / "reference" monkeypatch.setenv("TEA_REFERENCE_ROOT", str(root)) app = create_app() app.config.update(TESTING=True) return app.test_client(), root def test_list_reference_cohorts(cohort_client): """Pin nested EDF counts and age-table metadata. Fail if either is omitted.""" client, root = cohort_client cohort = root / "reference-1" nested = cohort / "2026" nested.mkdir(parents=True) (root / "empty").mkdir() (root / "loose.edf").write_bytes(b"loose") (cohort / "study.edf").write_bytes(b"edf") (nested / "second.EDF").write_bytes(b"second") (cohort / "edades.csv").write_bytes(b"ages") response = client.get(COHORTS) assert response.status_code == 200 assert response.json == [ {"name": "empty", "edfs": 0, "size_bytes": 0, "has_ages": False, "path": str(root / "empty")}, {"name": "reference-1", "edfs": 2, "size_bytes": 13, "has_ages": True, "path": str(cohort)}, ] @pytest.mark.parametrize("filename", ["study.edf", "edades.csv"]) def test_upload_reference_file(cohort_client, filename): """Pin each permitted upload and the stored bytes. Fail if the upload leaves a staging file.""" client, root = cohort_client payload = b"reference data" response = client.post(f"{COHORTS}/sample-1/files", data={"file": (BytesIO(payload), filename)}) assert response.status_code == 201 assert response.json == {"name": "sample-1", "file": filename, "size_bytes": len(payload)} folder = root / "sample-1" assert list(folder.iterdir()) == [folder / filename] assert (folder / filename).read_bytes() == payload @pytest.mark.parametrize(("name", "filename"), [ ("sample", "other.csv"), ("sample", "../study.edf"), ("sample", r"..\study.edf"), ("sample", "nested/study.edf"), ("sample", r"nested\study.edf"), ("sample/other", "study.edf"), ("..", "study.edf"), ("sample..other", "study.edf"), ("bad%20name", "study.edf"), ("a" * 65, "study.edf"), ]) def test_reject_invalid_paths_and_extensions(cohort_client, name, filename): """Pin validation before writes. Fail if an unsafe component or unsupported extension is accepted.""" client, root = cohort_client response = client.post(f"{COHORTS}/{name}/files", data={"file": (BytesIO(b"edf"), filename)}) assert response.status_code == 400 assert response.json["error"] assert not root.exists() def test_reject_a_cohort_symlink(cohort_client, tmp_path): """Pin root confinement. Fail if a cohort symlink allows an upload outside the root.""" client, root = cohort_client outside = tmp_path / "outside" outside.mkdir() root.mkdir() (root / "linked").symlink_to(outside, target_is_directory=True) response = client.post(f"{COHORTS}/linked/files", data={"file": (BytesIO(b"edf"), "study.edf")}) assert response.status_code == 400 assert not list(outside.iterdir()) assert client.get(COHORTS).json == [] def test_missing_root_configuration(cohort_client, monkeypatch): """Pin a 503 on both endpoints. Fail if either silently falls back to a different root.""" client, _ = cohort_client monkeypatch.delenv("TEA_REFERENCE_ROOT") for response in (client.get(COHORTS), client.post(f"{COHORTS}/sample/files")): assert response.status_code == 503 assert response.json["error"] def test_upload_uses_app_size_limit(cohort_client): """Pin the existing JSON 413. Fail if multipart parsing bypasses the configured size limit.""" client, root = cohort_client client.application.config["MAX_CONTENT_LENGTH"] = 1024 response = client.post(f"{COHORTS}/sample/files", data={"file": (BytesIO(b"x" * 1024), "study.edf")}) assert response.status_code == 413 assert response.json["error"] assert not root.exists()