Skip to content

Commit 5c4232a

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate unit/test_streaming_client_stream_encryptor.py from unittest to pytest (#127)
* Migrate unit/test_streaming_client_stream_encryptor.py from unittest to pytest * Added yield in apply_fixtures function and fixed typo in line 493
1 parent 8a01c3e commit 5c4232a

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

test/unit/test_streaming_client_stream_encryptor.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# language governing permissions and limitations under the License.
1313
"""Unit test suite for aws_encryption_sdk.streaming_client.StreamEncryptor"""
1414
import io
15-
import unittest
1615

1716
import pytest
1817
import six
@@ -36,8 +35,9 @@
3635
pytestmark = [pytest.mark.unit, pytest.mark.local]
3736

3837

39-
class TestStreamEncryptor(unittest.TestCase):
40-
def setUp(self):
38+
class TestStreamEncryptor(object):
39+
@pytest.fixture(autouse=True)
40+
def apply_fixtures(self):
4141
# Create mock key provider
4242
self.mock_key_provider = MagicMock()
4343
self.mock_key_provider.__class__ = MasterKeyProvider
@@ -144,8 +144,8 @@ def setUp(self):
144144
# Set up serialize_frame patch
145145
self.mock_serialize_frame_patcher = patch("aws_encryption_sdk.streaming_client.serialize_frame")
146146
self.mock_serialize_frame = self.mock_serialize_frame_patcher.start()
147-
148-
def tearDown(self):
147+
yield
148+
# Run tearDown
149149
self.mock_content_type_patcher.stop()
150150
self.mock_validate_frame_length_patcher.stop()
151151
self.mock_message_id_patcher.stop()
@@ -173,14 +173,15 @@ def test_init(self):
173173
assert test_encryptor.content_type is sentinel.content_type
174174

175175
def test_init_non_framed_message_too_large(self):
176-
with six.assertRaisesRegex(self, SerializationError, "Source too large for non-framed message"):
176+
with pytest.raises(SerializationError) as excinfo:
177177
StreamEncryptor(
178178
source=io.BytesIO(self.plaintext),
179179
key_provider=self.mock_key_provider,
180180
frame_length=0,
181181
algorithm=self.mock_algorithm,
182182
source_length=aws_encryption_sdk.internal.defaults.MAX_NON_FRAMED_SIZE + 1,
183183
)
184+
excinfo.match("Source too large for non-framed message")
184185

185186
def test_prep_message_no_master_keys(self):
186187
self.mock_key_provider.master_keys_for_encryption.return_value = sentinel.primary_master_key, set()
@@ -190,9 +191,9 @@ def test_prep_message_no_master_keys(self):
190191
frame_length=self.mock_frame_length,
191192
source_length=5,
192193
)
193-
194-
with six.assertRaisesRegex(self, MasterKeyProviderError, "No Master Keys available from Master Key Provider"):
194+
with pytest.raises(MasterKeyProviderError) as excinfo:
195195
test_encryptor._prep_message()
196+
excinfo.match("No Master Keys available from Master Key Provider")
196197

197198
def test_prep_message_primary_master_key_not_in_master_keys(self):
198199
self.mock_key_provider.master_keys_for_encryption.return_value = (
@@ -206,8 +207,9 @@ def test_prep_message_primary_master_key_not_in_master_keys(self):
206207
source_length=5,
207208
)
208209

209-
with six.assertRaisesRegex(self, MasterKeyProviderError, "Primary Master Key not in provided Master Keys"):
210+
with pytest.raises(MasterKeyProviderError) as excinfo:
210211
test_encryptor._prep_message()
212+
excinfo.match("Primary Master Key not in provided Master Keys")
211213

212214
def test_prep_message_algorithm_change(self):
213215
self.mock_encryption_materials.algorithm = Algorithm.AES_256_GCM_IV12_TAG16
@@ -217,13 +219,11 @@ def test_prep_message_algorithm_change(self):
217219
algorithm=Algorithm.AES_128_GCM_IV12_TAG16,
218220
source_length=128,
219221
)
220-
221-
with six.assertRaisesRegex(
222-
self,
223-
ActionNotAllowedError,
224-
"Cryptographic materials manager provided algorithm suite differs from algorithm suite in request.*",
225-
):
222+
with pytest.raises(ActionNotAllowedError) as excinfo:
226223
test_encryptor._prep_message()
224+
excinfo.match(
225+
"Cryptographic materials manager provided algorithm suite differs from algorithm suite in request.*"
226+
)
227227

228228
@patch("aws_encryption_sdk.streaming_client.EncryptionMaterialsRequest")
229229
@patch("aws_encryption_sdk.streaming_client.derive_data_encryption_key")
@@ -396,8 +396,9 @@ def test_read_bytes_to_non_framed_body_too_large(self):
396396
test_encryptor.bytes_read = aws_encryption_sdk.internal.defaults.MAX_NON_FRAMED_SIZE
397397
test_encryptor._StreamEncryptor__unframed_plaintext_cache = pt_stream
398398

399-
with six.assertRaisesRegex(self, SerializationError, "Source too large for non-framed message"):
399+
with pytest.raises(SerializationError) as excinfo:
400400
test_encryptor._read_bytes_to_non_framed_body(5)
401+
excinfo.match("Source too large for non-framed message")
401402

402403
def test_read_bytes_to_non_framed_body_close(self):
403404
test_encryptor = StreamEncryptor(source=io.BytesIO(self.plaintext), key_provider=self.mock_key_provider)
@@ -485,8 +486,9 @@ def test_read_bytes_unsupported_type(self, mock_read_non_framed, mock_read_frame
485486
test_encryptor._encryption_materials = self.mock_encryption_materials
486487
test_encryptor._header = MagicMock()
487488
test_encryptor.content_type = None
488-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported content type"):
489+
with pytest.raises(NotSupportedError) as excinfo:
489490
test_encryptor._read_bytes(5)
491+
excinfo.match("Unsupported content type")
490492
assert not mock_read_non_framed.called
491493
assert not mock_read_framed.called
492494

0 commit comments

Comments
 (0)