Skip to content

Commit 53d5829

Browse files
committed
Add Unit Tests
1 parent 4df2727 commit 53d5829

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from unittest import mock
2+
3+
import pytest
4+
from starlette.testclient import TestClient
5+
6+
from a2a.server.apps import A2AFastAPIApplication, A2AStarletteApplication
7+
from a2a.types import (
8+
APIKeySecurityScheme,
9+
AgentCapabilities,
10+
AgentCard,
11+
In,
12+
SecurityScheme,
13+
)
14+
from pydantic import ValidationError
15+
16+
17+
@pytest.fixture
18+
def agent_card_with_api_key():
19+
"""Provides an AgentCard with an APIKeySecurityScheme for testing serialization."""
20+
# This data uses the alias 'in', which is correct for creating the model.
21+
api_key_scheme_data = {
22+
'type': 'apiKey',
23+
'name': 'X-API-KEY',
24+
'in': 'header',
25+
}
26+
api_key_scheme = APIKeySecurityScheme.model_validate(api_key_scheme_data)
27+
28+
agent_card = AgentCard(
29+
name='APIKeyAgent',
30+
description='An agent that uses API Key auth.',
31+
url='http://example.com/apikey-agent',
32+
version='1.0.0',
33+
capabilities=AgentCapabilities(),
34+
defaultInputModes=['text/plain'],
35+
defaultOutputModes=['text/plain'],
36+
skills=[],
37+
securitySchemes={'api_key_auth': SecurityScheme(root=api_key_scheme)},
38+
security=[{'api_key_auth': []}],
39+
)
40+
return agent_card
41+
42+
43+
def test_starlette_agent_card_with_api_key_scheme_alias(
44+
agent_card_with_api_key: AgentCard,
45+
):
46+
"""
47+
Tests that the A2AStarletteApplication endpoint correctly serializes aliased fields.
48+
49+
This verifies the fix for `APIKeySecurityScheme.in_` being serialized as `in_` instead of `in`.
50+
"""
51+
handler = mock.AsyncMock()
52+
app_instance = A2AStarletteApplication(agent_card_with_api_key, handler)
53+
client = TestClient(app_instance.build())
54+
55+
response = client.get('/.well-known/agent.json')
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+
try:
65+
parsed_card = AgentCard.model_validate(response_data)
66+
parsed_scheme_wrapper = parsed_card.securitySchemes['api_key_auth']
67+
assert isinstance(parsed_scheme_wrapper.root, APIKeySecurityScheme)
68+
assert parsed_scheme_wrapper.root.in_ == In.header
69+
except ValidationError as e:
70+
pytest.fail(
71+
f"AgentCard.model_validate failed on the server's response: {e}"
72+
)
73+
74+
75+
def test_fastapi_agent_card_with_api_key_scheme_alias(
76+
agent_card_with_api_key: AgentCard,
77+
):
78+
"""
79+
Tests that the A2AFastAPIApplication endpoint correctly serializes aliased fields.
80+
81+
This verifies the fix for `APIKeySecurityScheme.in_` being serialized as `in_` instead of `in`.
82+
"""
83+
handler = mock.AsyncMock()
84+
app_instance = A2AFastAPIApplication(agent_card_with_api_key, handler)
85+
client = TestClient(app_instance.build())
86+
87+
response = client.get('/.well-known/agent.json')
88+
assert response.status_code == 200
89+
response_data = response.json()
90+
91+
security_scheme_json = response_data['securitySchemes']['api_key_auth']
92+
assert 'in' in security_scheme_json
93+
assert 'in_' not in security_scheme_json
94+
assert security_scheme_json['in'] == 'header'

0 commit comments

Comments
 (0)