Skip to content

Commit a6a80a0

Browse files
committed
Fix tests
1 parent a233818 commit a6a80a0

32 files changed

+1119
-189
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ black-lint:
2626
black --check .
2727

2828
ruff-lint:
29-
ruff .
29+
ruff check .
3030

3131
unit:
3232
pytest -svvv tests

Diff for: pycfmodel/model/cf_model.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ def resolve(self, extra_params=None) -> "CFModel":
8181
resolved_conditions = {}
8282
for key, value in conditions.items():
8383
resolved_conditions.update(
84-
{key: _extended_bool(resolve(value, extended_parameters, self.Mappings, resolved_conditions))}
84+
{
85+
key: _extended_bool(
86+
resolve(
87+
value,
88+
extended_parameters,
89+
self.Mappings,
90+
resolved_conditions,
91+
)
92+
)
93+
}
8594
)
8695

8796
resources = dict_value.pop("Resources")

Diff for: pycfmodel/model/resources/iam_managed_policy.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class IAMManagedPolicy(Resource):
4848
def policy_documents(self) -> List[OptionallyNamedPolicyDocument]:
4949
return [
5050
OptionallyNamedPolicyDocument(
51-
name=self.Properties.ManagedPolicyName, policy_document=self.Properties.PolicyDocument
51+
name=self.Properties.ManagedPolicyName,
52+
policy_document=self.Properties.PolicyDocument,
5253
)
5354
]

Diff for: pycfmodel/model/resources/iam_policy.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class IAMPolicy(Resource):
4444
def policy_documents(self) -> List[OptionallyNamedPolicyDocument]:
4545
return [
4646
OptionallyNamedPolicyDocument(
47-
name=self.Properties.PolicyName, policy_document=self.Properties.PolicyDocument
47+
name=self.Properties.PolicyName,
48+
policy_document=self.Properties.PolicyDocument,
4849
)
4950
]

Diff for: pycfmodel/model/resources/iam_role.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def policy_documents(self) -> List[OptionallyNamedPolicyDocument]:
5757
return result
5858

5959
@property
60-
def assume_role_as_optionally_named_policy_document_list(self) -> List[OptionallyNamedPolicyDocument]:
60+
def assume_role_as_optionally_named_policy_document_list(
61+
self,
62+
) -> List[OptionallyNamedPolicyDocument]:
6163
return [OptionallyNamedPolicyDocument(name=None, policy_document=self.Properties.AssumeRolePolicyDocument)]
6264

6365
@property

Diff for: pycfmodel/model/resources/properties/statement_condition.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,20 @@ def build_evaluator(function: str, arg_a: Any, arg_b: Any) -> Callable:
4545
elif function == "Null":
4646
return lambda kwargs: (kwargs.get(arg_a) is not None) is arg_b
4747

48-
elif function in ("StringEquals", "ArnEquals", "BinaryEquals", "NumericEquals", "DateEquals"):
48+
elif function in (
49+
"StringEquals",
50+
"ArnEquals",
51+
"BinaryEquals",
52+
"NumericEquals",
53+
"DateEquals",
54+
):
4955
return lambda kwargs: kwargs[arg_a] == arg_b
50-
elif function in ("StringNotEquals", "ArnNotEquals", "NumericNotEquals", "DateNotEquals"):
56+
elif function in (
57+
"StringNotEquals",
58+
"ArnNotEquals",
59+
"NumericNotEquals",
60+
"DateNotEquals",
61+
):
5162
return lambda kwargs: kwargs[arg_a] != arg_b
5263

5364
elif function in ("NumericLessThan", "DateLessThan"):

Diff for: pycfmodel/model/resources/properties/types.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
from pycfmodel.model.resources.properties.tag import Tag
1010

1111
Properties = Union[
12-
Policy, PolicyDocument, SecurityGroupEgressProp, SecurityGroupIngressProp, Statement, StatementCondition, Tag
12+
Policy,
13+
PolicyDocument,
14+
SecurityGroupEgressProp,
15+
SecurityGroupIngressProp,
16+
Statement,
17+
StatementCondition,
18+
Tag,
1319
]

Diff for: pycfmodel/model/resources/resource.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def policy_documents(self) -> List[OptionallyNamedPolicyDocument]:
5757
return policy_documents
5858

5959
self.obtain_policy_documents(
60-
policy_documents=policy_documents, properties=list(self.Properties.__dict__.values())
60+
policy_documents=policy_documents,
61+
properties=list(self.Properties.__dict__.values()),
6162
)
6263
return policy_documents
6364

@@ -71,7 +72,8 @@ def obtain_policy_documents(self, policy_documents: List, properties: List[Any])
7172
elif isinstance(property_type, Policy):
7273
policy_documents.append(
7374
OptionallyNamedPolicyDocument(
74-
name=property_type.PolicyName, policy_document=property_type.PolicyDocument
75+
name=property_type.PolicyName,
76+
policy_document=property_type.PolicyDocument,
7577
)
7678
)
7779
elif isinstance(property_type, OptionallyNamedPolicyDocument):
@@ -80,7 +82,8 @@ def obtain_policy_documents(self, policy_documents: List, properties: List[Any])
8082
self.obtain_policy_documents(policy_documents=policy_documents, properties=property_type)
8183
elif isinstance(property_type, Generic):
8284
self.obtain_policy_documents(
83-
policy_documents=policy_documents, properties=list(property_type.__dict__.values())
85+
policy_documents=policy_documents,
86+
properties=list(property_type.__dict__.values()),
8487
)
8588

8689
@property

Diff for: pycfmodel/resolver.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class _BooleanModel(BaseModel):
2222
bool_value: bool
2323

2424

25-
def resolve(function: ValidResolvers, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]):
25+
def resolve(
26+
function: ValidResolvers,
27+
params: Dict,
28+
mappings: Dict[str, Dict],
29+
conditions: Dict[str, bool],
30+
):
2631
if function is None:
2732
return function
2833

@@ -147,24 +152,49 @@ def resolve_if(function_body, params: Dict, mappings: Dict[str, Dict], condition
147152
return resolve(false_section, params, mappings, conditions)
148153

149154

150-
def resolve_and(function_body: List, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]) -> bool:
155+
def resolve_and(
156+
function_body: List,
157+
params: Dict,
158+
mappings: Dict[str, Dict],
159+
conditions: Dict[str, bool],
160+
) -> bool:
151161
return all(_extended_bool(resolve(part, params, mappings, conditions)) for part in function_body)
152162

153163

154-
def resolve_or(function_body: List, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]) -> bool:
164+
def resolve_or(
165+
function_body: List,
166+
params: Dict,
167+
mappings: Dict[str, Dict],
168+
conditions: Dict[str, bool],
169+
) -> bool:
155170
return any(_extended_bool(resolve(part, params, mappings, conditions)) for part in function_body)
156171

157172

158-
def resolve_not(function_body: List, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]) -> bool:
173+
def resolve_not(
174+
function_body: List,
175+
params: Dict,
176+
mappings: Dict[str, Dict],
177+
conditions: Dict[str, bool],
178+
) -> bool:
159179
return not _extended_bool(resolve(function_body[0], params, mappings, conditions))
160180

161181

162-
def resolve_equals(function_body: List, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]) -> bool:
182+
def resolve_equals(
183+
function_body: List,
184+
params: Dict,
185+
mappings: Dict[str, Dict],
186+
conditions: Dict[str, bool],
187+
) -> bool:
163188
part_1, part_2 = function_body
164189
return resolve(part_1, params, mappings, conditions) == resolve(part_2, params, mappings, conditions)
165190

166191

167-
def resolve_base64(function_body: str, params: Dict, mappings: Dict[str, Dict], conditions: Dict[str, bool]) -> str:
192+
def resolve_base64(
193+
function_body: str,
194+
params: Dict,
195+
mappings: Dict[str, Dict],
196+
conditions: Dict[str, bool],
197+
) -> str:
168198
resolved_string = resolve(function_body, params, mappings, conditions)
169199
return str(b64encode(resolved_string.encode("utf-8")), "utf-8")
170200

Diff for: pyproject.toml

+63
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,66 @@ include_trailing_comma = true
88
force_grid_wrap = 0
99
use_parentheses = true
1010
line_length = 120
11+
12+
[tool.ruff]
13+
# Exclude a variety of commonly ignored directories.
14+
exclude = [
15+
".bzr",
16+
".direnv",
17+
".eggs",
18+
".git",
19+
".git-rewrite",
20+
".hg",
21+
".ipynb_checkpoints",
22+
".mypy_cache",
23+
".nox",
24+
".pants.d",
25+
".pyenv",
26+
".pytest_cache",
27+
".pytype",
28+
".ruff_cache",
29+
".svn",
30+
".tox",
31+
".venv",
32+
".vscode",
33+
"__pypackages__",
34+
"_build",
35+
"buck-out",
36+
"build",
37+
"dist",
38+
"node_modules",
39+
"site-packages",
40+
"venv",
41+
]
42+
43+
# Same as Black.
44+
line-length = 120
45+
indent-width = 4
46+
47+
# Assume Python 3.7
48+
target-version = "py37"
49+
50+
[tool.ruff.lint]
51+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
52+
select = ["E4", "E7", "E9", "F"]
53+
ignore = []
54+
55+
# Allow fix for all enabled rules (when `--fix`) is provided.
56+
fixable = ["ALL"]
57+
unfixable = []
58+
59+
# Allow unused variables when underscore-prefixed.
60+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
61+
62+
[tool.ruff.format]
63+
# Like Black, use double quotes for strings.
64+
quote-style = "double"
65+
66+
# Like Black, indent with spaces, rather than tabs.
67+
indent-style = "space"
68+
69+
# Like Black, respect magic trailing commas.
70+
skip-magic-trailing-comma = false
71+
72+
# Like Black, automatically detect the appropriate line ending.
73+
line-ending = "auto"

