Skip to content

Commit d6d1f87

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate "test/unit/test_streaming_client_stream_decryptor.py" from unittest to pytest (#126)
* Migrating unit/test_deserialize.py from unittest from pytest * Migrate unit/test_provides_kms_master_key.py from unittest to pytest * Migrated unit/test/_providers_kms_master_key_provider.py from unittest to pytest * unit/test_providers_raw_master_key.py * Migrate unit/test_providers_raw_master_key_provider.py from unittest from pytest * Migrate unit/test_serialize.py from unittest to pytest * Migrate unit/test_streaming_client_stream_decryptor.py from unittest to pytest * Autoformat * Removed unit tests not corresponding to this branch * Removed unused import and added yield and tearDown to apply_fixture function
1 parent ec60010 commit d6d1f87

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

test/unit/test_streaming_client_stream_decryptor.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
# language governing permissions and limitations under the License.
1313
"""Unit test suite for aws_encryption_sdk.streaming_client.StreamDecryptor"""
1414
import io
15-
import unittest
1615

1716
import pytest
18-
import six
1917
from mock import MagicMock, call, patch, sentinel
2018

2119
from aws_encryption_sdk.exceptions import CustomMaximumValueExceeded, NotSupportedError, SerializationError
@@ -29,8 +27,9 @@
2927
pytestmark = [pytest.mark.unit, pytest.mark.local]
3028

3129

32-
class TestStreamDecryptor(unittest.TestCase):
33-
def setUp(self):
30+
class TestStreamDecryptor(object):
31+
@pytest.fixture(autouse=True)
32+
def apply_fixtures(self):
3433
self.mock_key_provider = MagicMock(__class__=MasterKeyProvider)
3534
self.mock_materials_manager = MagicMock(__class__=CryptoMaterialsManager)
3635
self.mock_materials_manager.decrypt_materials.return_value = MagicMock(
@@ -92,8 +91,8 @@ def setUp(self):
9291
# Set up decrypt patch
9392
self.mock_decrypt_patcher = patch("aws_encryption_sdk.streaming_client.decrypt")
9493
self.mock_decrypt = self.mock_decrypt_patcher.start()
95-
96-
def tearDown(self):
94+
yield
95+
# Run tearDown
9796
self.mock_deserialize_header_patcher.stop()
9897
self.mock_deserialize_header_auth_patcher.stop()
9998
self.mock_validate_header_patcher.stop()
@@ -186,12 +185,11 @@ def test_read_header_frame_too_large(self, mock_derive_datakey):
186185
test_decryptor.key_provider = self.mock_key_provider
187186
test_decryptor.source_stream = ct_stream
188187
test_decryptor._stream_length = len(VALUES["data_128"])
189-
with six.assertRaisesRegex(
190-
self,
191-
CustomMaximumValueExceeded,
192-
"Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10),
193-
):
188+
with pytest.raises(CustomMaximumValueExceeded) as excinfo:
194189
test_decryptor._read_header()
190+
excinfo.match(
191+
"Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10)
192+
)
195193

196194
@patch("aws_encryption_sdk.streaming_client.Verifier")
197195
@patch("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest")
@@ -220,14 +218,13 @@ def test_prep_non_framed_content_length_too_large(self):
220218
mock_data_key = MagicMock()
221219
test_decryptor.data_key = mock_data_key
222220

223-
with six.assertRaisesRegex(
224-
self,
225-
CustomMaximumValueExceeded,
221+
with pytest.raises(CustomMaximumValueExceeded) as excinfo:
222+
test_decryptor._prep_non_framed()
223+
excinfo.match(
226224
"Non-framed message content length found larger than custom value: {found} > {custom}".format(
227225
found=len(VALUES["data_128"]), custom=len(VALUES["data_128"]) // 2
228-
),
229-
):
230-
test_decryptor._prep_non_framed()
226+
)
227+
)
231228

232229
def test_prep_non_framed(self):
233230
test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=self.mock_input_stream)
@@ -287,10 +284,9 @@ def test_read_bytes_from_non_framed_message_body_too_small(self):
287284
test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=ct_stream)
288285
test_decryptor.body_length = len(VALUES["data_128"] * 2)
289286
test_decryptor._header = self.mock_header
290-
with six.assertRaisesRegex(
291-
self, SerializationError, "Total message body contents less than specified in body description"
292-
):
287+
with pytest.raises(SerializationError) as excinfo:
293288
test_decryptor._read_bytes_from_non_framed_body(1)
289+
excinfo.match("Total message body contents less than specified in body description")
294290

295291
def test_read_bytes_from_non_framed_no_verifier(self):
296292
ct_stream = io.BytesIO(VALUES["data_128"])
@@ -493,8 +489,9 @@ def test_read_bytes_from_framed_body_bad_sequence_number(self):
493489
frame_data.final_frame = False
494490
frame_data.ciphertext = b"asdfzxcv"
495491
self.mock_deserialize_frame.return_value = (frame_data, False)
496-
with six.assertRaisesRegex(self, SerializationError, "Malformed message: frames out of order"):
492+
with pytest.raises(SerializationError) as excinfo:
497493
test_decryptor._read_bytes_from_framed_body(4)
494+
excinfo.match("Malformed message: frames out of order")
498495

499496
@patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_non_framed_body")
500497
@patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_framed_body")
@@ -545,8 +542,9 @@ def test_read_bytes_unknown(self, mock_read_frame, mock_read_block):
545542
test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=ct_stream)
546543
test_decryptor._header = MagicMock()
547544
test_decryptor._header.content_type = None
548-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported content type"):
545+
with pytest.raises(NotSupportedError) as excinfo:
549546
test_decryptor._read_bytes(5)
547+
excinfo.match("Unsupported content type")
550548

551549
@patch("aws_encryption_sdk.streaming_client._EncryptionStream.close")
552550
def test_close(self, mock_close):
@@ -561,5 +559,6 @@ def test_close(self, mock_close):
561559
def test_close_no_footer(self, mock_close):
562560
self.mock_header.content_type = ContentType.FRAMED_DATA
563561
test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=self.mock_input_stream)
564-
with six.assertRaisesRegex(self, SerializationError, "Footer not read"):
562+
with pytest.raises(SerializationError) as excinfo:
565563
test_decryptor.close()
564+
excinfo.match("Footer not read")

0 commit comments

Comments
 (0)