Skip to content

Commit c2d81a3

Browse files
committed
updating tests of attrs-controlled tests to a) actually do something useful and b) not break with attrs 17.3.0
1 parent 1874fab commit c2d81a3

11 files changed

+379
-696
lines changed

test/unit/test_internal_structures.py

Lines changed: 47 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,60 @@
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.structures"""
14-
import attr
1514
import pytest
16-
import six
1715

1816
from aws_encryption_sdk.internal.structures import (
1917
EncryptedData, MessageFooter, MessageFrameBody, MessageHeaderAuthentication, MessageNoFrameBody
2018
)
19+
from .unit_test_utils import all_invalid_kwargs, all_valid_kwargs
20+
21+
22+
VALID_KWARGS = {
23+
EncryptedData: [
24+
dict(ciphertext=b'asjfoiwaj', iv=None, tag=None),
25+
dict(ciphertext=b'asjfoiwaj', iv=b'ajsdhfiuaweh', tag=None),
26+
dict(ciphertext=b'asjfoiwaj', iv=None, tag=b'aosijfoiewj'),
27+
dict(ciphertext=b'asjfoiwaj', iv=b'ajsdhfiuaweh', tag=b'aosijfoiewj')
28+
],
29+
MessageHeaderAuthentication: [
30+
dict(iv=b'oasijfoaiwej', tag=b'aisudhfoaweij')
31+
],
32+
MessageFrameBody: [
33+
dict(
34+
iv=b'oaijefoiajew',
35+
ciphertext=b'oasidjfaowiejf',
36+
tag=b'ecoaiwjeconadf',
37+
sequence_number=42523,
38+
final_frame=False
39+
)
40+
],
41+
MessageNoFrameBody: [
42+
dict(
43+
iv=b'afioaewj',
44+
ciphertext=b'oasjfoeiwjfio',
45+
tag=b'asfowaeijf'
46+
)
47+
],
48+
MessageFooter: [
49+
dict(signature=b'oajwefiowjaeofi')
50+
]
51+
}
52+
INVALID_KWARGS = {
53+
EncryptedData: [
54+
dict(ciphertext=None, iv=None, tag=None)
55+
]
56+
}
2157

2258

23-
@pytest.mark.parametrize('attribute, validator_type, is_optional', (
24-
(EncryptedData.iv, bytes, True),
25-
(EncryptedData.ciphertext, bytes, False),
26-
(EncryptedData.tag, bytes, True),
27-
(MessageHeaderAuthentication.iv, bytes, False),
28-
(MessageHeaderAuthentication.tag, bytes, False),
29-
(MessageFrameBody.iv, bytes, False),
30-
(MessageFrameBody.ciphertext, bytes, False),
31-
(MessageFrameBody.tag, bytes, False),
32-
(MessageFrameBody.sequence_number, six.integer_types, False),
33-
(MessageFrameBody.final_frame, bool, False),
34-
(MessageNoFrameBody.iv, bytes, False),
35-
(MessageNoFrameBody.ciphertext, bytes, False),
36-
(MessageNoFrameBody.tag, bytes, False),
37-
(MessageFooter.signature, bytes, False)
38-
))
39-
def test_attributes(attribute, validator_type, is_optional):
40-
assert isinstance(attribute, attr.Attribute)
41-
assert attribute.hash
42-
if is_optional:
43-
assert attribute.validator.validator.type == validator_type
44-
else:
45-
assert attribute.validator.type == validator_type
59+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
60+
def test_attributes_valid_kwargs(cls, kwargs):
61+
cls(**kwargs)
62+
63+
64+
@pytest.mark.parametrize('cls, kwargs', all_invalid_kwargs(VALID_KWARGS, INVALID_KWARGS))
65+
def test_attributes_invalid_kwargs(cls, kwargs):
66+
with pytest.raises(TypeError):
67+
cls(**kwargs)
4668

4769

4870
@pytest.mark.parametrize('attribute, value', (
@@ -51,82 +73,3 @@ def test_attributes(attribute, validator_type, is_optional):
5173
))
5274
def test_static_attributes(attribute, value):
5375
assert attribute == value
54-
55-
56-
def test_encrypted_data_fails():
57-
with pytest.raises(TypeError):
58-
EncryptedData(ciphertext=None)
59-
60-
61-
@pytest.mark.parametrize('iv, ciphertext, tag', (
62-
(b'iv', b'ciphertext', b'tag'),
63-
(None, b'ciphertext', None)
64-
))
65-
def test_encrypted_data_succeeds(iv, ciphertext, tag):
66-
EncryptedData(iv=iv, ciphertext=ciphertext, tag=tag)
67-
68-
69-
@pytest.mark.parametrize('iv, tag', (
70-
(None, b''),
71-
(b'', None)
72-
))
73-
def test_message_header_auth_fails(iv, tag):
74-
with pytest.raises(TypeError):
75-
MessageHeaderAuthentication(iv=iv, tag=tag)
76-
77-
78-
def test_message_header_auth_succeeds():
79-
MessageHeaderAuthentication(iv=b'', tag=b'')
80-
81-
82-
@pytest.mark.parametrize('iv, ciphertext, tag, sequence_number, final_frame', (
83-
(None, b'', b'', 1, True),
84-
(b'', None, b'', 1, True),
85-
(b'', b'', None, 1, True),
86-
(b'', b'', b'', None, True),
87-
(b'', b'', b'', 1, None)
88-
))
89-
def test_message_frame_body_fails(iv, ciphertext, tag, sequence_number, final_frame):
90-
with pytest.raises(TypeError):
91-
MessageFrameBody(
92-
iv=iv,
93-
ciphertext=ciphertext,
94-
tag=tag,
95-
sequence_number=sequence_number,
96-
final_frame=final_frame
97-
)
98-
99-
100-
def test_message_frame_body_succeeds():
101-
MessageFrameBody(
102-
iv=b'iv',
103-
ciphertext=b'ciphertext',
104-
tag=b'tag',
105-
sequence_number=1,
106-
final_frame=False
107-
)
108-
109-
110-
@pytest.mark.parametrize('iv, ciphertext, tag', (
111-
(None, b'', b''),
112-
(b'', None, b''),
113-
(b'', b'', None)
114-
))
115-
def test_message_no_frame_body_fails(iv, ciphertext, tag):
116-
with pytest.raises(TypeError):
117-
MessageNoFrameBody(iv=iv, ciphertext=ciphertext, tag=tag)
118-
119-
120-
def test_message_no_frame_body_succeeds():
121-
test = MessageNoFrameBody(iv=b'', ciphertext=b'', tag=b'')
122-
assert test.sequence_number == 1
123-
assert test.final_frame
124-
125-
126-
def test_message_footer_fails():
127-
with pytest.raises(TypeError):
128-
MessageFooter(signature=None)
129-
130-
131-
def test_message_footer_succeeds():
132-
MessageFooter(signature=b'')

test/unit/test_providers_base_master_key_config.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,39 @@
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 to validate aws_encryption_sdk.key_providers.base.MasterKeyConfig"""
14-
import attr
1514
from mock import sentinel
1615
import pytest
17-
import six
1816

