Skip to content

Commit 32cdf5b

Browse files
author
Andres Sanchez
committed
Migrate unit/test_providers_base_master_key.py from unittest to pytest
1 parent 9eef35e commit 32cdf5b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

test/unit/test_providers_base_master_key.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
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
1816
import six
@@ -63,8 +61,9 @@ class FakeMasterKey(MockMasterKey):
6361
excinfo.match(r'MasterKey config classes must have a "provider_id" attribute defined.')
6462

6563

66-
class TestMasterKey(unittest.TestCase):
67-
def setUp(self):
64+
class TestMasterKey(object):
65+
@pytest.fixture(autouse=True)
66+
def apply_fixture(self):
6867
self.mock_data_key_len_check_patcher = patch("aws_encryption_sdk.internal.utils.source_data_key_length_check")
6968
self.mock_data_key_len_check = self.mock_data_key_len_check_patcher.start()
7069

@@ -85,8 +84,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
8584
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
8685
pass
8786

88-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
87+
with pytest.raises(TypeError) as excinfo:
8988
TestMasterKey()
89+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
9090

9191
def test_generate_data_key_enforcement(self):
9292
class TestMasterKey(MasterKey):
@@ -98,8 +98,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
9898
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
9999
pass
100100

101-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
101+
with pytest.raises(TypeError) as excinfo:
102102
TestMasterKey()
103+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
103104

104105
def test_encrypt_data_key_enforcement(self):
105106
class TestMasterKey(MasterKey):
@@ -111,8 +112,9 @@ def _generate_data_key(self, algorithm, encryption_context):
111112
def _decrypt_data_key(self, encrypted_data_key, algorithm, encryption_context):
112113
pass
113114

114-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
115+
with pytest.raises(TypeError) as excinfo:
115116
TestMasterKey()
117+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
116118

117119
def test_decrypt_data_key_enforcement(self):
118120
class TestMasterKey(MasterKey):
@@ -124,8 +126,9 @@ def _generate_data_key(self, algorithm, encryption_context):
124126
def _encrypt_data_key(self, data_key, algorithm, encryption_context):
125127
pass
126128

127-
with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestMasterKey *"):
129+
with pytest.raises(TypeError) as excinfo:
128130
TestMasterKey()
131+
excinfo.match("Can't instantiate abstract class TestMasterKey *")
129132

130133
def test_new(self):
131134
mock_master_key = MockMasterKey(
@@ -142,10 +145,9 @@ def test_new_conf_mismatch(self):
142145
mock_config = MagicMock()
143146
mock_config.__class__ = MockMasterKeyConfig
144147
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-
):
148+
with pytest.raises(ConfigMismatchError) as excinfo:
148149
MockMasterKey(config=mock_config)
150+
excinfo.match("Config provider_id does not match MasterKey provider_id: *")
149151

150152
def test_owns_data_key_owned(self):
151153
mock_master_key = MockMasterKey(
@@ -220,8 +222,9 @@ def test_new_master_key_invalid(self):
220222
mock_encrypted_data_key=sentinel.encrypted_data_key,
221223
mock_decrypted_data_key=sentinel.decrypted_data_key,
222224
)
223-
with six.assertRaisesRegex(self, InvalidKeyIdError, "MasterKeys can only provide themselves. *"):
225+
with pytest.raises(InvalidKeyIdError) as excinfo:
224226
mock_master_key._new_master_key(sentinel.another_key_id)
227+
excinfo.match("MasterKeys can only provide themselves. *")
225228

226229
def test_key_check_valid(self):
227230
mock_master_key = MockMasterKey(
@@ -243,8 +246,9 @@ def test_key_check_invalid(self):
243246
)
244247
mock_data_key = MagicMock()
245248
mock_data_key.key_provider = sentinel.another_key_provider
246-
with six.assertRaisesRegex(self, IncorrectMasterKeyError, "Provided data key provider *"):
249+
with pytest.raises(IncorrectMasterKeyError) as excinfo:
247250
mock_master_key._key_check(mock_data_key)
251+
excinfo.match("Provided data key provider *")
248252

249253
def test_generate_data_key(self):
250254
mock_master_key = MockMasterKey(

0 commit comments

Comments
 (0)