Diff for: tests/resources/properties/test_policy_document.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def policy_document_one_statement():
1616
"Version": "2012-10-17",
1717
"Statement": {
1818
"Effect": "Allow",
19-
"Principal": {"Service": ["ec2.amazonaws.com"], "AWS": "arn:aws:iam::324320755747:root"},
19+
"Principal": {
20+
"Service": ["ec2.amazonaws.com"],
21+
"AWS": "arn:aws:iam::324320755747:root",
22+
},
2023
"Action": ["sts:AssumeRole"],
2124
},
2225
}
@@ -31,12 +34,18 @@ def policy_document_multi_statement():
3134
"Statement": [
3235
{
3336
"Effect": "Allow",
34-
"Principal": {"Service": ["ec2.amazonaws.com"], "AWS": "arn:aws:iam::324320755747:root"},
37+
"Principal": {
38+
"Service": ["ec2.amazonaws.com"],
39+
"AWS": "arn:aws:iam::324320755747:root",
40+
},
3541
"Action": ["sts:AssumeRole"],
3642
},
3743
{
3844
"Effect": "Allow",
39-
"Principal": {"Service": ["ec2.amazonaws.com"], "AWS": "arn:aws:iam::324320755747:root"},
45+
"Principal": {
46+
"Service": ["ec2.amazonaws.com"],
47+
"AWS": "arn:aws:iam::324320755747:root",
48+
},
4049
"Action": ["sts:AssumeRole"],
4150
},
4251
],
@@ -47,7 +56,16 @@ def policy_document_multi_statement():
4756
@fixture
4857
def policy_document_star_resource():
4958
return PolicyDocument(
50-
**{"Statement": [{"Action": ["*"], "Effect": "Allow", "Resource": "*", "Principal": {"AWS": ["156460612806"]}}]}
59+
**{
60+
"Statement": [
61+
{
62+
"Action": ["*"],
63+
"Effect": "Allow",
64+
"Resource": "*",
65+
"Principal": {"AWS": ["156460612806"]},
66+
}
67+
]
68+
}
5169
)
5270

5371

@@ -344,13 +362,20 @@ def test_policy_document_chan_check_if_the_statement_effect_is_allow_or_deny(sta
344362
assert PolicyDocument._is_statement_effect_allow(statement_effect=statement_effect) == is_allow
345363

346364

347-
def test_policy_document_condition_with_source_ip(policy_document_condition_with_source_ip: PolicyDocument):
365+
def test_policy_document_condition_with_source_ip(
366+
policy_document_condition_with_source_ip: PolicyDocument,
367+
):
348368
assert policy_document_condition_with_source_ip.Statement[0].Condition.IpAddress == {
349-
"aws:SourceIp": [IPv4Network("116.202.65.160/32"), IPv4Network("116.202.68.32/27")]
369+
"aws:SourceIp": [
370+
IPv4Network("116.202.65.160/32"),
371+
IPv4Network("116.202.68.32/27"),
372+
]
350373
}
351374

352375

353-
def test_policy_document_condition_with_source_vpce(policy_document_condition_with_source_vpce: PolicyDocument):
376+
def test_policy_document_condition_with_source_vpce(
377+
policy_document_condition_with_source_vpce: PolicyDocument,
378+
):
354379
assert policy_document_condition_with_source_vpce.Statement[0].Condition.IpAddress == {
355380
"aws:SourceVpce": ["vpce-123456"]
356381
}

Diff for: tests/resources/properties/test_principal.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
{"AWS": "arn:aws:iam::123456789012:role/role-name"},
1414
{"AWS": "arn:aws:sts::123456789012:federated-user/user-name"},
1515
{"AWS": ["123456789012", "555555555555"]},
16-
{"AWS": ["arn:aws:iam::123456789012:user/user-name-1", "arn:aws:iam::123456789012:user/user-name-2"]},
16+
{
17+
"AWS": [
18+
"arn:aws:iam::123456789012:user/user-name-1",
19+
"arn:aws:iam::123456789012:user/user-name-2",
20+
]
21+
},
1722
{"CanonicalUser": "79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be"},
1823
{
1924
"AWS": ["arn:aws:iam::123456789012:root", "999999999999"],

0 commit comments

Comments
 (0)