"""C7 — the gate runs first, in every handler, machine and UI alike (family contract).""" from __future__ import annotations import json import types import pytest import gradio_ui from src.endpoints import ENDPOINTS @pytest.fixture() def enforced(monkeypatch): monkeypatch.setenv("ACCESS_CONTROL", "enforce") monkeypatch.setenv("ALLOWED_IDS", "someone-else") monkeypatch.setenv("ADMIN_IDS", "an-admin") @pytest.mark.parametrize("name", list(ENDPOINTS)) def test_no_token_is_denied_with_no_run_id(enforced, name): payload = json.loads(getattr(gradio_ui, name)(request=None)) assert payload["status"] == "denied" assert payload["machine_auth"] == "no_token_header" assert "run_id" not in payload @pytest.mark.parametrize("name", list(ENDPOINTS)) def test_the_gate_runs_before_validation(enforced, name): """Bad input from an unauthenticated caller is denied, never refused: it learns nothing.""" payload = json.loads(getattr(gradio_ui, name)("what drugs target KRAS?", request=None)) assert payload["status"] == "denied" def test_an_unresolvable_token_is_denied(enforced, monkeypatch): monkeypatch.setattr(gradio_ui, "_resolve_token_identity", lambda token: None) request = types.SimpleNamespace(headers={"x-orchestrator-token": "Bearer not-a-real-token"}) payload = json.loads(gradio_ui.annotate_genes("KRAS", request=request)) assert payload["status"] == "denied" and payload["machine_auth"] == "token_unresolved" def test_a_resolved_but_unlisted_caller_is_denied(enforced, monkeypatch): monkeypatch.setattr(gradio_ui, "_resolve_token_identity", lambda token: "a-stranger") request = types.SimpleNamespace(headers={"x-orchestrator-token": "hf_test"}) payload = json.loads(gradio_ui.annotate_genes("KRAS", request=request)) assert payload["status"] == "denied" and payload["machine_auth"] == "not_allowlisted" def test_an_allowlisted_caller_gets_through_to_validation(enforced, monkeypatch): monkeypatch.setattr(gradio_ui, "_resolve_token_identity", lambda token: "someone-else") request = types.SimpleNamespace(headers={"x-orchestrator-token": "hf_test"}) payload = json.loads(gradio_ui.annotate_genes("what drugs target KRAS?", request=request)) assert payload["status"] == "refused" and payload["reason"] == "invalid_identifier" def test_a_non_string_argument_is_a_typed_error(monkeypatch): monkeypatch.delenv("ACCESS_CONTROL", raising=False) payload = json.loads(gradio_ui.drugs_for_targets(["KRAS"], request=None)) assert payload["status"] == "error" assert payload["machine_input"] == "genes_not_a_string" def test_the_ui_handler_runs_the_gate_too(enforced): summary, raw = gradio_ui._ui_call("annotate_genes")("KRAS", profile=None) assert "Access denied" in summary assert json.loads(raw)["status"] == "denied" def test_gate_off_lets_a_call_through(monkeypatch, replay): monkeypatch.delenv("ACCESS_CONTROL", raising=False) replay("q3_annotate_elavl1") # installs the replay client; the machine call below reuses it payload = json.loads(gradio_ui.annotate_genes("ELAVL1", request=None)) assert payload["status"] == "ok" and payload["rows"][0]["symbol"] == "ELAVL1"