-
Notifications
You must be signed in to change notification settings - Fork 429
feat(rest): add tenant support to rest #773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa2863b
feat: add `tenant` to `ServerCallContext`, add tenant-prefixed route…
sokoliva 9ab432b
Merge branch '1.0-dev' into tenant-server
sokoliva 39d5351
refactor: add helper _build_call_context and reduce code duplication …
sokoliva d194b37
Merge branch '1.0-dev' of https://github.com/a2aproject/a2a-python in…
sokoliva b0fdc69
Merge branch 'tenant-server' of https://github.com/sokoliva/a2a-pytho…
sokoliva 2a57166
fix: add tenant support to `handle_authenticated_agent_card` and add …
sokoliva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import pytest | ||
| from unittest.mock import MagicMock | ||
| from fastapi import FastAPI | ||
| from httpx import ASGITransport, AsyncClient | ||
| from google.protobuf import json_format | ||
|
|
||
| from a2a.server.apps.rest.fastapi_app import A2ARESTFastAPIApplication | ||
| from a2a.server.request_handlers.request_handler import RequestHandler | ||
| from a2a.types.a2a_pb2 import ( | ||
| AgentCard, | ||
| Message, | ||
| Role, | ||
| Part, | ||
| SendMessageRequest, | ||
| SendMessageConfiguration, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def agent_card() -> AgentCard: | ||
| mock_agent_card = MagicMock(spec=AgentCard) | ||
| mock_agent_card.url = 'http://mockurl.com' | ||
| mock_capabilities = MagicMock() | ||
| mock_capabilities.streaming = False | ||
| mock_agent_card.capabilities = mock_capabilities | ||
| return mock_agent_card | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def request_handler() -> RequestHandler: | ||
| handler = MagicMock(spec=RequestHandler) | ||
| # Return a default response so the test doesn't crash on return value expectation | ||
| handler.on_message_send.return_value = Message( | ||
| message_id='test', | ||
| role=Role.ROLE_AGENT, | ||
| parts=[Part(text='response message')], | ||
| ) | ||
| return handler | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def app( | ||
| agent_card: AgentCard, request_handler: RequestHandler | ||
| ) -> FastAPI: | ||
| return A2ARESTFastAPIApplication(agent_card, request_handler).build( | ||
| agent_card_url='/well-known/agent.json', rpc_url='' | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def client(app: FastAPI) -> AsyncClient: | ||
| return AsyncClient(transport=ASGITransport(app=app), base_url='http://test') | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_tenant_extraction_from_path( | ||
| client: AsyncClient, request_handler: MagicMock | ||
| ) -> None: | ||
| request = SendMessageRequest( | ||
| message=Message(), | ||
| configuration=SendMessageConfiguration(), | ||
| ) | ||
|
|
||
| # Test with tenant in URL | ||
| tenant_id = 'my-tenant-123' | ||
| response = await client.post( | ||
| f'/{tenant_id}/message:send', json=json_format.MessageToDict(request) | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| # Verify handler was called | ||
| assert request_handler.on_message_send.called | ||
|
|
||
| # Verify call context has tenant | ||
| args, _ = request_handler.on_message_send.call_args | ||
| # args[0] is the request proto, args[1] is the ServerCallContext | ||
| context = args[1] | ||
| assert context.tenant == tenant_id | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_no_tenant_extraction( | ||
| client: AsyncClient, request_handler: MagicMock | ||
| ) -> None: | ||
| request = SendMessageRequest( | ||
| message=Message(), | ||
| configuration=SendMessageConfiguration(), | ||
| ) | ||
|
|
||
| # Test without tenant in URL | ||
| response = await client.post( | ||
| '/message:send', json=json_format.MessageToDict(request) | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| # Verify call context has empty string tenant (default) | ||
| args, _ = request_handler.on_message_send.call_args | ||
| context = args[1] | ||
| assert context.tenant == '' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.