|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from httpx import ASGITransport, AsyncClient |
| 6 | + |
| 7 | +from a2a.server.apps.rest.fastapi_app import A2ARESTFastAPIApplication |
| 8 | +from a2a.types import ( |
| 9 | + APIKeySecurityScheme, |
| 10 | + AgentCapabilities, |
| 11 | + AgentCard, |
| 12 | + In, |
| 13 | + SecurityScheme, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def agent_card_with_api_key() -> AgentCard: |
| 19 | + api_key_scheme_data = { |
| 20 | + 'type': 'apiKey', |
| 21 | + 'name': 'X-API-KEY', |
| 22 | + 'in': 'header', |
| 23 | + } |
| 24 | + api_key_scheme = APIKeySecurityScheme.model_validate(api_key_scheme_data) |
| 25 | + |
| 26 | + return AgentCard( |
| 27 | + name='APIKeyAgent', |
| 28 | + description='An agent that uses API Key auth.', |
| 29 | + url='http://example.com/apikey-agent', |
| 30 | + version='1.0.0', |
| 31 | + capabilities=AgentCapabilities(), |
| 32 | + default_input_modes=['text/plain'], |
| 33 | + default_output_modes=['text/plain'], |
| 34 | + skills=[], |
| 35 | + security_schemes={'api_key_auth': SecurityScheme(root=api_key_scheme)}, |
| 36 | + security=[{'api_key_auth': []}], |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.anyio |
| 41 | +async def test_rest_agent_card_with_api_key_scheme_alias( |
| 42 | + agent_card_with_api_key: AgentCard, |
| 43 | +): |
| 44 | + """Ensures REST agent card serialization uses the 'in' alias.""" |
| 45 | + handler = mock.AsyncMock() |
| 46 | + app_instance = A2ARESTFastAPIApplication(agent_card_with_api_key, handler) |
| 47 | + app = app_instance.build( |
| 48 | + agent_card_url='/.well-known/agent.json', rpc_url='' |
| 49 | + ) |
| 50 | + |
| 51 | + async with AsyncClient( |
| 52 | + transport=ASGITransport(app=app), base_url='http://test' |
| 53 | + ) as client: |
| 54 | + response = await client.get('/.well-known/agent.json') |
| 55 | + |
| 56 | + assert response.status_code == 200 |
| 57 | + response_data = response.json() |
| 58 | + |
| 59 | + security_scheme_json = response_data['securitySchemes']['api_key_auth'] |
| 60 | + assert 'in' in security_scheme_json |
| 61 | + assert security_scheme_json['in'] == 'header' |
| 62 | + assert 'in_' not in security_scheme_json |
| 63 | + |
| 64 | + parsed_card = AgentCard.model_validate(response_data) |
| 65 | + parsed_scheme_wrapper = parsed_card.security_schemes['api_key_auth'] |
| 66 | + assert parsed_scheme_wrapper.root.in_ == In.header |
0 commit comments