|
| 1 | +import pytest |
| 2 | + |
| 3 | +from botocore.exceptions import ClientError |
| 4 | +from datetime import datetime |
| 5 | +from nypl_py_utils.classes.secrets_manager_client import ( |
| 6 | + SecretsManagerClient, SecretsManagerClientError) |
| 7 | + |
| 8 | +_TEST_RESPONSE = { |
| 9 | + 'ARN': 'test_arn', |
| 10 | + 'Name': 'test_secret', |
| 11 | + 'VersionId': 'test_version', |
| 12 | + 'SecretString': '{\n "key1": "value1",\n "key2": "value2"\n}', |
| 13 | + 'VersionStages': ['AWSCURRENT'], |
| 14 | + 'CreatedDate': datetime(2024, 1, 1, 1, 1, 1, 1), |
| 15 | + 'ResponseMetadata': { |
| 16 | + 'RequestId': 'test-request-id', |
| 17 | + 'HTTPStatusCode': 200, |
| 18 | + 'HTTPHeaders': { |
| 19 | + 'x-amzn-requestid': 'test-request-id', |
| 20 | + 'content-type': 'application/x-amz-json-1.1', |
| 21 | + 'content-length': '155', |
| 22 | + 'date': 'Mon, 1 Jan 2024 07:01:01 GMT' |
| 23 | + }, |
| 24 | + 'RetryAttempts': 0} |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class TestSecretsManagerClient: |
| 29 | + |
| 30 | + @pytest.fixture |
| 31 | + def test_instance(self, mocker): |
| 32 | + mocker.patch('boto3.client') |
| 33 | + return SecretsManagerClient() |
| 34 | + |
| 35 | + def test_get_secret(self, test_instance): |
| 36 | + test_instance.secrets_manager_client.get_secret_value.return_value = \ |
| 37 | + _TEST_RESPONSE |
| 38 | + assert test_instance.get_secret('test_secret') == { |
| 39 | + 'key1': 'value1', 'key2': 'value2'} |
| 40 | + test_instance.secrets_manager_client.get_secret_value\ |
| 41 | + .assert_called_once_with(SecretId='test_secret') |
| 42 | + |
| 43 | + def test_get_secret_non_json(self, test_instance): |
| 44 | + test_instance.secrets_manager_client.get_secret_value.return_value = \ |
| 45 | + _TEST_RESPONSE |
| 46 | + assert test_instance.get_secret('test_secret', is_json=False) == ( |
| 47 | + '{\n "key1": "value1",\n "key2": "value2"\n}') |
| 48 | + test_instance.secrets_manager_client.get_secret_value\ |
| 49 | + .assert_called_once_with(SecretId='test_secret') |
| 50 | + |
| 51 | + def test_get_secret_error(self, test_instance): |
| 52 | + test_instance.secrets_manager_client.get_secret_value.side_effect = \ |
| 53 | + ClientError({}, 'GetSecretValue') |
| 54 | + with pytest.raises(SecretsManagerClientError): |
| 55 | + test_instance.get_secret('test_secret') |
0 commit comments