|
1 | 1 | """Tests for Bedrock AgentCore authentication decorators and functions."""
|
2 | 2 |
|
| 3 | +import json |
3 | 4 | import os
|
4 |
| -from unittest.mock import AsyncMock, Mock, mock_open, patch |
| 5 | +from unittest.mock import AsyncMock, Mock, patch |
5 | 6 |
|
6 | 7 | import pytest
|
7 | 8 |
|
@@ -332,60 +333,67 @@ class TestSetUpLocalAuth:
|
332 | 333 | """Test _set_up_local_auth function."""
|
333 | 334 |
|
334 | 335 | @pytest.mark.asyncio
|
335 |
| - async def test_existing_config(self): |
| 336 | + async def test_existing_config(self, tmp_path): |
336 | 337 | """Test when config file exists with both workload_identity_name and user_id."""
|
337 | 338 | config_content = {"workload_identity_name": "existing-workload-123", "user_id": "existing-user-456"}
|
338 | 339 | mock_client = Mock()
|
339 | 340 | mock_client.get_workload_access_token = Mock(return_value={"workloadAccessToken": "test-access-token-456"})
|
340 | 341 |
|
341 |
| - with patch("pathlib.Path") as mock_path_class: |
342 |
| - mock_path = Mock() |
343 |
| - mock_path.exists.return_value = True |
344 |
| - mock_path.absolute.return_value = "/test/.agentcore.yaml" |
345 |
| - mock_path_class.return_value = mock_path |
346 |
| - |
347 |
| - with patch("builtins.open", mock_open()): |
348 |
| - with patch("yaml.safe_load", return_value=config_content): |
349 |
| - result = await _set_up_local_auth(mock_client) |
350 |
| - |
351 |
| - # Should use existing workload identity and user_id |
352 |
| - assert result == "test-access-token-456" |
353 |
| - mock_client.create_workload_identity.assert_not_called() |
354 |
| - mock_client.get_workload_access_token.assert_called_once_with( |
355 |
| - "existing-workload-123", user_id="existing-user-456" |
356 |
| - ) |
| 342 | + # Create the config file in the temp directory |
| 343 | + config_file = tmp_path / ".agentcore.json" |
| 344 | + config_file.write_text(json.dumps(config_content)) |
| 345 | + |
| 346 | + # Change to the temp directory for the test |
| 347 | + import os |
| 348 | + |
| 349 | + original_dir = os.getcwd() |
| 350 | + try: |
| 351 | + os.chdir(tmp_path) |
| 352 | + result = await _set_up_local_auth(mock_client) |
| 353 | + |
| 354 | + # Should use existing workload identity and user_id |
| 355 | + assert result == "test-access-token-456" |
| 356 | + mock_client.create_workload_identity.assert_not_called() |
| 357 | + mock_client.get_workload_access_token.assert_called_once_with( |
| 358 | + "existing-workload-123", user_id="existing-user-456" |
| 359 | + ) |
| 360 | + finally: |
| 361 | + os.chdir(original_dir) |
357 | 362 |
|
358 | 363 | @pytest.mark.asyncio
|
359 |
| - async def test_no_config(self): |
| 364 | + async def test_no_config(self, tmp_path): |
360 | 365 | """Test when config file doesn't exist."""
|
361 | 366 | mock_client = Mock()
|
362 | 367 | mock_client.create_workload_identity = Mock(return_value={"name": "test-workload-123"})
|
363 | 368 | mock_client.get_workload_access_token = Mock(return_value={"workloadAccessToken": "test-access-token-456"})
|
364 | 369 |
|
365 |
| - with patch("pathlib.Path") as mock_path_class: |
366 |
| - mock_path = Mock() |
367 |
| - mock_path.exists.return_value = False |
368 |
| - mock_path_class.return_value = mock_path |
369 |
| - |
370 |
| - with patch("builtins.open", mock_open()): |
371 |
| - with patch("yaml.dump") as mock_yaml_dump: |
372 |
| - with patch("uuid.uuid4") as mock_uuid: |
373 |
| - mock_uuid.return_value.hex = "abcd1234efgh5678" |
374 |
| - |
375 |
| - result = await _set_up_local_auth(mock_client) |
376 |
| - |
377 |
| - # Should create new workload identity and user_id |
378 |
| - assert result == "test-access-token-456" |
379 |
| - mock_client.create_workload_identity.assert_called_once() |
380 |
| - mock_client.get_workload_access_token.assert_called_once_with( |
381 |
| - "test-workload-123", user_id="abcd1234" |
382 |
| - ) |
383 |
| - |
384 |
| - # Should create and save new config |
385 |
| - mock_yaml_dump.assert_called_once() |
386 |
| - saved_config = mock_yaml_dump.call_args[0][0] |
387 |
| - assert saved_config["workload_identity_name"] == "test-workload-123" |
388 |
| - assert saved_config["user_id"] == "abcd1234" |
| 370 | + # Change to the temp directory for the test |
| 371 | + import os |
| 372 | + |
| 373 | + original_dir = os.getcwd() |
| 374 | + try: |
| 375 | + os.chdir(tmp_path) |
| 376 | + |
| 377 | + with patch("uuid.uuid4") as mock_uuid: |
| 378 | + mock_uuid.return_value.hex = "abcd1234efgh5678" |
| 379 | + |
| 380 | + result = await _set_up_local_auth(mock_client) |
| 381 | + |
| 382 | + # Should create new workload identity and user_id |
| 383 | + assert result == "test-access-token-456" |
| 384 | + mock_client.create_workload_identity.assert_called_once() |
| 385 | + mock_client.get_workload_access_token.assert_called_once_with("test-workload-123", user_id="abcd1234") |
| 386 | + |
| 387 | + # Verify that the config file was created |
| 388 | + config_file = tmp_path / ".agentcore.json" |
| 389 | + assert config_file.exists() |
| 390 | + |
| 391 | + # Verify the config file content |
| 392 | + saved_config = json.loads(config_file.read_text()) |
| 393 | + assert saved_config["workload_identity_name"] == "test-workload-123" |
| 394 | + assert saved_config["user_id"] == "abcd1234" |
| 395 | + finally: |
| 396 | + os.chdir(original_dir) |
389 | 397 |
|
390 | 398 |
|
391 | 399 | class TestGetRegion:
|
|
0 commit comments