19-
from aws_encryption_sdk.internal.str_ops import to_bytes
2017
from aws_encryption_sdk.key_providers.base import MasterKeyConfig
18+
from .unit_test_utils import all_invalid_kwargs, all_valid_kwargs
2119

2220

23-
@pytest.mark.parametrize('attribute, validator_type, convert_function', (
24-
(MasterKeyConfig.key_id, (six.string_types, bytes), to_bytes),
25-
))
26-
def test_attributes(attribute, validator_type, convert_function):
27-
assert isinstance(attribute, attr.Attribute)
28-
assert attribute.hash
29-
assert attribute.validator.type == validator_type
30-
assert attribute.convert is convert_function
21+
class FakeMasterKeyConfig(MasterKeyConfig):
22+
provider_id = None
3123

3224

33-
def test_attributes_fails():
34-
class TestConfig(MasterKeyConfig):
35-
provider_id = sentinel.provider_id
25+
VALID_KWARGS = {
26+
FakeMasterKeyConfig: [
27+
dict(key_id='a key id'),
28+
dict(key_id=b'a key id')
29+
]
30+
}
31+
32+
33+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
34+
def test_attributes_valid_kwargs(cls, kwargs):
35+
cls(**kwargs)
36+
37+
38+
@pytest.mark.parametrize('cls, kwargs', all_invalid_kwargs(VALID_KWARGS))
39+
def test_attributes_invalid_kwargs(cls, kwargs):
3640
with pytest.raises(TypeError):
37-
TestConfig(key_id=None)
41+
cls(**kwargs)
3842

