Files
computer-playbook/tests/unit/python/plugins/lookup/test_mcp_client_bearer.py
T
kevinveenbirkenbachandClaude Opus 5.5 3610e7b9ef fix(code-scanning): resolve the alerts that describe real code
Of the 76 open code scanning alerts, 39 point at code that can change.

svc-ai-mcp-adapter:
- A path argument that is empty or made only of dots is refused.
  Percent-encoding keeps a separator inside its segment, but "." and ".."
  need none, so an upstream that resolves dot segments could answer from a
  path the contract does not name.
- parse_duration splits on the units. The pattern it used before rescanned
  a long run of digits from every start position.

Tests and the README generators:
- Every Jinja environment built with autoescape=False and a "noqa: S701"
  uses select_autoescape now, as six test modules and
  utils/templating/jinja.py already did. It escapes templates named
  *.html, *.htm and *.xml, which none of these call sites loads.
- Dead code goes: a doubled "rel" block in the email lint, an unread
  _SKIP_DIRS, and a module that _resolve returned to no caller.
- Two "except ...: pass" blocks become contextlib.suppress and
  assertRaises.
- Nine test modules import unittest.mock one way instead of two, and one
  lookup test imports its module once.

Closes #807

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-06 09:18:00 +02:00

110 lines
3.4 KiB
Python

"""Unit tests for ``plugins/lookup/mcp_client_bearer.py``.
The sub-lookups are stubbed so the tests stay hermetic: what matters is that
the source declared in ``mcp.credential`` decides which secret comes back, and
that a provider fronted by an adapter never answers with the token its upstream
principal holds.
"""
from __future__ import annotations
import importlib.util
import unittest
from unittest.mock import MagicMock, patch
from ansible.errors import AnsibleError
from . import PROJECT_ROOT
def _load_module():
path = PROJECT_ROOT / "plugins/lookup/mcp_client_bearer.py"
spec = importlib.util.spec_from_file_location("lookup_mcp_client_bearer", str(path))
module = importlib.util.module_from_spec(spec)
assert spec and spec.loader
spec.loader.exec_module(module)
return module
class _DummyTemplar:
def __init__(self):
self.available_variables = {}
class TestMcpClientBearerLookup(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.mod = _load_module()
def _run(self, terms, credential, role_credentials=None, users=None):
lookup = self.mod.LookupModule()
lookup._templar = _DummyTemplar()
lookup._loader = MagicMock()
class _StubConfig:
def run(self, terms_, variables=None, **kwargs):
if terms_[1] == "mcp.credential":
return [credential]
return [role_credentials if role_credentials is not None else {}]
class _StubUsers:
def run(self, terms_, variables=None, **kwargs):
return [users if users is not None else {}]
def _dispatch(name, **_kwargs):
return _StubConfig() if name == "config" else _StubUsers()
with patch.object(self.mod.lookup_loader, "get", side_effect=_dispatch):
return lookup.run(terms, variables={})
def test_an_adapter_answers_with_its_own_rendered_bearer(self):
credential = {
"owner": "mcp-web-app-gitea",
"source": "credentials",
"key": "mcp_bearer",
}
self.assertEqual(
["adapter-secret"],
self._run(
["web-app-gitea"],
credential,
role_credentials={"mcp_bearer": "adapter-secret"},
users={"tokens": {"web-app-gitea": "upstream-pat"}},
),
)
def test_a_native_provider_answers_with_the_stored_token(self):
credential = {
"owner": "mcp-web-app-nextcloud",
"source": "token_store",
"key": "web-app-nextcloud",
}
self.assertEqual(
["stored-token"],
self._run(
["web-app-nextcloud"],
credential,
users={"tokens": {"web-app-nextcloud": "stored-token"}},
),
)
def test_a_provider_without_a_secret_yet_reads_empty(self):
credential = {
"owner": "mcp-web-app-gitea",
"source": "credentials",
"key": "mcp_bearer",
}
self.assertEqual([""], self._run(["web-app-gitea"], credential))
def test_an_undeclared_credential_raises(self):
with self.assertRaises(AnsibleError):
self._run(["web-app-gitea"], None)
def test_a_missing_application_id_raises(self):
with self.assertRaises(AnsibleError):
self._run([], {})
if __name__ == "__main__":
unittest.main()