@@ -14,30 +14,43 @@ def mock_boto3(mocker):
1414 return s3helper .boto3
1515
1616
17- def test_get_input_artifact (mock_boto3 ):
17+ @pytest .fixture
18+ def mock_zipfile (mocker ):
19+ mocker .patch .object (s3helper , 'zipfile' )
20+ return s3helper .zipfile
21+
22+
23+ def test_get_input_artifact (mock_boto3 , mock_zipfile ):
24+ zipped_content_as_bytes = b'zipped_packaged_template_content'
25+ expected_result = 'packaged_template_content'
26+
1827 mock_s3 = MagicMock ()
1928 mock_boto3 .client .return_value = mock_s3
20- mock_s3 .get_object .return_value .get .return_value .read .return_value .decode .return_value = 'packaged_template_content'
29+ mock_s3 .get_object .return_value .get .return_value .read .return_value = zipped_content_as_bytes
30+ mock_zipfile_object = MagicMock ()
31+ mock_zipfile .ZipFile .return_value = mock_zipfile_object
32+ mock_zipfile_object .read .return_value = bytes (expected_result , encoding = 'utf-8' )
2133
22- s3helper .get_input_artifact (mock_codepipeline_event )
34+ assert s3helper .get_input_artifact (mock_codepipeline_event ) == expected_result
2335
2436 mock_s3 .get_object .assert_called_once_with (
2537 Bucket = 'sample-pipeline-artifact-store-bucket' ,
2638 Key = 'sample-artifact-key'
2739 )
2840
2941
30- def test_get_input_artifact_unable_to_find_artifact (mock_boto3 ):
42+ def test_get_input_artifact_unable_to_find_artifact (mock_boto3 , mock_zipfile ):
3143 mock_s3 = MagicMock ()
3244 mock_boto3 .client .return_value = mock_s3
3345
3446 with pytest .raises (RuntimeError , match = 'Unable to find the artifact with name ' + s3helper .PACKAGED_TEMPLATE ):
3547 s3helper .get_input_artifact (mock_codepipeline_event_no_artifact_found )
3648
3749 mock_s3 .get_object .assert_not_called ()
50+ mock_zipfile .assert_not_called ()
3851
3952
40- def test_get_input_artifact_unable_to_get_artifact (mock_boto3 ):
53+ def test_get_input_artifact_unable_to_get_artifact (mock_boto3 , mock_zipfile ):
4154 exception_thrown = ClientError (
4255 {
4356 "Error" : {
@@ -59,3 +72,4 @@ def test_get_input_artifact_unable_to_get_artifact(mock_boto3):
5972 Bucket = 'sample-pipeline-artifact-store-bucket' ,
6073 Key = 'sample-artifact-key'
6174 )
75+ mock_zipfile .assert_not_called ()
0 commit comments