11
11
# ANY KIND, either express or implied. See the License for the specific
12
12
# language governing permissions and limitations under the License.
13
13
"""Test suite for aws_encryption_sdk.key_providers.base.MasterKey"""
14
- import unittest
15
-
16
14
import attr
17
15
import pytest
18
- import six
19
16
from mock import MagicMock , patch , sentinel
20
17
21
18
from aws_encryption_sdk .exceptions import ConfigMismatchError , IncorrectMasterKeyError , InvalidKeyIdError
@@ -63,12 +60,13 @@ class FakeMasterKey(MockMasterKey):
63
60
excinfo .match (r'MasterKey config classes must have a "provider_id" attribute defined.' )
64
61
65
62
66
- class TestMasterKey (unittest .TestCase ):
67
- def setUp (self ):
63
+ class TestMasterKey (object ):
64
+ @pytest .fixture (autouse = True )
65
+ def apply_fixture (self ):
68
66
self .mock_data_key_len_check_patcher = patch ("aws_encryption_sdk.internal.utils.source_data_key_length_check" )
69
67
self .mock_data_key_len_check = self .mock_data_key_len_check_patcher .start ()
70
-
71
- def tearDown ( self ):
68
+ yield
69
+ # Run tearDown
72
70
self .mock_data_key_len_check_patcher .stop ()
73
71
74
72
def test_parent (self ):
@@ -85,8 +83,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
85
83
def _decrypt_data_key (self , encrypted_data_key , algorithm , encryption_context ):
86
84
pass
87
85
88
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestMasterKey *" ) :
86
+ with pytest . raises ( TypeError ) as excinfo :
89
87
TestMasterKey ()
88
+ excinfo .match ("Can't instantiate abstract class TestMasterKey *" )
90
89
91
90
def test_generate_data_key_enforcement (self ):
92
91
class TestMasterKey (MasterKey ):
@@ -98,8 +97,9 @@ def _encrypt_data_key(self, data_key, algorithm, encryption_context):
98
97
def _decrypt_data_key (self , encrypted_data_key , algorithm , encryption_context ):
99
98
pass
100
99
101
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestMasterKey *" ) :
100
+ with pytest . raises ( TypeError ) as excinfo :
102
101
TestMasterKey ()
102
+ excinfo .match ("Can't instantiate abstract class TestMasterKey *" )
103
103
104
104
def test_encrypt_data_key_enforcement (self ):
105
105
class TestMasterKey (MasterKey ):
@@ -111,8 +111,9 @@ def _generate_data_key(self, algorithm, encryption_context):
111
111
def _decrypt_data_key (self , encrypted_data_key , algorithm , encryption_context ):
112
112
pass
113
113
114
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestMasterKey *" ) :
114
+ with pytest . raises ( TypeError ) as excinfo :
115
115
TestMasterKey ()
116
+ excinfo .match ("Can't instantiate abstract class TestMasterKey *" )
116
117
117
118
def test_decrypt_data_key_enforcement (self ):
118
119
class TestMasterKey (MasterKey ):
@@ -124,8 +125,9 @@ def _generate_data_key(self, algorithm, encryption_context):
124
125
def _encrypt_data_key (self , data_key , algorithm , encryption_context ):
125
126
pass
126
127
127
- with six . assertRaisesRegex ( self , TypeError , "Can't instantiate abstract class TestMasterKey *" ) :
128
+ with pytest . raises ( TypeError ) as excinfo :
128
129
TestMasterKey ()
130
+ excinfo .match ("Can't instantiate abstract class TestMasterKey *" )
129
131
130
132
def test_new (self ):
131
133
mock_master_key = MockMasterKey (
@@ -142,10 +144,9 @@ def test_new_conf_mismatch(self):
142
144
mock_config = MagicMock ()
143
145
mock_config .__class__ = MockMasterKeyConfig
144
146
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 :
148
148
MockMasterKey (config = mock_config )
149
+ excinfo .match ("Config provider_id does not match MasterKey provider_id: *" )
149
150
150
151
def test_owns_data_key_owned (self ):
151
152
mock_master_key = MockMasterKey (
@@ -220,8 +221,9 @@ def test_new_master_key_invalid(self):
220
221
mock_encrypted_data_key = sentinel .encrypted_data_key ,
221
222
mock_decrypted_data_key = sentinel .decrypted_data_key ,
222
223
)
223
- with six . assertRaisesRegex ( self , InvalidKeyIdError , "MasterKeys can only provide themselves. *" ) :
224
+ with pytest . raises ( InvalidKeyIdError ) as excinfo :
224
225
mock_master_key ._new_master_key (sentinel .another_key_id )
226
+ excinfo .match ("MasterKeys can only provide themselves. *" )
225
227
226
228
def test_key_check_valid (self ):
227
229
mock_master_key = MockMasterKey (
@@ -243,8 +245,9 @@ def test_key_check_invalid(self):
243
245
)
244
246
mock_data_key = MagicMock ()
245
247
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 :
247
249
mock_master_key ._key_check (mock_data_key )
250
+ excinfo .match ("Provided data key provider *" )
248
251
249
252
def test_generate_data_key (self ):
250
253
mock_master_key = MockMasterKey (
0 commit comments