Skip to content

Commit 4857df9

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate unit/test_encryption_context.py from unittest to pytest (#118)
* Migrate unit/test_encryption_context.py from unittest to pytest * Removed unused import
1 parent b790082 commit 4857df9

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

test/unit/test_encryption_context.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Unit test suite for aws_encryption_sdk.internal.formatting.encryption_context"""
14-
import unittest
15-
1614
import pytest
17-
import six
1815

1916
import aws_encryption_sdk.internal.defaults
2017
import aws_encryption_sdk.internal.formatting.encryption_context
@@ -26,7 +23,7 @@
2623
pytestmark = [pytest.mark.unit, pytest.mark.local]
2724

2825

29-
class TestEncryptionContext(unittest.TestCase):
26+
class TestEncryptionContext(object):
3027
def test_assemble_content_aad(self):
3128
"""Validate that the assemble_content_aad function
3229
behaves as expected.
@@ -40,10 +37,11 @@ def test_assemble_content_aad(self):
4037
assert test == VALUES["non_framed_aac"]
4138

4239
def test_assemble_content_aad_unknown_type(self):
43-
with six.assertRaisesRegex(self, SerializationError, "Unknown aad_content_string"):
40+
with pytest.raises(SerializationError) as excinfo:
4441
aws_encryption_sdk.internal.formatting.encryption_context.assemble_content_aad(
4542
message_id=VALUES["message_id"], aad_content_string=None, seq_num=1, length=VALUES["content_len"]
4643
)
44+
excinfo.match("Unknown aad_content_string")
4745

4846
def test_serialize_encryption_context_no_encryption_context(self):
4947
"""Validate that the serialize_encryption_context
@@ -59,20 +57,22 @@ def test_serialize_encryption_context_too_many_elements(self):
5957
with an encryption context with too many
6058
elements.
6159
"""
62-
with six.assertRaisesRegex(self, SerializationError, "The encryption context contains too many elements."):
60+
with pytest.raises(SerializationError) as excinfo:
6361
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
6462
VALUES["encryption_context_too_many_elements"]
6563
)
64+
excinfo.match("The encryption context contains too many elements.")
6665

6766
def test_serialize_encryption_context_too_large(self):
6867
"""Validate that the serialize_encryption_context
6968
function behaves as expected when presented
7069
with an encryption context which is too large.
7170
"""
72-
with six.assertRaisesRegex(self, SerializationError, "The serialized context is too large"):
71+
with pytest.raises(SerializationError) as excinfo:
7372
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
7473
VALUES["encryption_context_too_large"]
7574
)
75+
excinfo.match("The serialized context is too large")
7676

7777
def test_serialize_encryption_context_unencodable(self):
7878
"""Validate that the serialize_encryption_context
@@ -81,10 +81,11 @@ def test_serialize_encryption_context_unencodable(self):
8181
unencodable elements.
8282
"""
8383
for encryption_context in [{"a": b"\xc4"}, {b"\xc4": "a"}, {b"\xc4": b"\xc4"}]:
84-
with six.assertRaisesRegex(self, SerializationError, "Cannot encode dictionary key or value using *"):
84+
with pytest.raises(SerializationError) as excinfo:
8585
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
8686
encryption_context
8787
)
88+
excinfo.match("Cannot encode dictionary key or value using *")
8889

8990
def test_serialize_encryption_context_valid(self):
9091
"""Validate that the serialize_encryption_context
@@ -100,8 +101,9 @@ def test_read_short_too_short(self):
100101
"""Validate that the read_short function behaves
101102
as expected when it encounters a struct error.
102103
"""
103-
with six.assertRaisesRegex(self, SerializationError, "Bad format of serialized context."):
104+
with pytest.raises(SerializationError) as excinfo:
104105
aws_encryption_sdk.internal.formatting.encryption_context.read_short(b"d", 0)
106+
excinfo.match("Bad format of serialized context.")
105107

106108
def test_read_short_valid(self):
107109
"""Validate that the read_short function behaves
@@ -116,8 +118,9 @@ def test_read_string_encoding_error(self):
116118
as expected when it encounters an encoding
117119
error.
118120
"""
119-
with six.assertRaisesRegex(self, SerializationError, "Bad format of serialized context."):
121+
with pytest.raises(SerializationError) as excinfo:
120122
aws_encryption_sdk.internal.formatting.encryption_context.read_string(b"\xc4", 0, 1)
123+
excinfo.match("Bad format of serialized context.")
121124

122125
def test_read_string_valid(self):
123126
"""Validate that the read_string function behaves
@@ -136,21 +139,23 @@ def test_deserialize_encryption_context_too_large(self):
136139
data = ""
137140
for i in range(aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE + 1):
138141
data += str(i)
139-
with six.assertRaisesRegex(self, SerializationError, "Serialized context is too long."):
142+
with pytest.raises(SerializationError) as excinfo:
140143
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
141144
serialized_encryption_context=data
142145
)
146+
excinfo.match("Serialized context is too long.")
143147

144148
def test_deserialize_encryption_context_duplicate_key(self):
145149
"""Validate that the deserialize_encryption_context
146150
function behaves as expected when it encounters
147151
a serialized encryption context which contains
148152
duplicate keys.
149153
"""
150-
with six.assertRaisesRegex(self, SerializationError, "Duplicate key in serialized context."):
154+
with pytest.raises(SerializationError) as excinfo:
151155
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
152156
serialized_encryption_context=VALUES["serialized_encryption_context_duplicate_key"]
153157
)
158+
excinfo.match("Duplicate key in serialized context.")
154159

155160
def test_deserialize_encryption_context_extra_data(self):
156161
"""Validate that the deserialize_encryption_context
@@ -160,10 +165,11 @@ def test_deserialize_encryption_context_extra_data(self):
160165
of pairs (formatting error).
161166
"""
162167
data = VALUES["serialized_encryption_context"] + b"jhofguijhsuskldfh"
163-
with six.assertRaisesRegex(self, SerializationError, "Formatting error: Extra data in serialized context."):
168+
with pytest.raises(SerializationError) as excinfo:
164169
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
165170
serialized_encryption_context=data
166171
)
172+
excinfo.match("Formatting error: Extra data in serialized context.")
167173

168174
def test_deserialize_encryption_context_valid(self):
169175
"""Validate that the deserialize_encryption_context

0 commit comments

Comments
 (0)