| 
									
										
										
										
											2020-01-23 12:46:24 -06:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2020-01-23 12:46:24 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | from moto.awslambda.policy import Policy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockLambdaFunction: | 
					
						
							|  |  |  |     def __init__(self, arn): | 
					
						
							|  |  |  |         self.function_arn = arn | 
					
						
							|  |  |  |         self.policy = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  | def test_policy(): | 
					
						
							|  |  |  |     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", | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expected = { | 
					
						
							|  |  |  |         "Action": "lambda:InvokeFunction", | 
					
						
							|  |  |  |         "FunctionName": "function_name", | 
					
						
							|  |  |  |         "Principal": {"Service": "events.amazonaws.com"}, | 
					
						
							|  |  |  |         "Effect": "Allow", | 
					
						
							| 
									
										
										
										
											2022-03-07 20:37:50 -01:00
										 |  |  |         "Resource": "arn", | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |         "Sid": "statement0", | 
					
						
							|  |  |  |         "Condition": { | 
					
						
							|  |  |  |             "ArnLike": { | 
					
						
							|  |  |  |                 "AWS:SourceArn": "arn:aws:events:us-east-1:111111111111:rule/rule_name", | 
					
						
							| 
									
										
										
										
											2020-01-27 10:35:46 -05:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |             "StringEquals": {"AWS:SourceAccount": "111111111111"}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-23 12:46:24 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |     policy.add_statement(json.dumps(statement)) | 
					
						
							|  |  |  |     expected.should.be.equal(policy.statements[0]) | 
					
						
							| 
									
										
										
										
											2020-01-23 12:46:24 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |     sid = statement.get("StatementId", None) | 
					
						
							| 
									
										
										
										
											2022-03-07 20:37:50 -01:00
										 |  |  |     if sid is None: | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |         raise "TestCase.statement does not contain StatementId" | 
					
						
							| 
									
										
										
										
											2020-01-23 12:46:24 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-29 09:46:09 -05:00
										 |  |  |     policy.del_statement(sid) | 
					
						
							|  |  |  |     [].should.be.equal(policy.statements) |