3943

4044
@pytest.mark.parametrize('key_id', (b'key', 'key'))
4145
def test_attributes_converts(key_id):
42-
class TestConfig(MasterKeyConfig):
43-
provider_id = sentinel.provider_id
44-
test = TestConfig(key_id=key_id)
46+
test = FakeMasterKeyConfig(key_id=key_id)
4547
assert isinstance(test.key_id, bytes)
4648

4749

test/unit/test_providers_kms_master_key_config.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,41 @@
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 to validate aws_encryption_sdk.key_providers.kms.KMSMasterKeyConfig"""
14-
import attr
15-
import botocore.client
16-
from mock import MagicMock, sentinel
14+
import boto3
1715
import pytest
1816

1917
from aws_encryption_sdk.key_providers.base import MasterKeyConfig
2018
from aws_encryption_sdk.key_providers.kms import _PROVIDER_ID, KMSMasterKeyConfig
19+
from .unit_test_utils import all_invalid_kwargs, all_valid_kwargs
2120

2221

23-
def test_parent():
24-
assert issubclass(KMSMasterKeyConfig, MasterKeyConfig)
22+
VALID_KWARGS = {
23+
KMSMasterKeyConfig: [
24+
dict(key_id=b'a cmk', client=boto3.client('kms', region_name='us-west-2'), grant_tokens=()),
25+
dict(key_id=b'a cmk', client=boto3.client('kms', region_name='us-west-2'), grant_tokens=[]),
26+
dict(key_id=b'a cmk', client=boto3.client('kms', region_name='us-west-2'))
27+
]
28+
}
29+
INVALID_KWARGS = {
30+
KMSMasterKeyConfig: [
31+
dict(client=None)
32+
]
33+
}
2534

2635

27-
@pytest.mark.parametrize('attribute, default, validator_type, convert_function', (
28-
(KMSMasterKeyConfig.client, attr.NOTHING, botocore.client.BaseClient, None),
29-
(KMSMasterKeyConfig.grant_tokens, attr.Factory(tuple), tuple, tuple)
30-
))
31-
def test_attributes(attribute, default, validator_type, convert_function):
32-
assert isinstance(attribute, attr.Attribute)
33-
assert attribute.hash
34-
assert attribute.default == default
35-
assert attribute.validator.type == validator_type
36-
if convert_function is not None:
37-
assert attribute.convert is convert_function
36+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
37+
def test_attributes_valid_kwargs(cls, kwargs):
38+
cls(**kwargs)
39+
40+
41+
@pytest.mark.parametrize('cls, kwargs', all_invalid_kwargs(VALID_KWARGS, INVALID_KWARGS))
42+
def test_attributes_invalid_kwargs(cls, kwargs):
43+
with pytest.raises(TypeError):
44+
cls(**kwargs)
45+
46+
47+
def test_parent():
48+
assert issubclass(KMSMasterKeyConfig, MasterKeyConfig)
3849

3950

4051
@pytest.mark.parametrize('attribute, value', (
@@ -44,23 +55,15 @@ def test_static_attributes(attribute, value):
4455
assert attribute == value
4556

4657

47-
def test_attributes_fail():
48-
with pytest.raises(TypeError):
49-
KMSMasterKeyConfig(key_id='', client=None)
50-
51-
5258
def test_attributes_defaults():
5359
test = KMSMasterKeyConfig(
54-
key_id='',
55-
client=MagicMock(__class__=botocore.client.BaseClient)
60+
key_id=b'a cmk',
61+
client=boto3.client('kms', region_name='us-west-2')
5662
)
5763
assert test.grant_tokens == ()
5864

5965

60-
def test_attributes_converts():
61-
test = KMSMasterKeyConfig(
62-
key_id='',
63-
client=MagicMock(__class__=botocore.client.BaseClient),
64-
grant_tokens=[sentinel.token_1, sentinel.token_2]
65-
)
66-
assert test.grant_tokens == (sentinel.token_1, sentinel.token_2)
66+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
67+
def test_attributes_converts(cls, kwargs):
68+
test = cls(**kwargs)
69+
assert isinstance(test.grant_tokens, tuple)

test/unit/test_providers_kms_master_key_provider_config.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,49 @@
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 to validate aws_encryption_sdk.key_providers.kms.KMSMasterKeyProviderConfig"""
14-
import attr
1514
import botocore.session
16-
from mock import MagicMock, sentinel
1715
import pytest
1816

