don't use table of test cases and reduce duplication in get_policy method
This commit is contained in:
parent
2a2ff32dec
commit
cb07d17e13
2 changed files with 35 additions and 100 deletions
|
|
@ -12,93 +12,39 @@ class MockLambdaFunction:
|
|||
self.policy = None
|
||||
|
||||
|
||||
class TC:
|
||||
def __init__(self, lambda_arn, statement, expected):
|
||||
self.statement = statement
|
||||
self.expected = expected
|
||||
self.fn = MockLambdaFunction(lambda_arn)
|
||||
self.policy = Policy(self.fn)
|
||||
class TestPolicy(unittest.TestCase):
|
||||
def test_policy(self):
|
||||
policy = Policy(MockLambdaFunction("arn"))
|
||||
statement = {
|
||||
"StatementId": "statement0",
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": "events.amazonaws.com",
|
||||
"SourceArn": "arn:aws:events:us-east-1:111111111111:rule/rule_name",
|
||||
"SourceAccount": "111111111111",
|
||||
}
|
||||
|
||||
def Run(self, parent):
|
||||
self.policy.add_statement(json.dumps(self.statement))
|
||||
parent.assertDictEqual(self.expected, self.policy.statements[0])
|
||||
expected = {
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": {"Service": "events.amazonaws.com"},
|
||||
"Effect": "Allow",
|
||||
"Resource": "arn:$LATEST",
|
||||
"Sid": "statement0",
|
||||
"Condition": {
|
||||
"ArnLike": {
|
||||
"AWS:SourceArn": "arn:aws:events:us-east-1:111111111111:rule/rule_name",
|
||||
},
|
||||
"StringEquals": {"AWS:SourceAccount": "111111111111"},
|
||||
},
|
||||
}
|
||||
|
||||
sid = self.statement.get("StatementId", None)
|
||||
policy.add_statement(json.dumps(statement))
|
||||
self.assertDictEqual(expected, policy.statements[0])
|
||||
|
||||
sid = statement.get("StatementId", None)
|
||||
if sid == None:
|
||||
raise "TestCase.statement does not contain StatementId"
|
||||
|
||||
self.policy.del_statement(sid)
|
||||
parent.assertEqual([], self.policy.statements)
|
||||
|
||||
|
||||
class TestPolicy(unittest.TestCase):
|
||||
def test(self):
|
||||
tt = [
|
||||
TC(
|
||||
# lambda_arn
|
||||
"arn",
|
||||
{ # statement
|
||||
"StatementId": "statement0",
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": "events.amazonaws.com",
|
||||
},
|
||||
{ # expected
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": {"Service": "events.amazonaws.com"},
|
||||
"Effect": "Allow",
|
||||
"Resource": "arn:$LATEST",
|
||||
"Sid": "statement0",
|
||||
},
|
||||
),
|
||||
TC(
|
||||
# lambda_arn
|
||||
"arn",
|
||||
{ # statement
|
||||
"StatementId": "statement1",
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": "events.amazonaws.com",
|
||||
"SourceArn": "arn:aws:events:us-east-1:111111111111:rule/rule_name",
|
||||
},
|
||||
{
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": {"Service": "events.amazonaws.com"},
|
||||
"Effect": "Allow",
|
||||
"Resource": "arn:$LATEST",
|
||||
"Sid": "statement1",
|
||||
"Condition": {
|
||||
"ArnLike": {
|
||||
"AWS:SourceArn": "arn:aws:events:us-east-1:111111111111:rule/rule_name"
|
||||
}
|
||||
},
|
||||
},
|
||||
),
|
||||
TC(
|
||||
# lambda_arn
|
||||
"arn",
|
||||
{ # statement
|
||||
"StatementId": "statement2",
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": "events.amazonaws.com",
|
||||
"SourceAccount": "111111111111",
|
||||
},
|
||||
{ # expected
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"FunctionName": "function_name",
|
||||
"Principal": {"Service": "events.amazonaws.com"},
|
||||
"Effect": "Allow",
|
||||
"Resource": "arn:$LATEST",
|
||||
"Sid": "statement2",
|
||||
"Condition": {
|
||||
"StringEquals": {"AWS:SourceAccount": "111111111111"}
|
||||
},
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
for tc in tt:
|
||||
tc.Run(self)
|
||||
policy.del_statement(sid)
|
||||
self.assertEqual([], policy.statements)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue