Skip to content

Commit b790082

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate "test/unit/test_providers_base_master_key.py" from unittest to pytest (#119)
* Migrate unit/test_defaults.py from unittest to pytest * Migrate unit/test_providers_base_master_key.py from unittest to pytest * Removed one test from previous commit * Removed unused import and added yield and tearDown to apply_fixtures function
1 parent 33c55ae commit b790082

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

test/unit/test_providers_base_master_key.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Test suite for aws_encryption_sdk.key_providers.base.MasterKey"""
14-
import unittest
15-
1614
import attr
1715
import pytest
18-
import six
1916
from mock import MagicMock, patch, sentinel
2017

2118
from aws_encryption_sdk.exceptions import ConfigMismatchError, IncorrectMasterKeyError, InvalidKeyIdError
@@ -63,12 +60,13 @@ class FakeMasterKey(MockMasterKey):
6360
excinfo.match(r'MasterKey config classes must have a "provider_id" attribute defined.')
6461

6562

66-
class TestMasterKey(unittest.TestCase):
67-
def setUp(self):
63+
class TestMasterKey(object):
64+
@pytest.fixture(autouse=True)
65+
def apply_fixture(self):
6866
self.mock_data_key_len_check_patcher = patch("aws_encryption_sdk.internal.utils.source_data_key_length_check")
6967
self.mock_data_key_len_check = self.mock_data_key_len_check_patcher.start()
70-
71-
def tearDown(self):
68+
yield
69+
# Run tearDown
7270
self.mock_data_key_len_check_patcher.stop()
7371

7472
def test_parent(self):
@@ -85,8 +83,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
8583
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
8684
pass
8785

88-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
86+
with pytest.raises(TypeError) as excinfo:
8987
TestMasterKey()
88+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
9089

9190
def test_generate_data_key_enforcement(self):
9291
class TestMasterKey(MasterKey):
@@ -98,8 +97,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
9897
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
9998
pass
10099

101-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
100+
with pytest.raises(TypeError) as excinfo:
102101
TestMasterKey()
102+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
103103

104104
def test_encrypt_data_key_enforcement(self):
105105
class TestMasterKey(MasterKey):
@@ -111,8 +111,9 @@ def _generate_data_key(self, algorithm, encryption_context):
111111
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
112112
pass
113113

114-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
114+
with pytest.raises(TypeError) as excinfo:
115115
TestMasterKey()
116+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
116117

117118
def test_decrypt_data_key_enforcement(self):
118119
class TestMasterKey(MasterKey):
@@ -124,8 +125,9 @@ def _generate_data_key(self, algorithm, encryption_context):
124125
def _encrypt_data_key(self, data_key, algorithm, encryption_context):
125126
pass
126127

127-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
128+
with pytest.raises(TypeError) as excinfo:
128129
TestMasterKey()
130+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
129131

130132
def test_new(self):
131133
mock_master_key = MockMasterKey(
@@ -142,10 +144,9 @@ def test_new_conf_mismatch(self):
142144
mock_config = MagicMock()
143145
mock_config.__class__ = MockMasterKeyConfig
144146
mock_config.provider_id = sentinel.mismatched_provider_id
145-
with six.assertRaisesRegex(
146-
self, ConfigMismatchError, "Config provider_id does not match MasterKey provider_id: *"
147-
):
147+
with pytest.raises(ConfigMismatchError) as excinfo:
148148
MockMasterKey(config=mock_config)
149+
excinfo.match("Config provider_id does not match MasterKey provider_id: *")
149150

150151
def test_owns_data_key_owned(self):
151152
mock_master_key = MockMasterKey(
@@ -220,8 +221,9 @@ def test_new_master_key_invalid(self):
220221
mock_encrypted_data_key=sentinel.encrypted_data_key,
221222
mock_decrypted_data_key=sentinel.decrypted_data_key,
222223
)
223-
with six.assertRaisesRegex(self, InvalidKeyIdError, "MasterKeys can only provide themselves. *"):
224+
with pytest.raises(InvalidKeyIdError) as excinfo:
224225
mock_master_key._new_master_key(sentinel.another_key_id)
226+
excinfo.match("MasterKeys can only provide themselves. *")
225227

226228
def test_key_check_valid(self):
227229
mock_master_key = MockMasterKey(
@@ -243,8 +245,9 @@ def test_key_check_invalid(self):
243245
)
244246
mock_data_key = MagicMock()
245247
mock_data_key.key_provider = sentinel.another_key_provider
246-
with six.assertRaisesRegex(self, IncorrectMasterKeyError, "Provided data key provider *"):
248+
with pytest.raises(IncorrectMasterKeyError) as excinfo:
247249
mock_master_key._key_check(mock_data_key)
250+
excinfo.match("Provided data key provider *")
248251

249252
def test_generate_data_key(self):
250253
mock_master_key = MockMasterKey(

0 commit comments

Comments
 (0)