-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathtest_grpc_handler.py
More file actions
790 lines (684 loc) · 25.5 KB
/
test_grpc_handler.py
File metadata and controls
790 lines (684 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import grpc
import grpc.aio
import pytest
from google.rpc import error_details_pb2, status_pb2
from a2a import types
from a2a.extensions.common import HTTP_EXTENSION_HEADER
from a2a.server.context import ServerCallContext
from a2a.server.request_handlers import GrpcHandler, RequestHandler
from a2a.types import a2a_pb2
# --- Fixtures ---
@pytest.fixture
def mock_request_handler() -> AsyncMock:
return AsyncMock(spec=RequestHandler)
@pytest.fixture
def mock_grpc_context() -> AsyncMock:
context = AsyncMock(spec=grpc.aio.ServicerContext)
context.abort = AsyncMock()
context.set_trailing_metadata = MagicMock()
return context
@pytest.fixture
def sample_agent_card() -> types.AgentCard:
return types.AgentCard(
name='Test Agent',
description='A test agent',
supported_interfaces=[
types.AgentInterface(
protocol_binding='GRPC', url='http://localhost'
)
],
version='1.0.0',
capabilities=types.AgentCapabilities(
streaming=True, push_notifications=True
),
default_input_modes=['text/plain'],
default_output_modes=['text/plain'],
skills=[],
)
@pytest.fixture
def grpc_handler(
mock_request_handler: AsyncMock, sample_agent_card: types.AgentCard
) -> GrpcHandler:
return GrpcHandler(
agent_card=sample_agent_card, request_handler=mock_request_handler
)
# --- Test Cases ---
@pytest.mark.asyncio
async def test_send_message_success(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful SendMessage call."""
request_proto = a2a_pb2.SendMessageRequest(
message=a2a_pb2.Message(message_id='msg-1')
)
response_model = types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.TASK_STATE_COMPLETED),
)
mock_request_handler.on_message_send.return_value = response_model
response = await grpc_handler.SendMessage(request_proto, mock_grpc_context)
mock_request_handler.on_message_send.assert_awaited_once()
assert isinstance(response, a2a_pb2.SendMessageResponse)
assert response.HasField('task')
assert response.task.id == 'task-1'
@pytest.mark.asyncio
async def test_send_message_server_error(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test SendMessage call when handler raises an A2AError."""
request_proto = a2a_pb2.SendMessageRequest()
error = types.InvalidParamsError(message='Bad params')
mock_request_handler.on_message_send.side_effect = error
await grpc_handler.SendMessage(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.INVALID_ARGUMENT, 'Bad params'
)
@pytest.mark.asyncio
async def test_get_task_success(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful GetTask call."""
request_proto = a2a_pb2.GetTaskRequest(id='task-1')
response_model = types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.TASK_STATE_WORKING),
)
mock_request_handler.on_get_task.return_value = response_model
response = await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_request_handler.on_get_task.assert_awaited_once()
assert isinstance(response, a2a_pb2.Task)
assert response.id == 'task-1'
@pytest.mark.asyncio
async def test_get_task_not_found(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetTask call when task is not found."""
request_proto = a2a_pb2.GetTaskRequest(id='task-1')
mock_request_handler.on_get_task.return_value = None
await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.NOT_FOUND, 'Task not found'
)
@pytest.mark.asyncio
async def test_cancel_task_server_error(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test CancelTask call when handler raises A2AError."""
request_proto = a2a_pb2.CancelTaskRequest(id='task-1')
error = types.TaskNotCancelableError()
mock_request_handler.on_cancel_task.side_effect = error
await grpc_handler.CancelTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once_with(
grpc.StatusCode.UNIMPLEMENTED,
'Task cannot be canceled',
)
@pytest.mark.asyncio
async def test_send_streaming_message(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
"""Test successful SendStreamingMessage call."""
async def mock_stream():
yield types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(state=types.TaskState.TASK_STATE_WORKING),
)
# Use MagicMock because on_message_send_stream is an async generator,
# and we iterate over it directly. AsyncMock would return a coroutine.
mock_request_handler.on_message_send_stream = MagicMock(
return_value=mock_stream()
)
request_proto = a2a_pb2.SendMessageRequest()
results = [
result
async for result in grpc_handler.SendStreamingMessage(
request_proto, mock_grpc_context
)
]
assert len(results) == 1
assert results[0].HasField('task')
assert results[0].task.id == 'task-1'
@pytest.mark.asyncio
async def test_get_extended_agent_card(
grpc_handler: GrpcHandler,
sample_agent_card: types.AgentCard,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetExtendedAgentCard call."""
request_proto = a2a_pb2.GetExtendedAgentCardRequest()
response = await grpc_handler.GetExtendedAgentCard(
request_proto, mock_grpc_context
)
assert response.name == sample_agent_card.name
assert response.version == sample_agent_card.version
@pytest.mark.asyncio
async def test_get_extended_agent_card_with_modifier(
mock_request_handler: AsyncMock,
sample_agent_card: types.AgentCard,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetExtendedAgentCard call with a card_modifier."""
async def modifier(card: types.AgentCard) -> types.AgentCard:
modified_card = types.AgentCard()
modified_card.CopyFrom(card)
modified_card.name = 'Modified gRPC Agent'
return modified_card
grpc_handler_modified = GrpcHandler(
agent_card=sample_agent_card,
request_handler=mock_request_handler,
card_modifier=modifier,
)
request_proto = a2a_pb2.GetExtendedAgentCardRequest()
response = await grpc_handler_modified.GetExtendedAgentCard(
request_proto, mock_grpc_context
)
assert response.name == 'Modified gRPC Agent'
assert response.version == sample_agent_card.version
@pytest.mark.asyncio
async def test_get_agent_card_with_modifier_sync(
mock_request_handler: AsyncMock,
sample_agent_card: types.AgentCard,
mock_grpc_context: AsyncMock,
) -> None:
"""Test GetAgentCard call with a synchronous card_modifier."""
def modifier(card: types.AgentCard) -> types.AgentCard:
# For proto, we need to create a new message with modified fields
modified_card = types.AgentCard()
modified_card.CopyFrom(card)
modified_card.name = 'Modified gRPC Agent'
return modified_card
grpc_handler_modified = GrpcHandler(
agent_card=sample_agent_card,
request_handler=mock_request_handler,
card_modifier=modifier,
)
request_proto = a2a_pb2.GetExtendedAgentCardRequest()
response = await grpc_handler_modified.GetExtendedAgentCard(
request_proto, mock_grpc_context
)
assert response.name == 'Modified gRPC Agent'
assert response.version == sample_agent_card.version
@pytest.mark.asyncio
async def test_list_tasks_success(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
):
"""Test successful ListTasks call."""
mock_request_handler.on_list_tasks.return_value = a2a_pb2.ListTasksResponse(
next_page_token='123',
tasks=[
types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(
state=types.TaskState.TASK_STATE_COMPLETED
),
),
types.Task(
id='task-2',
context_id='ctx-1',
status=types.TaskStatus(
state=types.TaskState.TASK_STATE_WORKING
),
),
],
)
response = await grpc_handler.ListTasks(
a2a_pb2.ListTasksRequest(page_size=2), mock_grpc_context
)
mock_request_handler.on_list_tasks.assert_awaited_once()
assert isinstance(response, a2a_pb2.ListTasksResponse)
assert len(response.tasks) == 2
assert response.tasks[0].id == 'task-1'
assert response.tasks[1].id == 'task-2'
@pytest.mark.asyncio
@pytest.mark.parametrize(
'a2a_error, grpc_status_code, error_message_part',
[
(
types.InvalidRequestError(),
grpc.StatusCode.INVALID_ARGUMENT,
'InvalidRequestError',
),
(
types.MethodNotFoundError(),
grpc.StatusCode.NOT_FOUND,
'MethodNotFoundError',
),
(
types.InvalidParamsError(),
grpc.StatusCode.INVALID_ARGUMENT,
'InvalidParamsError',
),
(
types.InternalError(),
grpc.StatusCode.INTERNAL,
'InternalError',
),
(
types.TaskNotFoundError(),
grpc.StatusCode.NOT_FOUND,
'TaskNotFoundError',
),
(
types.TaskNotCancelableError(),
grpc.StatusCode.UNIMPLEMENTED,
'TaskNotCancelableError',
),
(
types.PushNotificationNotSupportedError(),
grpc.StatusCode.UNIMPLEMENTED,
'PushNotificationNotSupportedError',
),
(
types.UnsupportedOperationError(),
grpc.StatusCode.UNIMPLEMENTED,
'UnsupportedOperationError',
),
(
types.ContentTypeNotSupportedError(),
grpc.StatusCode.UNIMPLEMENTED,
'ContentTypeNotSupportedError',
),
(
types.InvalidAgentResponseError(),
grpc.StatusCode.INTERNAL,
'InvalidAgentResponseError',
),
],
)
async def test_abort_context_error_mapping( # noqa: PLR0913
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
a2a_error: Exception,
grpc_status_code: grpc.StatusCode,
error_message_part: str,
) -> None:
mock_request_handler.on_get_task.side_effect = a2a_error
request_proto = a2a_pb2.GetTaskRequest(id='any')
await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_grpc_context.abort.assert_awaited_once()
call_args, _ = mock_grpc_context.abort.call_args
assert call_args[0] == grpc_status_code
# We shouldn't rely on the legacy ExceptionName: message string format
# But for backward compatability fallback it shouldn't fail
mock_grpc_context.set_trailing_metadata.assert_called_once()
metadata = mock_grpc_context.set_trailing_metadata.call_args[0][0]
assert any(key == 'grpc-status-details-bin' for key, _ in metadata)
@pytest.mark.asyncio
async def test_abort_context_rich_error_format(
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
error = types.TaskNotFoundError('Could not find the task')
mock_request_handler.on_get_task.side_effect = error
request_proto = a2a_pb2.GetTaskRequest(id='any')
await grpc_handler.GetTask(request_proto, mock_grpc_context)
mock_grpc_context.set_trailing_metadata.assert_called_once()
metadata = mock_grpc_context.set_trailing_metadata.call_args[0][0]
bin_values = [v for k, v in metadata if k == 'grpc-status-details-bin']
assert len(bin_values) == 1
status = status_pb2.Status.FromString(bin_values[0])
assert status.code == grpc.StatusCode.NOT_FOUND.value[0]
assert status.message == 'Could not find the task'
assert len(status.details) == 1
error_info = error_details_pb2.ErrorInfo()
status.details[0].Unpack(error_info)
assert error_info.reason == 'TASK_NOT_FOUND'
assert error_info.domain == 'a2a-protocol.org'
@pytest.mark.asyncio
class TestGrpcExtensions:
async def test_send_message_with_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER.lower(), 'foo'),
(HTTP_EXTENSION_HEADER.lower(), 'bar'),
)
def side_effect(request, context: ServerCallContext):
context.activated_extensions.add('foo')
context.activated_extensions.add('baz')
return types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(
state=types.TaskState.TASK_STATE_COMPLETED
),
)
mock_request_handler.on_message_send.side_effect = side_effect
await grpc_handler.SendMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
mock_request_handler.on_message_send.assert_awaited_once()
call_context = mock_request_handler.on_message_send.call_args[0][1]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar'}
mock_grpc_context.set_trailing_metadata.assert_called_once()
called_metadata = (
mock_grpc_context.set_trailing_metadata.call_args.args[0]
)
assert set(called_metadata) == {
(HTTP_EXTENSION_HEADER.lower(), 'foo'),
(HTTP_EXTENSION_HEADER.lower(), 'baz'),
}
async def test_send_message_with_comma_separated_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER.lower(), 'foo ,, bar,'),
(HTTP_EXTENSION_HEADER.lower(), 'baz , bar'),
)
mock_request_handler.on_message_send.return_value = types.Message(
message_id='1',
role=types.Role.ROLE_AGENT,
parts=[types.Part(text='test')],
)
await grpc_handler.SendMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
mock_request_handler.on_message_send.assert_awaited_once()
call_context = mock_request_handler.on_message_send.call_args[0][1]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar', 'baz'}
async def test_send_streaming_message_with_extensions(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
) -> None:
mock_grpc_context.invocation_metadata.return_value = grpc.aio.Metadata(
(HTTP_EXTENSION_HEADER.lower(), 'foo'),
(HTTP_EXTENSION_HEADER.lower(), 'bar'),
)
async def side_effect(request, context: ServerCallContext):
context.activated_extensions.add('foo')
context.activated_extensions.add('baz')
yield types.Task(
id='task-1',
context_id='ctx-1',
status=types.TaskStatus(
state=types.TaskState.TASK_STATE_WORKING
),
)
mock_request_handler.on_message_send_stream.side_effect = side_effect
results = [
result
async for result in grpc_handler.SendStreamingMessage(
a2a_pb2.SendMessageRequest(), mock_grpc_context
)
]
assert results
mock_request_handler.on_message_send_stream.assert_called_once()
call_context = mock_request_handler.on_message_send_stream.call_args[0][
1
]
assert isinstance(call_context, ServerCallContext)
assert call_context.requested_extensions == {'foo', 'bar'}
mock_grpc_context.set_trailing_metadata.assert_called_once()
called_metadata = (
mock_grpc_context.set_trailing_metadata.call_args.args[0]
)
assert set(called_metadata) == {
(HTTP_EXTENSION_HEADER.lower(), 'foo'),
(HTTP_EXTENSION_HEADER.lower(), 'baz'),
}
@pytest.mark.asyncio
class TestTenantExtraction:
@pytest.mark.parametrize(
'method_name, request_proto, handler_method_name, return_value',
[
(
'SendMessage',
a2a_pb2.SendMessageRequest(tenant='my-tenant'),
'on_message_send',
types.Message(),
),
(
'CancelTask',
a2a_pb2.CancelTaskRequest(tenant='my-tenant', id='1'),
'on_cancel_task',
types.Task(id='1'),
),
(
'GetTask',
a2a_pb2.GetTaskRequest(tenant='my-tenant', id='1'),
'on_get_task',
types.Task(id='1'),
),
(
'ListTasks',
a2a_pb2.ListTasksRequest(tenant='my-tenant'),
'on_list_tasks',
a2a_pb2.ListTasksResponse(),
),
(
'GetTaskPushNotificationConfig',
a2a_pb2.GetTaskPushNotificationConfigRequest(
tenant='my-tenant', task_id='1', id='c1'
),
'on_get_task_push_notification_config',
a2a_pb2.TaskPushNotificationConfig(),
),
(
'CreateTaskPushNotificationConfig',
a2a_pb2.CreateTaskPushNotificationConfigRequest(
tenant='my-tenant',
task_id='1',
config=a2a_pb2.PushNotificationConfig(),
),
'on_create_task_push_notification_config',
a2a_pb2.TaskPushNotificationConfig(),
),
(
'ListTaskPushNotificationConfigs',
a2a_pb2.ListTaskPushNotificationConfigsRequest(
tenant='my-tenant', task_id='1'
),
'on_list_task_push_notification_configs',
a2a_pb2.ListTaskPushNotificationConfigsResponse(),
),
(
'DeleteTaskPushNotificationConfig',
a2a_pb2.DeleteTaskPushNotificationConfigRequest(
tenant='my-tenant', task_id='1', id='c1'
),
'on_delete_task_push_notification_config',
None,
),
],
)
async def test_non_streaming_tenant_extraction(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
method_name: str,
request_proto: Any,
handler_method_name: str,
return_value: Any,
) -> None:
handler_mock = getattr(mock_request_handler, handler_method_name)
handler_mock.return_value = return_value
grpc_method = getattr(grpc_handler, method_name)
await grpc_method(request_proto, mock_grpc_context)
handler_mock.assert_awaited_once()
call_args = handler_mock.call_args
server_context = call_args[0][1]
assert isinstance(server_context, ServerCallContext)
assert server_context.tenant == 'my-tenant'
@pytest.mark.parametrize(
'method_name, request_proto, handler_method_name',
[
(
'SendStreamingMessage',
a2a_pb2.SendMessageRequest(tenant='my-tenant'),
'on_message_send_stream',
),
(
'SubscribeToTask',
a2a_pb2.SubscribeToTaskRequest(tenant='my-tenant', id='1'),
'on_subscribe_to_task',
),
],
)
async def test_streaming_tenant_extraction(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
method_name: str,
request_proto: Any,
handler_method_name: str,
) -> None:
async def mock_stream(*args, **kwargs):
yield types.Message(message_id='msg-1')
handler_mock_attr = MagicMock(return_value=mock_stream())
setattr(mock_request_handler, handler_method_name, handler_mock_attr)
grpc_method = getattr(grpc_handler, method_name)
async for _ in grpc_method(request_proto, mock_grpc_context):
pass
handler_mock_attr.assert_called_once()
call_args = handler_mock_attr.call_args
server_context = call_args[0][1]
assert isinstance(server_context, ServerCallContext)
assert server_context.tenant == 'my-tenant'
@pytest.mark.parametrize(
'method_name, request_proto, handler_method_name, return_value',
[
(
'SendMessage',
a2a_pb2.SendMessageRequest(),
'on_message_send',
types.Message(),
),
(
'CancelTask',
a2a_pb2.CancelTaskRequest(id='1'),
'on_cancel_task',
types.Task(id='1'),
),
(
'GetTask',
a2a_pb2.GetTaskRequest(id='1'),
'on_get_task',
types.Task(id='1'),
),
(
'ListTasks',
a2a_pb2.ListTasksRequest(),
'on_list_tasks',
a2a_pb2.ListTasksResponse(),
),
(
'GetTaskPushNotificationConfig',
a2a_pb2.GetTaskPushNotificationConfigRequest(
task_id='1', id='c1'
),
'on_get_task_push_notification_config',
a2a_pb2.TaskPushNotificationConfig(),
),
(
'CreateTaskPushNotificationConfig',
a2a_pb2.CreateTaskPushNotificationConfigRequest(
task_id='1',
config=a2a_pb2.PushNotificationConfig(),
),
'on_create_task_push_notification_config',
a2a_pb2.TaskPushNotificationConfig(),
),
(
'ListTaskPushNotificationConfigs',
a2a_pb2.ListTaskPushNotificationConfigsRequest(task_id='1'),
'on_list_task_push_notification_configs',
a2a_pb2.ListTaskPushNotificationConfigsResponse(),
),
(
'DeleteTaskPushNotificationConfig',
a2a_pb2.DeleteTaskPushNotificationConfigRequest(
task_id='1', id='c1'
),
'on_delete_task_push_notification_config',
None,
),
],
)
async def test_non_streaming_no_tenant_extraction(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
method_name: str,
request_proto: Any,
handler_method_name: str,
return_value: Any,
) -> None:
handler_mock = getattr(mock_request_handler, handler_method_name)
handler_mock.return_value = return_value
grpc_method = getattr(grpc_handler, method_name)
await grpc_method(request_proto, mock_grpc_context)
handler_mock.assert_awaited_once()
call_args = handler_mock.call_args
server_context = call_args[0][1]
assert isinstance(server_context, ServerCallContext)
assert server_context.tenant == ''
@pytest.mark.parametrize(
'method_name, request_proto, handler_method_name',
[
(
'SendStreamingMessage',
a2a_pb2.SendMessageRequest(),
'on_message_send_stream',
),
(
'SubscribeToTask',
a2a_pb2.SubscribeToTaskRequest(id='1'),
'on_subscribe_to_task',
),
],
)
async def test_streaming_no_tenant_extraction(
self,
grpc_handler: GrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
method_name: str,
request_proto: Any,
handler_method_name: str,
) -> None:
async def mock_stream(*args, **kwargs):
yield types.Message(message_id='msg-1')
handler_mock_attr = MagicMock(return_value=mock_stream())
setattr(mock_request_handler, handler_method_name, handler_mock_attr)
grpc_method = getattr(grpc_handler, method_name)
async for _ in grpc_method(request_proto, mock_grpc_context):
pass
handler_mock_attr.assert_called_once()
call_args = handler_mock_attr.call_args
server_context = call_args[0][1]
assert isinstance(server_context, ServerCallContext)
assert server_context.tenant == ''