Skip to content

Commit 7fdd7e8

Browse files
committed
add unit test
1 parent af87910 commit 7fdd7e8

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

tests/test_api.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
import unittest
3+
from unittest.mock import patch, MagicMock
4+
5+
import datadog_lambda.api as api
6+
7+
class TestDatadogLambdaAPI(unittest.TestCase):
8+
def setUp(self):
9+
api.api_key = None
10+
self.env_patcher = patch.dict(os.environ, {
11+
"DD_API_KEY_SECRET_ARN": "",
12+
"DD_API_KEY_SSM_NAME": "",
13+
"DD_KMS_API_KEY": "",
14+
"DD_API_KEY": "",
15+
"DATADOG_API_KEY": "",
16+
"AWS_REGION": "",
17+
}, clear=True)
18+
self.env_patcher.start()
19+
20+
@patch('boto3.client')
21+
def test_secrets_manager_fips_endpoint(self, mock_boto3_client):
22+
mock_client = MagicMock()
23+
mock_client.get_secret_value.return_value = {"SecretString": "test-api-key"}
24+
mock_boto3_client.return_value = mock_client
25+
26+
os.environ["AWS_REGION"] = "us-gov-east-1"
27+
os.environ["DD_API_KEY_SECRET_ARN"] = "test-secrets-arn"
28+
29+
api_key = api.get_api_key()
30+
31+
mock_boto3_client.assert_called_with(
32+
"secretsmanager",
33+
endpoint_url="https://secretsmanager-fips.us-gov-east-1.amazonaws.com"
34+
)
35+
self.assertEqual(api_key, "test-api-key")
36+
37+
@patch('boto3.client')
38+
def test_ssm_fips_endpoint(self, mock_boto3_client):
39+
mock_client = MagicMock()
40+
mock_client.get_parameter.return_value = {"Parameter": {"Value": "test-api-key"}}
41+
mock_boto3_client.return_value = mock_client
42+
43+
os.environ["AWS_REGION"] = "us-gov-west-1"
44+
os.environ["DD_API_KEY_SSM_NAME"] = "test-ssm-param"
45+
46+
api_key = api.get_api_key()
47+
48+
mock_boto3_client.assert_called_with(
49+
"ssm",
50+
endpoint_url="https://ssm-fips.us-gov-west-1.amazonaws.com"
51+
)
52+
self.assertEqual(api_key, "test-api-key")
53+
54+
@patch('boto3.client')
55+
@patch('datadog_lambda.api.decrypt_kms_api_key')
56+
def test_kms_fips_endpoint(self, mock_decrypt_kms, mock_boto3_client):
57+
mock_client = MagicMock()
58+
mock_boto3_client.return_value = mock_client
59+
mock_decrypt_kms.return_value = "test-api-key"
60+
61+
os.environ["AWS_REGION"] = "us-gov-west-1"
62+
os.environ["DD_KMS_API_KEY"] = "encrypted-api-key"
63+
64+
api_key = api.get_api_key()
65+
66+
mock_boto3_client.assert_called_with(
67+
"kms",
68+
endpoint_url="https://kms-fips.us-gov-west-1.amazonaws.com"
69+
)
70+
self.assertEqual(api_key, "test-api-key")
71+
72+
@patch('boto3.client')
73+
def test_no_fips_for_standard_regions(self, mock_boto3_client):
74+
mock_client = MagicMock()
75+
mock_client.get_secret_value.return_value = {"SecretString": "test-api-key"}
76+
mock_boto3_client.return_value = mock_client
77+
78+
os.environ.clear()
79+
os.environ["AWS_REGION"] = "us-west-2"
80+
os.environ["DD_API_KEY_SECRET_ARN"] = "test-arn"
81+
82+
api.get_api_key()
83+
84+
mock_boto3_client.assert_called_with("secretsmanager", endpoint_url=None)

0 commit comments

Comments
 (0)