forked from nccgroup/ScoutSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
executable file
·133 lines (124 loc) · 4.56 KB
/
test_utils.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Import AWS utils
from ScoutSuite.providers.aws.utils import (
get_keys,
no_camel,
get_name,
is_throttled,
get_aws_account_id,
get_partition_name,
snake_keys,
)
from ScoutSuite.utils import *
import collections
import unittest
from unittest import mock
import datetime
#
# Test methods for ScoutSuite/utils.py
#
class TestScoutUtilsClass(unittest.TestCase):
def test_format_service_name(self):
assert format_service_name("iAm") == "IAM"
assert format_service_name("cloudformation") == "CloudFormation"
def test_get_keys(self):
test1 = {"a": "b", "c": "d"}
test2 = {"a": "", "e": "f"}
get_keys(test1, test2, "a")
assert test2["a"] == "b"
assert "c" not in test2
get_keys(test1, test2, "c")
assert test2["c"] == "d"
def test_no_camel(self):
assert no_camel("TestTest") == "test_test"
def test_is_throttled(self):
CustomException = collections.namedtuple("CustomException", "response")
# test the throttling cases
for t in ["Throttling", "RequestLimitExceeded", "ThrottlingException"]:
e = CustomException(response={"Error": {"Code": t}})
assert is_throttled(e)
# test the non-throttling exception
e = CustomException(response={"Error": {"Code": "Not Throttling"}})
assert not is_throttled(e)
# test the except block
e = CustomException(response={"Error": ""})
assert not is_throttled(e)
def test_get_name(self):
src = {
"Tags": [
{"Key": "Not Name", "Value": "xyz"},
{"Key": "Name", "Value": "abc"},
],
"default_attribute": "default_value",
}
dst = {}
default_attribute = "default_attribute"
assert get_name(src, dst, default_attribute) == "abc"
assert dst["name"] == "abc"
src = {
"Tags": [{"Key": "Not Name", "Value": "xyz"}],
"default_attribute": "default_value",
}
dst = {}
default_attribute = "default_attribute"
assert get_name(src, dst, default_attribute) == "default_value"
assert dst["name"] == "default_value"
def test_get_identity(self):
with mock.patch(
"ScoutSuite.providers.aws.utils.get_caller_identity",
return_value={"Arn": "a:b:c:d:e:f:"},
):
assert get_aws_account_id("") == "e"
def test_get_partition_name(self):
with mock.patch(
"ScoutSuite.providers.aws.utils.get_caller_identity",
return_value={"Arn": "a:b:c:d:e:f:"},
):
assert get_partition_name("") == "b"
def test_snake_case(self):
src = {
"AttributeDefinitions": [
{"AttributeName": "string", "AttributeType": "S"},
],
"TableName": "string",
"KeySchema": [{"AttributeName": "string", "KeyType": "HASH"},],
"TableStatus": "CREATING",
"CreationDateTime": datetime.datetime(2015, 1, 1, 1, 1, 1, 1, None),
"ProvisionedThroughput": {
"LastIncreaseDateTime": datetime.datetime(2015, 1, 1, 1, 1, 1, 1, None),
"LastDecreaseDateTime": datetime.datetime(2015, 1, 1, 1, 1, 1, 1, None),
"NumberOfDecreasesToday": 123,
"ReadCapacityUnits": 123,
"WriteCapacityUnits": 123,
},
"TableSizeBytes": 123,
"AnotherArray": [
"One",
"Two",
"AnotherThing",
]
}
dest = {
"attribute_definitions": [
{"attribute_name": "string", "attribute_type": "S"},
],
"table_name": "string",
"key_schema": [{"attribute_name": "string", "key_type": "HASH"}],
"table_status": "CREATING",
"creation_date_time": datetime.datetime(2015, 1, 1, 1, 1, 1, 1, None),
"provisioned_throughput": {
"last_increase_date_time": datetime.datetime(
2015, 1, 1, 1, 1, 1, 1, None
),
"last_decrease_date_time": datetime.datetime(
2015, 1, 1, 1, 1, 1, 1, None
),
"number_of_decreases_today": 123,
"read_capacity_units": 123,
"write_capacity_units": 123,
},
"table_size_bytes": 123,
"another_array": ["One", "Two", "AnotherThing"]
}
d = snake_keys(src)
self.maxDiff = None
self.assertEquals(d, dest)