|
1 | | -"""Registration metadata: what an agent reports about itself at startup.""" |
| 1 | +"""Startup registration metadata and credential compatibility.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import os |
| 6 | +import json |
| 7 | +from unittest.mock import AsyncMock, call |
| 8 | + |
| 9 | +import httpx |
5 | 10 | import pytest |
6 | 11 |
|
7 | | -from agentex.lib.utils.registration import build_registration_metadata |
| 12 | +from agentex.lib.utils import registration |
| 13 | +from agentex.lib.utils.registration import register_agent, build_registration_metadata |
8 | 14 | from agentex.lib.environment_variables import EnvironmentVariables |
9 | 15 |
|
10 | 16 | SHA = "b362b171a9c4e1f09d8e7a6b5c4d3e2f1a0b9c8d" |
@@ -47,3 +53,153 @@ def model_dump(self): |
47 | 53 | "deployment_id": "dep-1", |
48 | 54 | "agent_card": {"name": "sample"}, |
49 | 55 | } |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def registration_env(monkeypatch): |
| 60 | + for name in ("AGENT_ID", "AGENT_NAME", "AGENT_API_KEY"): |
| 61 | + monkeypatch.delenv(name, raising=False) |
| 62 | + monkeypatch.setattr(registration, "refreshed_environment_variables", None, raising=False) |
| 63 | + return _env(AGENTEX_BASE_URL="https://agentex.example.test/") |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def retry_sleep(monkeypatch): |
| 68 | + sleep = AsyncMock() |
| 69 | + monkeypatch.setattr(registration.asyncio, "sleep", sleep) |
| 70 | + return sleep |
| 71 | + |
| 72 | + |
| 73 | +def _success(**overrides): |
| 74 | + return httpx.Response(200, json={"id": "agent-1", "name": "sample-agent", **overrides}) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("configured_key", [None, "", "configured-agent-key"]) |
| 78 | +@pytest.mark.parametrize("response_key", [{}, {"agent_api_key": None}, {"agent_api_key": ""}]) |
| 79 | +async def test_registration_preserves_configured_key_when_response_has_no_key( |
| 80 | + registration_env, respx_mock, configured_key, response_key |
| 81 | +): |
| 82 | + registration_env.AGENT_API_KEY = configured_key |
| 83 | + if configured_key is not None: |
| 84 | + os.environ["AGENT_API_KEY"] = configured_key |
| 85 | + route = respx_mock.post("https://agentex.example.test/agents/register").mock(return_value=_success(**response_key)) |
| 86 | + |
| 87 | + await register_agent(registration_env) |
| 88 | + |
| 89 | + request = route.calls.last.request |
| 90 | + assert route.call_count == 1 |
| 91 | + assert request.headers.get("x-agent-api-key") == (configured_key or None) |
| 92 | + assert "authorization" not in request.headers |
| 93 | + assert "agent_api_key" not in json.loads(request.content) |
| 94 | + assert registration_env.AGENT_API_KEY == configured_key |
| 95 | + assert os.environ.get("AGENT_API_KEY") == configured_key |
| 96 | + assert registration_env.AGENT_ID == os.environ["AGENT_ID"] == "agent-1" |
| 97 | + assert registration_env.AGENT_NAME == os.environ["AGENT_NAME"] == "sample-agent" |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("configured_key", [None, "older-agent-key"]) |
| 101 | +async def test_registration_accepts_returned_key(registration_env, respx_mock, configured_key): |
| 102 | + registration_env.AGENT_API_KEY = configured_key |
| 103 | + route = respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 104 | + return_value=_success(agent_api_key="returned-agent-key") |
| 105 | + ) |
| 106 | + |
| 107 | + await register_agent(registration_env) |
| 108 | + |
| 109 | + assert route.calls.last.request.headers.get("x-agent-api-key") == configured_key |
| 110 | + assert registration_env.AGENT_API_KEY == os.environ["AGENT_API_KEY"] == "returned-agent-key" |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("status", [401, 403]) |
| 114 | +async def test_rejected_registration_never_retries_anonymously( |
| 115 | + registration_env, respx_mock, retry_sleep, caplog, status |
| 116 | +): |
| 117 | + key = "configured-agent-key" |
| 118 | + registration_env.AGENT_API_KEY = key |
| 119 | + os.environ["AGENT_API_KEY"] = key |
| 120 | + route = respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 121 | + return_value=httpx.Response(status, text=f"Rejected credential: {key}") |
| 122 | + ) |
| 123 | + |
| 124 | + with pytest.raises(RuntimeError, match=f"Status: {status}") as exc: |
| 125 | + await register_agent(registration_env) |
| 126 | + |
| 127 | + assert route.call_count == 3 |
| 128 | + assert all(item.request.headers["x-agent-api-key"] == key for item in route.calls) |
| 129 | + assert registration_env.AGENT_API_KEY == os.environ["AGENT_API_KEY"] == key |
| 130 | + assert registration_env.AGENT_ID is None |
| 131 | + assert "AGENT_ID" not in os.environ |
| 132 | + assert retry_sleep.await_args_list == [call(5), call(10)] |
| 133 | + assert key not in caplog.text |
| 134 | + assert key not in str(exc.value) |
| 135 | + |
| 136 | + |
| 137 | +async def test_transient_failure_retries_with_configured_key(registration_env, respx_mock, retry_sleep): |
| 138 | + registration_env.AGENT_API_KEY = "configured-agent-key" |
| 139 | + route = respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 140 | + side_effect=[httpx.Response(503), _success(agent_api_key="returned-agent-key")] |
| 141 | + ) |
| 142 | + |
| 143 | + await register_agent(registration_env) |
| 144 | + |
| 145 | + assert route.call_count == 2 |
| 146 | + assert all(item.request.headers["x-agent-api-key"] == "configured-agent-key" for item in route.calls) |
| 147 | + assert registration_env.AGENT_API_KEY == os.environ["AGENT_API_KEY"] == "returned-agent-key" |
| 148 | + retry_sleep.assert_awaited_once_with(5) |
| 149 | + |
| 150 | + |
| 151 | +async def test_transport_error_does_not_expose_credential(registration_env, respx_mock, retry_sleep, caplog): |
| 152 | + key = "configured-agent-key" |
| 153 | + registration_env.AGENT_API_KEY = key |
| 154 | + route = respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 155 | + side_effect=httpx.ReadTimeout(f"Timeout sending {key}") |
| 156 | + ) |
| 157 | + |
| 158 | + with pytest.raises(RuntimeError, match="ReadTimeout") as exc: |
| 159 | + await register_agent(registration_env) |
| 160 | + |
| 161 | + assert route.call_count == 3 |
| 162 | + assert all(item.request.headers["x-agent-api-key"] == key for item in route.calls) |
| 163 | + assert key not in caplog.text |
| 164 | + assert key not in str(exc.value) |
| 165 | + assert exc.value.__context__ is None |
| 166 | + |
| 167 | + |
| 168 | +async def test_success_does_not_log_registration_payload(registration_env, respx_mock, caplog): |
| 169 | + registration_env.AGENT_API_KEY = "configured-agent-key" |
| 170 | + registration_env.AGENT_DESCRIPTION = "private-description" |
| 171 | + respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 172 | + return_value=_success(agent_api_key="returned-agent-key") |
| 173 | + ) |
| 174 | + |
| 175 | + await register_agent(registration_env, agent_card={"name": "private-card"}) |
| 176 | + |
| 177 | + assert "Successfully registered agent" in caplog.text |
| 178 | + for value in ("configured-agent-key", "returned-agent-key", "private-description", "private-card"): |
| 179 | + assert value not in caplog.text |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.parametrize("returned_key", [123, {"value": "not-a-string"}]) |
| 183 | +async def test_invalid_returned_key_does_not_overwrite_configuration( |
| 184 | + registration_env, respx_mock, retry_sleep, returned_key |
| 185 | +): |
| 186 | + registration_env.AGENT_API_KEY = "configured-agent-key" |
| 187 | + os.environ["AGENT_API_KEY"] = "configured-agent-key" |
| 188 | + respx_mock.post("https://agentex.example.test/agents/register").mock( |
| 189 | + return_value=_success(agent_api_key=returned_key) |
| 190 | + ) |
| 191 | + |
| 192 | + with pytest.raises(RuntimeError, match="ValueError"): |
| 193 | + await register_agent(registration_env) |
| 194 | + |
| 195 | + assert registration_env.AGENT_API_KEY == os.environ["AGENT_API_KEY"] == "configured-agent-key" |
| 196 | + assert registration_env.AGENT_ID is None |
| 197 | + assert "AGENT_ID" not in os.environ |
| 198 | + |
| 199 | + |
| 200 | +async def test_missing_base_url_skips_registration(registration_env, respx_mock): |
| 201 | + registration_env.AGENTEX_BASE_URL = None |
| 202 | + |
| 203 | + await register_agent(registration_env) |
| 204 | + |
| 205 | + assert not respx_mock.calls |
0 commit comments