forked from jksprattler/aws-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_secret_rotation_s3.py
More file actions
49 lines (37 loc) · 1.59 KB
/
Copy pathtest_secret_rotation_s3.py
File metadata and controls
49 lines (37 loc) · 1.59 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
#!/usr/bin/env python3
"""Unit tests for secret rotation S3 storage script."""
import unittest
from unittest.mock import Mock, patch
# Add scripts directory to path
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))
import aws_iam_secret_rotation_s3 as rot # noqa: E402
class TestRotateAndStore(unittest.TestCase):
"""Test rotate_and_store_credentials function."""
@patch("aws_iam_secret_rotation_s3.create_secure_bucket")
@patch("aws_iam_secret_rotation_s3.get_s3_client")
@patch("aws_iam_secret_rotation_s3.get_iam_client")
def test_rotate_and_store_success(self, mock_get_iam, mock_get_s3, mock_bucket):
iam_client = Mock()
s3_client = Mock()
mock_get_iam.return_value = iam_client
mock_get_s3.return_value = s3_client
mock_bucket.return_value = "test-bucket"
iam_client.get_user.return_value = {"User": {"UserName": "test"}}
iam_client.create_access_key.return_value = {
"AccessKey": {
"AccessKeyId": "AKIATEST",
"SecretAccessKey": "secret",
}
}
s3_client.generate_presigned_url.return_value = "https://example.com/url"
url = rot.rotate_and_store_credentials("OLDKEY")
iam_client.update_access_key.assert_called_once_with(
UserName="test", AccessKeyId="OLDKEY", Status="Inactive"
)
s3_client.put_object.assert_called_once()
mock_bucket.assert_called_once()
self.assertEqual(url, "https://example.com/url")
if __name__ == "__main__":
unittest.main()