Skip to content

Commit b670bdb

Browse files
authored
add missing tests (#505)
1 parent 72ce384 commit b670bdb

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

examples/files/accounts.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
accounts:
2+
default_account: 123456789012
3+
prod: 123456789013
4+
test: 123456789014

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Twitter = "https://twitter.com/kmcquade3"
4949
dev = [
5050
"boto3-stubs-lite[iam,s3,sts]>=1.40.0,<2.0.0",
5151
"coverage>=7.11.0,<8.0.0",
52+
"moto[sts]>=5.1.0,<6.0.0",
5253
"mypy>=1.18.0,<2.0.0",
5354
"prek>=0.2.13,<0.3.0",
5455
"pytest>=8.4.0,<9.0.0",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import shutil
2+
import tempfile
3+
import unittest
4+
from pathlib import Path
5+
6+
from click.testing import CliRunner
7+
from moto import mock_aws
8+
9+
from cloudsplaining.command.scan_multi_account import scan_multi_account
10+
11+
12+
class ScanClickUnitTests(unittest.TestCase):
13+
def setUp(self):
14+
self.runner = CliRunner()
15+
self.temp_dir = tempfile.mkdtemp()
16+
17+
def tearDown(self):
18+
shutil.rmtree(self.temp_dir)
19+
20+
@mock_aws
21+
def test_scan_example_file_with_click(self):
22+
# given
23+
examples_directory = Path(__file__).parents[2] / "examples"
24+
config_file = examples_directory / "files/accounts.yaml"
25+
26+
args = ["--config", config_file, "--role-name", "example-role", "--output-directory", self.temp_dir, "-v"]
27+
28+
# when
29+
response = self.runner.invoke(cli=scan_multi_account, args=args)
30+
31+
# then
32+
self.assertTrue(response.exit_code == 0)
33+
34+
# 3 accounts -> 3 reports
35+
self.assertEqual(len(list(Path(self.temp_dir).glob("*.html"))), 3)
36+
self.assertEqual(response.output.count("Scanning account"), 3)
37+
self.assertEqual(response.output.count("Saved the HTML report to"), 3)

test/shared/test_aws_login.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import boto3
2+
from moto import mock_aws
3+
4+
from cloudsplaining.shared.aws_login import (
5+
get_current_account_id,
6+
get_available_regions,
7+
get_target_account_credentials,
8+
get_boto3_client,
9+
get_boto3_resource,
10+
)
11+
12+
13+
@mock_aws
14+
def test_get_boto3_client():
15+
# given
16+
service = "sts"
17+
region = "eu-central-1"
18+
19+
# when
20+
client = get_boto3_client(service=service, region=region)
21+
22+
# then
23+
assert client # just make sure it doesn't raise an exception
24+
25+
26+
@mock_aws
27+
def test_get_boto3_resource():
28+
# given
29+
service = "s3"
30+
region = "eu-central-1"
31+
32+
# when
33+
resource = get_boto3_resource(service=service, region=region)
34+
35+
# then
36+
assert resource # just make sure it doesn't raise an exception
37+
38+
39+
@mock_aws
40+
def test_get_current_account_id():
41+
# given
42+
client = boto3.client("sts")
43+
44+
# when
45+
account_id = get_current_account_id(client)
46+
47+
# then
48+
assert account_id # make sure it is not empty
49+
50+
51+
@mock_aws
52+
def test_get_available_regions():
53+
# given
54+
service = "s3"
55+
56+
# when
57+
regions = get_available_regions(service)
58+
59+
# then
60+
assert len(regions) >= 30
61+
62+
63+
@mock_aws
64+
def test_get_target_account_credentialscredentials():
65+
# given
66+
role_name = "example-role"
67+
account_id = "111111111111"
68+
69+
# when
70+
creds = get_target_account_credentials(
71+
target_account_role_name=role_name,
72+
target_account_id=account_id,
73+
)
74+
75+
# then
76+
assert len(creds) == 3
77+
assert creds[0] # make sure it is not empty
78+
assert creds[1] # make sure it is not empty
79+
assert creds[2] # make sure it is not empty

0 commit comments

Comments
 (0)