1917
from aws_encryption_sdk.key_providers.base import MasterKeyProviderConfig
2018
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProviderConfig
19+
from .unit_test_utils import all_invalid_kwargs, all_valid_kwargs
2120

2221

23-
def test_parent():
24-
assert issubclass(KMSMasterKeyProviderConfig, MasterKeyProviderConfig)
22+
VALID_KWARGS = {
23+
KMSMasterKeyProviderConfig: [
24+
dict(botocore_session=botocore.session.Session(), key_ids=(), region_names=()),
25+
dict(botocore_session=botocore.session.Session(), key_ids=[], region_names=()),
26+
dict(botocore_session=botocore.session.Session(), key_ids=(), region_names=[]),
27+
dict(botocore_session=botocore.session.Session(), region_names=()),
28+
dict(botocore_session=botocore.session.Session(), key_ids=()),
29+
dict(botocore_session=botocore.session.Session())
30+
]
31+
}
32+
INVALID_KWARGS = {
33+
KMSMasterKeyProviderConfig: [dict(botocore_session=None)]
34+
}
2535

2636

27-
@pytest.mark.parametrize('attribute, default, validator_type, convert_function', (
28-
(
29-
KMSMasterKeyProviderConfig.botocore_session,
30-
attr.Factory(botocore.session.Session),
31-
botocore.session.Session,
32-
None
33-
),
34-
(KMSMasterKeyProviderConfig.key_ids, attr.Factory(tuple), tuple, tuple),
35-
(KMSMasterKeyProviderConfig.region_names, attr.Factory(tuple), tuple, tuple)
36-
))
37-
def test_attributes(attribute, default, validator_type, convert_function):
38-
assert isinstance(attribute, attr.Attribute)
39-
assert attribute.hash
40-
assert attribute.default == default
41-
assert attribute.validator.type == validator_type
42-
if convert_function is not None:
43-
assert attribute.convert is convert_function
37+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
38+
def test_attributes_valid_kwargs(cls, kwargs):
39+
cls(**kwargs)
4440

4541

46-
def test_attributes_fails():
42+
@pytest.mark.parametrize('cls, kwargs', all_invalid_kwargs(VALID_KWARGS, INVALID_KWARGS))
43+
def test_attributes_invalid_kwargs(cls, kwargs):
4744
with pytest.raises(TypeError):
48-
KMSMasterKeyProviderConfig(botocore_session=None)
45+
cls(**kwargs)
46+
47+
48+
def test_parent():
49+
assert issubclass(KMSMasterKeyProviderConfig, MasterKeyProviderConfig)
4950

5051

51-
def test_attributes_converts():
52-
test = KMSMasterKeyProviderConfig(
53-
botocore_session=MagicMock(__class__=botocore.session.Session),
54-
key_ids=[sentinel.key_a, sentinel.key_b],
55-
region_names=[sentinel.region_a, sentinel.region_b]
56-
)
57-
assert test.key_ids == (sentinel.key_a, sentinel.key_b)
58-
assert test.region_names == (sentinel.region_a, sentinel.region_b)
52+
@pytest.mark.parametrize('cls, kwargs', all_valid_kwargs(VALID_KWARGS))
53+
def test_attributes_converts(cls, kwargs):
54+
test = cls(**kwargs)
55+
assert isinstance(test.key_ids, tuple)
56+
assert isinstance(test.region_names, tuple)
5957

6058

6159
def test_attributes_defaults():

0 commit comments

Comments
 (0)