From 7b79bb88dd40e1a1a496070a757363514120079b Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Thu, 13 Dec 2018 14:31:42 -0600 Subject: [PATCH 1/3] Migrate unit/test_utils.py from unittest to pytest --- test/unit/test_utils.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index a94519b58..28bab0573 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -13,7 +13,6 @@ # language governing permissions and limitations under the License. """Test suite for aws_encryption_sdk.internal.utils""" import io -import unittest import pytest import six @@ -44,8 +43,9 @@ def test_prep_stream_data_wrap(source): assert_prepped_stream_identity(test, io.BytesIO) -class TestUtils(unittest.TestCase): - def setUp(self): +class TestUtils(object): + @pytest.fixture(autouse=True) + def apply_fixtures(self): # Set up mock key provider and keys self.mock_key_provider_1 = MasterKeyInfo(provider_id="adijoasijfoi", key_info=b"asoiwef8q34") self.mock_raw_data_key_1_bytes = b"asioufhaw9eruhtg" @@ -148,40 +148,35 @@ def test_validate_frame_length_negative_frame_length(self): behaves as expected when supplied with a negative frame length. """ - with six.assertRaisesRegex( - self, - SerializationError, - "Frame size must be a non-negative multiple of the block size of the crypto algorithm: *", - ): + with pytest.raises(SerializationError) as excinfo: aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=-1, algorithm=self.mock_algorithm) + excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *") def test_validate_frame_length_invalid_frame_length(self): """Validate that the validate_frame_length function behaves as expected when supplied with an invalid frame length. """ - with six.assertRaisesRegex( - self, - SerializationError, - "Frame size must be a non-negative multiple of the block size of the crypto algorithm: *", - ): + with pytest.raises(SerializationError) as excinfo: aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=1, algorithm=self.mock_algorithm) + excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *") def test_validate_frame_length_too_large(self): """Validate that the validate_frame_length function behaves as expected when supplied with a frame length which is too large. """ - with six.assertRaisesRegex(self, SerializationError, "Frame size too large: *"): + with pytest.raises(SerializationError) as excinfo: aws_encryption_sdk.internal.utils.validate_frame_length( frame_length=MAX_FRAME_SIZE + 1, algorithm=self.mock_algorithm ) + excinfo.match("Frame size too large: *") def test_message_id(self): """Validate that the message_id function behaves as expected.""" test = aws_encryption_sdk.internal.utils.message_id() self.mock_urandom.assert_called_once_with(MESSAGE_ID_LENGTH) - self.assertEqual(test, sentinel.random) + assert test == sentinel.random def test_get_aad_content_string_no_framing(self): """Validate that the get_aad_content_string function behaves @@ -214,8 +209,9 @@ def test_get_aad_content_string_framing_bad_type(self): """Validate that the get_aad_content_string function behaves as expected when called with an unknown content type. """ - with six.assertRaisesRegex(self, UnknownIdentityError, "Unhandled content type"): + with pytest.raises(UnknownIdentityError) as excinfo: aws_encryption_sdk.internal.utils.get_aad_content_string(-1, False) + excinfo.match("Unhandled content type") def test_prepare_data_keys(self): mock_encryption_dk = DataKey( @@ -265,9 +261,8 @@ def test_source_data_key_length_check_invalid(self): mock_algorithm.kdf_input_len = 5 mock_data_key = MagicMock() mock_data_key.data_key = "1234" - with six.assertRaisesRegex( - self, InvalidDataKeyError, "Invalid Source Data Key length 4 for algorithm required: 5" - ): + with pytest.raises(InvalidDataKeyError) as excinfo: aws_encryption_sdk.internal.utils.source_data_key_length_check( source_data_key=mock_data_key, algorithm=mock_algorithm ) + excinfo.match("Invalid Source Data Key length 4 for algorithm required: 5") From fdc05ffbb790df1ebc416fb249c36ec40f35a9ee Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Mon, 17 Dec 2018 17:04:00 -0600 Subject: [PATCH 2/3] Removed unused import and added yield and tearDown to apply_fixtures function --- test/unit/test_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index 28bab0573..c13f0e32b 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -15,7 +15,6 @@ import io import pytest -import six from mock import MagicMock, patch, sentinel import aws_encryption_sdk.identifiers @@ -139,8 +138,7 @@ def apply_fixtures(self): ) self.mock_aws_encryption_sdk_instance.decrypt.return_value = VALUES["data_key"] self.mock_aws_encryption_sdk_instance.encrypt.return_value = VALUES["encrypted_data_key"] - - def tearDown(self): + # Run tearDown self.mock_urandom_patcher.stop() def test_validate_frame_length_negative_frame_length(self): From 444a2eed1d246fd2e35535811ed41269be4c1dda Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Mon, 17 Dec 2018 17:36:37 -0600 Subject: [PATCH 3/3] Made changes to fix CI travis issue --- test/unit/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index c13f0e32b..b1374a09d 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -138,6 +138,7 @@ def apply_fixtures(self): ) self.mock_aws_encryption_sdk_instance.decrypt.return_value = VALUES["data_key"] self.mock_aws_encryption_sdk_instance.encrypt.return_value = VALUES["encrypted_data_key"] + yield # Run tearDown self.mock_urandom_patcher.stop()