|
6 | 6 | from io import BytesIO |
7 | 7 | from typing import Dict |
8 | 8 |
|
| 9 | +import boto3 |
9 | 10 | import pytest |
10 | 11 | from boto3.dynamodb.conditions import Key |
11 | 12 | from botocore import stub |
@@ -444,6 +445,43 @@ def test_ssm_provider_get(mock_name, mock_value, mock_version, config): |
444 | 445 | stubber.deactivate() |
445 | 446 |
|
446 | 447 |
|
| 448 | +def test_ssm_provider_get_with_custom_client(mock_name, mock_value, mock_version, config): |
| 449 | + """ |
| 450 | + Test SSMProvider.get() with a non-cached value |
| 451 | + """ |
| 452 | + |
| 453 | + client = boto3.client("ssm", config=config) |
| 454 | + |
| 455 | + # Create a new provider |
| 456 | + provider = parameters.SSMProvider(boto3_client=client) |
| 457 | + |
| 458 | + # Stub the boto3 client |
| 459 | + stubber = stub.Stubber(provider.client) |
| 460 | + response = { |
| 461 | + "Parameter": { |
| 462 | + "Name": mock_name, |
| 463 | + "Type": "String", |
| 464 | + "Value": mock_value, |
| 465 | + "Version": mock_version, |
| 466 | + "Selector": f"{mock_name}:{mock_version}", |
| 467 | + "SourceResult": "string", |
| 468 | + "LastModifiedDate": datetime(2015, 1, 1), |
| 469 | + "ARN": f"arn:aws:ssm:us-east-2:111122223333:parameter/{mock_name}", |
| 470 | + } |
| 471 | + } |
| 472 | + expected_params = {"Name": mock_name, "WithDecryption": False} |
| 473 | + stubber.add_response("get_parameter", response, expected_params) |
| 474 | + stubber.activate() |
| 475 | + |
| 476 | + try: |
| 477 | + value = provider.get(mock_name) |
| 478 | + |
| 479 | + assert value == mock_value |
| 480 | + stubber.assert_no_pending_responses() |
| 481 | + finally: |
| 482 | + stubber.deactivate() |
| 483 | + |
| 484 | + |
447 | 485 | def test_ssm_provider_get_default_config(monkeypatch, mock_name, mock_value, mock_version): |
448 | 486 | """ |
449 | 487 | Test SSMProvider.get() without specifying the config |
@@ -925,6 +963,37 @@ def test_secrets_provider_get(mock_name, mock_value, config): |
925 | 963 | stubber.deactivate() |
926 | 964 |
|
927 | 965 |
|
| 966 | +def test_secrets_provider_get_with_custom_client(mock_name, mock_value, config): |
| 967 | + """ |
| 968 | + Test SecretsProvider.get() with a non-cached value |
| 969 | + """ |
| 970 | + client = boto3.client("secretsmanager", config=config) |
| 971 | + |
| 972 | + # Create a new provider |
| 973 | + provider = parameters.SecretsProvider(boto3_client=client) |
| 974 | + |
| 975 | + # Stub the boto3 client |
| 976 | + stubber = stub.Stubber(provider.client) |
| 977 | + response = { |
| 978 | + "ARN": f"arn:aws:secretsmanager:us-east-1:132456789012:secret/{mock_name}", |
| 979 | + "Name": mock_name, |
| 980 | + "VersionId": "7a9155b8-2dc9-466e-b4f6-5bc46516c84d", |
| 981 | + "SecretString": mock_value, |
| 982 | + "CreatedDate": datetime(2015, 1, 1), |
| 983 | + } |
| 984 | + expected_params = {"SecretId": mock_name} |
| 985 | + stubber.add_response("get_secret_value", response, expected_params) |
| 986 | + stubber.activate() |
| 987 | + |
| 988 | + try: |
| 989 | + value = provider.get(mock_name) |
| 990 | + |
| 991 | + assert value == mock_value |
| 992 | + stubber.assert_no_pending_responses() |
| 993 | + finally: |
| 994 | + stubber.deactivate() |
| 995 | + |
| 996 | + |
928 | 997 | def test_secrets_provider_get_default_config(monkeypatch, mock_name, mock_value): |
929 | 998 | """ |
930 | 999 | Test SecretsProvider.get() without specifying a config |
@@ -1555,6 +1624,37 @@ def test_appconf_provider_get_configuration_json_content_type(mock_name, config) |
1555 | 1624 | stubber.deactivate() |
1556 | 1625 |
|
1557 | 1626 |
|
| 1627 | +def test_appconf_provider_get_configuration_json_content_type_with_custom_client(mock_name, config): |
| 1628 | + """ |
| 1629 | + Test get_configuration.get with default values |
| 1630 | + """ |
| 1631 | + |
| 1632 | + client = boto3.client("appconfig", config=config) |
| 1633 | + |
| 1634 | + # Create a new provider |
| 1635 | + environment = "dev" |
| 1636 | + application = "myapp" |
| 1637 | + provider = parameters.AppConfigProvider(environment=environment, application=application, boto3_client=client) |
| 1638 | + |
| 1639 | + mock_body_json = {"myenvvar1": "Black Panther", "myenvvar2": 3} |
| 1640 | + encoded_message = json.dumps(mock_body_json).encode("utf-8") |
| 1641 | + mock_value = StreamingBody(BytesIO(encoded_message), len(encoded_message)) |
| 1642 | + |
| 1643 | + # Stub the boto3 client |
| 1644 | + stubber = stub.Stubber(provider.client) |
| 1645 | + response = {"Content": mock_value, "ConfigurationVersion": "1", "ContentType": "application/json"} |
| 1646 | + stubber.add_response("get_configuration", response) |
| 1647 | + stubber.activate() |
| 1648 | + |
| 1649 | + try: |
| 1650 | + value = provider.get(mock_name, transform="json", ClientConfigurationVersion="2") |
| 1651 | + |
| 1652 | + assert value == mock_body_json |
| 1653 | + stubber.assert_no_pending_responses() |
| 1654 | + finally: |
| 1655 | + stubber.deactivate() |
| 1656 | + |
| 1657 | + |
1558 | 1658 | def test_appconf_provider_get_configuration_no_transform(mock_name, config): |
1559 | 1659 | """ |
1560 | 1660 | Test appconfigprovider.get with default values |
|
0 commit comments