Repository navigation
Expand file tree
/
Copy pathtest_auth_preprocessor.py
More file actions
541 lines (461 loc) · 17 KB
/
Copy pathtest_auth_preprocessor.py
File metadata and controls
541 lines (461 loc) · 17 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
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for auth_preprocessor module."""
from __future__ import annotations
from unittest.mock import AsyncMock
from unittest.mock import Mock
from unittest.mock import patch
from google.adk.agents.invocation_context import InvocationContext
from google.adk.auth.auth_handler import AuthHandler
from google.adk.auth.auth_preprocessor import _AuthLlmRequestProcessor
from google.adk.auth.auth_tool import AuthConfig
from google.adk.auth.auth_tool import AuthToolArguments
from google.adk.events.event import Event
from google.adk.flows.llm_flows.functions import REQUEST_EUC_FUNCTION_CALL_NAME
from google.adk.models.llm_request import LlmRequest
import pytest
class TestAuthLlmRequestProcessor:
"""Tests for _AuthLlmRequestProcessor class."""
@pytest.fixture
def processor(self):
"""Create an _AuthLlmRequestProcessor instance."""
return _AuthLlmRequestProcessor()
@pytest.fixture
def mock_llm_agent(self):
"""Create a mock LlmAgent."""
from google.adk.agents.llm_agent import LlmAgent
agent = Mock(spec=LlmAgent)
agent.canonical_tools = AsyncMock(return_value=[])
return agent
@pytest.fixture
def mock_non_llm_agent(self):
"""Create a mock non-LLM agent."""
agent = Mock()
agent.__class__.__name__ = 'BaseAgent'
return agent
@pytest.fixture
def mock_session(self):
"""Create a mock session."""
session = Mock()
session.state = {}
session.events = []
return session
@pytest.fixture
def mock_invocation_context(self, mock_llm_agent, mock_session):
"""Create a mock invocation context."""
context = Mock(spec=InvocationContext)
context.agent = mock_llm_agent
context.session = mock_session
return context
@pytest.fixture
def mock_llm_request(self):
"""Create a mock LlmRequest."""
return Mock(spec=LlmRequest)
@pytest.fixture
def mock_auth_config(self):
"""Create a mock AuthConfig."""
return Mock(spec=AuthConfig)
@pytest.fixture
def mock_function_response_with_auth(self, mock_auth_config):
"""Create a mock function response with auth data."""
function_response = Mock()
function_response.name = REQUEST_EUC_FUNCTION_CALL_NAME
function_response.id = 'auth_response_id'
function_response.response = mock_auth_config
return function_response
@pytest.fixture
def mock_function_response_without_auth(self):
"""Create a mock function response without auth data."""
function_response = Mock()
function_response.name = 'some_other_function'
function_response.id = 'other_response_id'
return function_response
@pytest.fixture
def mock_user_event_with_auth_response(
self, mock_function_response_with_auth
):
"""Create a mock user event with auth response."""
event = Mock(spec=Event)
event.author = 'user'
event.content = Mock() # Non-None content
event.get_function_responses.return_value = [
mock_function_response_with_auth
]
return event
@pytest.fixture
def mock_user_event_without_auth_response(
self, mock_function_response_without_auth
):
"""Create a mock user event without auth response."""
event = Mock(spec=Event)
event.author = 'user'
event.content = Mock() # Non-None content
event.get_function_responses.return_value = [
mock_function_response_without_auth
]
return event
@pytest.fixture
def mock_user_event_no_responses(self):
"""Create a mock user event with no responses."""
event = Mock(spec=Event)
event.author = 'user'
event.content = Mock() # Non-None content
event.get_function_responses.return_value = []
return event
@pytest.fixture
def mock_agent_event(self):
"""Create a mock agent-authored event."""
event = Mock(spec=Event)
event.author = 'test_agent'
event.content = Mock() # Non-None content
return event
@pytest.fixture
def mock_event_no_content(self):
"""Create a mock event with no content."""
event = Mock(spec=Event)
event.author = 'user'
event.content = None
return event
@pytest.fixture
def mock_agent_event_with_content(self):
"""Create a mock agent event with content."""
event = Mock(spec=Event)
event.author = 'test_agent'
event.content = Mock() # Non-None content
return event
@pytest.mark.asyncio
async def test_non_llm_agent_returns_early(
self, processor, mock_llm_request, mock_session
):
"""Test that non-LLM agents return early."""
mock_context = Mock(spec=InvocationContext)
mock_context.agent = Mock()
mock_context.agent.__class__.__name__ = 'BaseAgent'
mock_context.session = mock_session
result = []
async for event in processor.run_async(mock_context, mock_llm_request):
result.append(event)
assert result == []
@pytest.mark.asyncio
async def test_empty_events_returns_early(
self, processor, mock_invocation_context, mock_llm_request
):
"""Test that empty events list returns early."""
mock_invocation_context.session.events = []
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
assert result == []
@pytest.mark.asyncio
async def test_no_events_with_content_returns_early(
self,
processor,
mock_invocation_context,
mock_llm_request,
mock_event_no_content,
):
"""Test that no events with content returns early."""
mock_invocation_context.session.events = [mock_event_no_content]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
assert result == []
@pytest.mark.asyncio
async def test_last_event_with_content_not_user_authored_returns_early(
self,
processor,
mock_invocation_context,
mock_llm_request,
mock_event_no_content,
mock_agent_event_with_content,
):
"""Test that last event with content not user-authored returns early."""
# Mix of events: user event with no content, then agent event with content
mock_invocation_context.session.events = [
mock_event_no_content,
mock_agent_event_with_content,
]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
assert result == []
@pytest.mark.asyncio
async def test_last_event_no_responses_returns_early(
self,
processor,
mock_invocation_context,
mock_llm_request,
mock_user_event_no_responses,
):
"""Test that user event with no responses returns early."""
mock_invocation_context.session.events = [mock_user_event_no_responses]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
assert result == []
@pytest.mark.asyncio
async def test_last_event_no_auth_responses_returns_early(
self,
processor,
mock_invocation_context,
mock_llm_request,
mock_user_event_without_auth_response,
):
"""Test that user event with non-auth responses returns early."""
mock_invocation_context.session.events = [
mock_user_event_without_auth_response
]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
assert result == []
@pytest.mark.asyncio
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
async def test_processes_auth_response_successfully(
self,
mock_auth_config_validate,
mock_auth_handler_class,
processor,
mock_invocation_context,
mock_llm_request,
mock_user_event_with_auth_response,
mock_auth_config,
):
"""Test successful processing of auth response in last event."""
# Setup mocks
mock_auth_config_validate.return_value = mock_auth_config
mock_auth_handler = Mock(spec=AuthHandler)
mock_auth_handler.parse_and_store_auth_response = AsyncMock()
mock_auth_handler_class.return_value = mock_auth_handler
mock_invocation_context.session.events = [
mock_user_event_with_auth_response
]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
# Verify auth config validation was called
mock_auth_config_validate.assert_called_once()
# Verify auth handler was created with the config
mock_auth_handler_class.assert_called_once_with(
auth_config=mock_auth_config
)
# Verify parse_and_store_auth_response was called
mock_auth_handler.parse_and_store_auth_response.assert_called_once_with(
state=mock_invocation_context.session.state
)
@pytest.mark.asyncio
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
@patch('google.adk.flows.llm_flows.functions.handle_function_calls_async')
async def test_processes_multiple_auth_responses_and_resumes_tools(
self,
mock_handle_function_calls,
mock_auth_config_validate,
mock_auth_handler_class,
processor,
mock_invocation_context,
mock_llm_request,
mock_auth_config,
):
"""Test processing multiple auth responses and resuming tools."""
# Create multiple auth responses
auth_response_1 = Mock()
auth_response_1.name = REQUEST_EUC_FUNCTION_CALL_NAME
auth_response_1.id = 'auth_id_1'
auth_response_1.response = mock_auth_config
auth_response_2 = Mock()
auth_response_2.name = REQUEST_EUC_FUNCTION_CALL_NAME
auth_response_2.id = 'auth_id_2'
auth_response_2.response = mock_auth_config
user_event_with_multiple_responses = Mock(spec=Event)
user_event_with_multiple_responses.author = 'user'
user_event_with_multiple_responses.content = Mock() # Non-None content
user_event_with_multiple_responses.get_function_responses.return_value = [
auth_response_1,
auth_response_2,
]
# Create system function call events
system_function_call_1 = Mock()
system_function_call_1.id = 'auth_id_1'
system_function_call_1.args = {
'function_call_id': 'tool_id_1',
'auth_config': mock_auth_config,
}
system_function_call_2 = Mock()
system_function_call_2.id = 'auth_id_2'
system_function_call_2.args = {
'function_call_id': 'tool_id_2',
'auth_config': mock_auth_config,
}
system_event = Mock(spec=Event)
system_event.content = Mock() # Non-None content
system_event.get_function_calls.return_value = [
system_function_call_1,
system_function_call_2,
]
# Create original function call event
original_function_call_1 = Mock()
original_function_call_1.id = 'tool_id_1'
original_function_call_2 = Mock()
original_function_call_2.id = 'tool_id_2'
original_event = Mock(spec=Event)
original_event.content = Mock() # Non-None content
original_event.get_function_calls.return_value = [
original_function_call_1,
original_function_call_2,
]
# Setup events in order: original -> system -> user_with_responses
mock_invocation_context.session.events = [
original_event,
system_event,
user_event_with_multiple_responses,
]
# Setup mocks
mock_auth_config_validate.return_value = mock_auth_config
mock_auth_handler = Mock(spec=AuthHandler)
mock_auth_handler.parse_and_store_auth_response = AsyncMock()
mock_auth_handler_class.return_value = mock_auth_handler
mock_function_response_event = Mock(spec=Event)
mock_handle_function_calls.return_value = mock_function_response_event
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
# Verify auth responses were processed
assert mock_auth_handler.parse_and_store_auth_response.call_count == 2
# Verify function calls were resumed
mock_handle_function_calls.assert_called_once()
call_args = mock_handle_function_calls.call_args
assert call_args[0][1] == original_event # The original event
assert call_args[0][3] == {'tool_id_1', 'tool_id_2'} # Tools to resume
# Verify the function response event was yielded
assert result == [mock_function_response_event]
@pytest.mark.asyncio
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
async def test_no_matching_system_function_calls_returns_early(
self,
mock_auth_config_validate,
mock_auth_handler_class,
processor,
mock_invocation_context,
mock_llm_request,
mock_user_event_with_auth_response,
mock_auth_config,
):
"""Test that missing matching system function calls returns early."""
# Setup mocks
mock_auth_config_validate.return_value = mock_auth_config
mock_auth_handler = Mock(spec=AuthHandler)
mock_auth_handler.parse_and_store_auth_response = AsyncMock()
mock_auth_handler_class.return_value = mock_auth_handler
# Create a non-matching system event
non_matching_function_call = Mock()
non_matching_function_call.id = ( # Different from 'auth_response_id'
'different_id'
)
system_event = Mock(spec=Event)
system_event.content = Mock() # Non-None content
system_event.get_function_calls.return_value = [non_matching_function_call]
mock_invocation_context.session.events = [
system_event,
mock_user_event_with_auth_response,
]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
# Should process auth response but not resume any tools
mock_auth_handler.parse_and_store_auth_response.assert_called_once()
assert result == []
@pytest.mark.asyncio
@patch('google.adk.auth.auth_preprocessor.AuthHandler')
@patch('google.adk.auth.auth_tool.AuthConfig.model_validate')
@patch('google.adk.auth.auth_tool.AuthToolArguments.model_validate')
async def test_handles_missing_original_function_calls(
self,
mock_auth_tool_args_validate,
mock_auth_config_validate,
mock_auth_handler_class,
processor,
mock_invocation_context,
mock_llm_request,
mock_user_event_with_auth_response,
mock_auth_config,
):
"""Test handling when original function calls are not found."""
# Setup mocks
mock_auth_config_validate.return_value = mock_auth_config
mock_auth_handler = Mock(spec=AuthHandler)
mock_auth_handler.parse_and_store_auth_response = AsyncMock()
mock_auth_handler_class.return_value = mock_auth_handler
# Create matching system function call
auth_tool_args = Mock(spec=AuthToolArguments)
auth_tool_args.function_call_id = 'tool_id_1'
mock_auth_tool_args_validate.return_value = auth_tool_args
system_function_call = Mock()
system_function_call.id = 'auth_response_id' # Matches the response ID
system_function_call.args = {
'function_call_id': 'tool_id_1',
'auth_config': mock_auth_config,
}
system_event = Mock(spec=Event)
system_event.content = Mock() # Non-None content
system_event.get_function_calls.return_value = [system_function_call]
# Create event with no function calls (original function calls missing)
empty_event = Mock(spec=Event)
empty_event.content = Mock() # Non-None content
empty_event.get_function_calls.return_value = []
mock_invocation_context.session.events = [
empty_event,
system_event,
mock_user_event_with_auth_response,
]
result = []
async for event in processor.run_async(
mock_invocation_context, mock_llm_request
):
result.append(event)
# Should process auth response but not find original function calls
mock_auth_handler.parse_and_store_auth_response.assert_called_once()
assert result == []
@pytest.mark.asyncio
async def test_isinstance_check_for_llm_agent(
self, processor, mock_llm_request, mock_session
):
"""Test that isinstance check works correctly for LlmAgent."""
# This test ensures the isinstance check work as expected
# Create a mock that fails isinstance check
mock_context = Mock(spec=InvocationContext)
mock_context.agent = Mock() # This will fail isinstance(agent, LlmAgent)
mock_context.session = mock_session
result = []
async for event in processor.run_async(mock_context, mock_llm_request):
result.append(event)
assert result == []