|
| 1 | +"""ISSUE-86: `exec_report_spec(..., token=...)` must authenticate the client it |
| 2 | +builds with `set_bearer_token(token)` instead of minting one from |
| 3 | +user/user_pass, on BOTH client-building paths (the format-row/find path and |
| 4 | +the analytic_function path). With no token, behaviour is unchanged |
| 5 | +(`create_egeria_bearer_token()`).""" |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pyegeria.view import format_set_executor as fse |
| 9 | +from pyegeria.core import mcp_adapter |
| 10 | + |
| 11 | + |
| 12 | +class _RecordingClient: |
| 13 | + """Stands in for any OMVS client class the executor instantiates.""" |
| 14 | + instances: list = [] |
| 15 | + |
| 16 | + def __init__(self, view_server, view_url, user_id=None, user_pwd=None, **kw): |
| 17 | + self.view_server = view_server |
| 18 | + self.platform_url = view_url |
| 19 | + self.user_id = user_id |
| 20 | + self.user_pwd = user_pwd |
| 21 | + self.token = None |
| 22 | + self.calls: list[str] = [] |
| 23 | + _RecordingClient.instances.append(self) |
| 24 | + |
| 25 | + def create_egeria_bearer_token(self, *a, **kw): |
| 26 | + self.calls.append("create") |
| 27 | + self.token = "minted" |
| 28 | + return self.token |
| 29 | + |
| 30 | + def set_bearer_token(self, token): |
| 31 | + self.calls.append("set") |
| 32 | + self.token = token |
| 33 | + |
| 34 | + def find_things(self, **kwargs): |
| 35 | + return [{"guid": "1", "displayName": "x"}] |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(autouse=True) |
| 39 | +def _reset_instances(): |
| 40 | + _RecordingClient.instances = [] |
| 41 | + yield |
| 42 | + _RecordingClient.instances = [] |
| 43 | + |
| 44 | + |
| 45 | +_FIND_FMT = { |
| 46 | + "action": { |
| 47 | + "function": "Fake.find_things", |
| 48 | + "required_params": [], |
| 49 | + "optional_params": [], |
| 50 | + "spec_params": {}, |
| 51 | + }, |
| 52 | + "target_type": "Referenceable", |
| 53 | +} |
| 54 | + |
| 55 | +_ANALYTIC_FMT = { |
| 56 | + "action": {"analytic_function": "fake.analytic", "analytic_spec_params": {}}, |
| 57 | + "target_type": "Referenceable", |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +def _patch_find_path(monkeypatch): |
| 62 | + monkeypatch.setattr(fse, "select_report_spec", lambda name, out: _FIND_FMT) |
| 63 | + monkeypatch.setattr(fse, "get_report_registry", lambda: {}) |
| 64 | + monkeypatch.setattr(fse, "_resolve_client_and_method", |
| 65 | + lambda decl: (_RecordingClient, "find_things")) |
| 66 | + |
| 67 | + |
| 68 | +def _patch_analytic_path(monkeypatch): |
| 69 | + def _analytic(client): |
| 70 | + return {"count": 3, "token_seen": client.token} |
| 71 | + monkeypatch.setattr(fse, "select_report_spec", lambda name, out: _ANALYTIC_FMT) |
| 72 | + monkeypatch.setattr(fse, "get_report_registry", lambda: {}) |
| 73 | + monkeypatch.setattr(fse, "_resolve_analytic_function", lambda decl: _analytic) |
| 74 | + monkeypatch.setattr(fse, "EgeriaTech", _RecordingClient) |
| 75 | + |
| 76 | + |
| 77 | +# --- format-row (find_method) path ------------------------------------------- |
| 78 | + |
| 79 | +def test_find_path_with_token_uses_set_bearer_token(monkeypatch): |
| 80 | + _patch_find_path(monkeypatch) |
| 81 | + result = fse.exec_report_spec( |
| 82 | + "Anything", output_format="DICT", view_server="vs", view_url="https://x", |
| 83 | + user="svc", user_pass="svc-pw", token="user-token", |
| 84 | + ) |
| 85 | + assert result["kind"] == "json" |
| 86 | + (client,) = _RecordingClient.instances |
| 87 | + assert client.calls == ["set"] |
| 88 | + assert client.token == "user-token" |
| 89 | + |
| 90 | + |
| 91 | +def test_find_path_without_token_mints_bearer_token(monkeypatch): |
| 92 | + _patch_find_path(monkeypatch) |
| 93 | + result = fse.exec_report_spec( |
| 94 | + "Anything", output_format="DICT", view_server="vs", view_url="https://x", |
| 95 | + user="svc", user_pass="svc-pw", |
| 96 | + ) |
| 97 | + assert result["kind"] == "json" |
| 98 | + (client,) = _RecordingClient.instances |
| 99 | + assert client.calls == ["create"] |
| 100 | + assert client.user_id == "svc" and client.user_pwd == "svc-pw" |
| 101 | + |
| 102 | + |
| 103 | +# --- analytic_function path --------------------------------------------------- |
| 104 | + |
| 105 | +def test_analytic_path_with_token_uses_set_bearer_token(monkeypatch): |
| 106 | + _patch_analytic_path(monkeypatch) |
| 107 | + result = fse.exec_report_spec( |
| 108 | + "Anything", output_format="DICT", view_server="vs", view_url="https://x", |
| 109 | + user="svc", user_pass="svc-pw", token="user-token", |
| 110 | + ) |
| 111 | + assert result == {"kind": "json", "data": {"count": 3, "token_seen": "user-token"}} |
| 112 | + (client,) = _RecordingClient.instances |
| 113 | + assert client.calls == ["set"] |
| 114 | + |
| 115 | + |
| 116 | +def test_analytic_path_without_token_mints_bearer_token(monkeypatch): |
| 117 | + _patch_analytic_path(monkeypatch) |
| 118 | + result = fse.exec_report_spec( |
| 119 | + "Anything", output_format="DICT", view_server="vs", view_url="https://x", |
| 120 | + user="svc", user_pass="svc-pw", |
| 121 | + ) |
| 122 | + assert result["data"]["token_seen"] == "minted" |
| 123 | + (client,) = _RecordingClient.instances |
| 124 | + assert client.calls == ["create"] |
| 125 | + |
| 126 | + |
| 127 | +def test_chart_path_threads_token(monkeypatch): |
| 128 | + """SERIES/BAR/PIE dispatch to _exec_analytic_chart before the Format-row |
| 129 | + lookup -- the token must survive that hop too.""" |
| 130 | + _patch_analytic_path(monkeypatch) |
| 131 | + monkeypatch.setattr(fse, "get_report_spec_heading", lambda name: "H", raising=False) |
| 132 | + fse.exec_report_spec( |
| 133 | + "Anything", output_format="BAR", view_server="vs", view_url="https://x", |
| 134 | + user="svc", user_pass="svc-pw", token="user-token", |
| 135 | + ) |
| 136 | + (client,) = _RecordingClient.instances |
| 137 | + assert client.calls == ["set"] |
| 138 | + assert client.token == "user-token" |
| 139 | + |
| 140 | + |
| 141 | +# --- MCP adapter passthrough -------------------------------------------------- |
| 142 | + |
| 143 | +def test_mcp_run_report_forwards_token(monkeypatch): |
| 144 | + seen = {} |
| 145 | + |
| 146 | + def _fake_exec(**kwargs): |
| 147 | + seen.update(kwargs) |
| 148 | + return {"kind": "empty"} |
| 149 | + |
| 150 | + monkeypatch.setattr(mcp_adapter, "exec_report_spec", _fake_exec) |
| 151 | + mcp_adapter.run_report(report="R", user="u", user_pass="p", token="tok") |
| 152 | + assert seen["token"] == "tok" |
| 153 | + assert seen["user"] == "u" and seen["user_pass"] == "p" |
| 154 | + |
| 155 | + |
| 156 | +def test_mcp_run_report_default_token_is_none(monkeypatch): |
| 157 | + seen = {} |
| 158 | + monkeypatch.setattr(mcp_adapter, "exec_report_spec", |
| 159 | + lambda **kw: seen.update(kw) or {"kind": "empty"}) |
| 160 | + mcp_adapter.run_report(report="R", user="u", user_pass="p") |
| 161 | + assert seen["token"] is None |
0 commit comments